Skip to content

Always getting an exception GpioConnection: Object reference not set to an instance of an object #86

Open

Description

@Asshen

Hey.

I have a Raspberry Pi 3 B. I did some GPIO development using Python, but I would love to use C#.
I set up a small project (Raspberry.IO references are OK):

using System;
using Raspberry.IO.GeneralPurpose;

namespace Lichtje1
{
    class Program
    {
        public static void Main( string[] args )
        {
            var led1 = ConnectorPin.P1Pin15.Output();

            var connection = new GpioConnection( led1 );

            for( var i = 0; i < 100; i++ )
            {
                connection.Toggle( led1 );
                System.Threading.Thread.Sleep( 250 );
            }

            connection.Close();
        }
    }
}

I keep getting the following error no matter if I compile on Visual Studio (on Windows) or MonoDevelop (on Raspberry Pi):

Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
  at Raspberry.IO.GeneralPurpose.GpioConnection.Allocate (Raspberry.IO.GeneralPurpose.PinConfiguration configuration) [0x00073] in <39616ab275014559a5b975fe859e9ad9>:0
  at Raspberry.IO.GeneralPurpose.GpioConnection.Open () [0x00036] in <39616ab275014559a5b975fe859e9ad9>:0
  at Raspberry.IO.GeneralPurpose.GpioConnection..ctor (Raspberry.IO.GeneralPurpose.GpioConnectionSettings settings, System.Collections.Generic.IEnumerable`1[T] pins) [0x000f8] in <39616ab275014559a5b975fe859e9ad9>:0
  at Raspberry.IO.GeneralPurpose.GpioConnection..ctor (Raspberry.IO.GeneralPurpose.PinConfiguration[] pins) [0x00000] in <39616ab275014559a5b975fe859e9ad9>:0
  at Lichtje1.Program.Main (System.String[] args) [0x00011] in <fb46ca8bbdcc45f0952b44480e70cfba>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
  at Raspberry.IO.GeneralPurpose.GpioConnection.Allocate (Raspberry.IO.GeneralPurpose.PinConfiguration configuration) [0x00073] in <39616ab275014559a5b975fe859e9ad9>:0
  at Raspberry.IO.GeneralPurpose.GpioConnection.Open () [0x00036] in <39616ab275014559a5b975fe859e9ad9>:0
  at Raspberry.IO.GeneralPurpose.GpioConnection..ctor (Raspberry.IO.GeneralPurpose.GpioConnectionSettings settings, System.Collections.Generic.IEnumerable`1[T] pins) [0x000f8] in <39616ab275014559a5b975fe859e9ad9>:0
  at Raspberry.IO.GeneralPurpose.GpioConnection..ctor (Raspberry.IO.GeneralPurpose.PinConfiguration[] pins) [0x00000] in <39616ab275014559a5b975fe859e9ad9>:0
  at Lichtje1.Program.Main (System.String[] args) [0x00011] in <fb46ca8bbdcc45f0952b44480e70cfba>:0

Thank you for any help.
Kind regards.
Kris.

Activity

  1. arakis commented on May 7, 2017

    @arakis

    i have a similar problem:

    System.NullReferenceException: Object reference not set to an instance of an object
    at Raspberry.IO.GeneralPurpose.GpioOutputBinaryPin..ctor
    

    Its since the upgrade from kernel ARCH Linux 4.4 to 4.9

  2. Bulinlinbu commented on May 30, 2017

    @Bulinlinbu

    So do I

    It seems that "Driver" is null GeneralPurpose > GpioConnection.cs line 488 :
    Driver.Allocate(configuration.Pin, configuration.Direction);

  3. Bulinlinbu commented on May 30, 2017

    @Bulinlinbu

    OK, il think the problem is from Raspberry.System API, it doesn't reconize the CPU of this version of PI.

    If I do this :
    Raspberry.Board.Current.IsRaspberryPi; // False

    See this request :
    raspberry-sharp/raspberry-sharp-system#6

  4. Bulinlinbu commented on May 30, 2017

    @Bulinlinbu

    Right, it works for me.
    First : in Raspberry.System librairy, add the BCM2835 CPU
    see this post : raspberry-sharp/raspberry-sharp-system#6

    Next : In Raspberry.IO.GeneralPurpose, edit the function :
    In GpioConnectionDriver.cs line 291
    and in MemoryGpioConnectionDriver line 246

    private static uint GetProcessorBaseAddress(Processor processor)
    {
    	switch (processor)
    	{
    		case Processor.Bcm2708:
    			return Interop.BCM2835_GPIO_BASE;
    		case Processor.Bcm2709:
    		case Processor.Bcm2835: // Add this
    			return Interop.BCM2836_GPIO_BASE;
    		default:
    			throw new ArgumentOutOfRangeException("processor");
    	}
    }
    

    IT is magic !

  5. NeededAnUnusedName commented on Jul 2, 2017

    @NeededAnUnusedName

    Hi, can anyone please repeat the steps (as simple as possible ^^) to solve this problem? There is a dead link. I found the function above but there are so many build errors... Is there a simpler solution, or can I maybe update to a fixed version with nuget?

  6. Bulinlinbu commented on Jul 2, 2017

    @Bulinlinbu

    Link not dead anymore

  7. NeededAnUnusedName commented on Jul 2, 2017

    @NeededAnUnusedName

    Thanks, maybe fix the other link too, I still cant reach it.
    But it helped a lot, thank you very much! =)

  8. JohnRusk commented on Oct 22, 2017

    @JohnRusk

    A note for others who find this page: it's recommended to use the revision number now, for identifying the processor. See raspberrypi/linux#2008 and raspberrypi/firmware#705

    Also, I wonder if even older Pi's report Bcm2835 as the Hardware value in /proc/cpuinfonow. (The change, after all, was in the OS kernel, not the actual hardware). If that's the case, the both old and new Pi's may be reporting the same value, in which case the fix suggested by @Bulinlinbu above will not be safe on older Pi's. I have no way of testing this tho, since I only have a new one.

    However, if you know you're only running on a RPi 3, then @Bulinlinbu's change looks safe enough.

    Finally, test on my RPi3 indicates that this quick and dirty workaround also works, if you know you are running on a RPi 3:

    Dictionary<string, string> boardSettings = 
                       (Dictionary<string, string>) Board.Current.GetType()
                      .GetField("settings", BindingFlags.Instance | BindingFlags.NonPublic)
                     .GetValue(Board.Current);
    boardSettings["Hardware"] = "BCM2709";
    

    I wouldn't advocate using that for any long-term purpose, but as a quick workaround until R#.IO, for users who know they are on RPi3, it seems to work. It amounts to forcing in the value that the library expects to see on an RPi 2 - which results in the correct GPIO memory addresses being selected because the addresses are the same on the Pi 3.

  9. Dip11 commented on Aug 7, 2018

    @Dip11

    install Raspberry.IO.GeneralPurpose3 -Version 3.1.1 instead of Raspberry.IO.GeneralPurpose as nuget package. Hope ur problem will be solved.

  10. JTrotta commented on Aug 7, 2018

    @JTrotta

    Raspberry.IO.GeneralPurpose3 has been disocontinued.
    Try https://github.com/JTrotta/RaspberrySharp

  11. piibet commented on Dec 16, 2018

    @piibet

    Please make sure you have enabled all Interfaces in Rasperry Pi Configuration.
    I forgot to do that, and it gave me same error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions