-1

EDIT: New to stackoverflow. That means I don't have enough experience points or whatever to ask for help in already existing questions. I have read the other questions about this error code and they don't appear to apply to my circumstance. If you think this is an exact duplicate question, then please leave a comment stating which question this is a duplicate of. Better yet, what answer of the duplicate question can help me. I'd love to see it. New to C# and new to OOP. That means I may not understand explanations provided in similar questions, yet, since I'm new to stackoverflow, I don't have enough points to ask for further explanation in those questions. How am I supposed to learn in such an exclusive environment? Or is this forum for experts only?

I'm new to C#, OOP, and stackoverflow. I'm doing a Windows Forms solution in .NET 2.0(Using API to control a Bluetooth dongle that was made in .NET 2.0.

In Program.cs I tried to make an interface between a class that's written in Program.cs to the form class Form1. When I try to start the program with debugging, it stops with the error An unhandled exception of type 'System.NullReferenceException' occurred in EP1 Additional information: Object reference not set to an instance of an object.

I made the interface based on the "even better solution" shown here, except in my case I'm trying to add a string generated in CCINS_Comm class to a listbox in the Form1 class

So, in Program.cs I have:

namespace EP1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }

    class CCINS_Comm : IDisposable
    {
        // Other stuff like constants
        //                  members
        //                  properties
        //                  constructor

// Do I have this in the right place in my class?

        private readonly IForm1Interface Form1;
        public CCINS_Comm(IForm1Interface Form1)
        {
            this.Form1 = Form1;
        }


        /// <summary>
        /// Start scan
        /// </summary>
        public CyApiErr StartScan()
        {
            return StartScanHelper();
        }

     /// <summary>
    /// Setup the scan result handler to log the discovered BLE devices
    /// </summary>
    private void SetupScanResultHandler()
        {
            m_scanCb.ScanResultHandler = (result) =>
            {
                if (result != null)
                {
                    StringBuilder SB = new StringBuilder();
                    foreach (var item in result.ScanRecords)
                    { 
                        SB.Length = 0;
                        SB.AppendFormat("Peer device: [{0:X12}, {1}], ADV_TYPE: {2}, RSSI: {3}",
                            item.PeerDeviceAddress.Address,
                            item.PeerDeviceAddress.AddressType,
                            item.AdvertisementType,
                            item.RSSI);
                        if(SB.ToString() != "")
                        {
                            string str = SB.ToString();    // Troubleshooting
                            Form1.AddToScanResultsList(str); // Error happens here
                        }

                    }
                }
            };
        }
   }
}

Then, in Form1.cs, I have:

namespace EP1
{

    // This is the interface.  Is it in the right place?
    interface IForm1Interface
    {
        void AddToScanResultsList(string resultString);
    }



    public partial class Form1 : Form, IForm1Interface
    {
        CCINS_Comm ccinsComm = new CCINS_Comm();  // Generates a new instance of CCINS_Comm for me to work with


        public Form1()
        {
            InitializeComponent();
            InitalizeStimControls();
        }


       // various methods handling manipulation of controls, etc.


        // method that the interface refers to 
        public void AddToScanResultsList(string resultString)
        {
            listBoxScanResults.Items.Add(resultString);
        }

    }
}
Community
  • 1
  • 1
Greg
  • 15
  • 7
  • 1
    Where `CCINS_Comm() ` comes from? I see `CCINS_Comm(IForm1Interface Form1)` only. – Diligent Key Presser May 30 '16 at 01:16
  • @DiligentKeyPresser, Could you explain a little more? I don't understand what you are asking. – Greg May 30 '16 at 15:55
  • @fredrik-mörk I'm trying to use your answer to a previous question [shown here](http://stackoverflow.com/questions/5646954/how-to-access-winform-textbox-control-from-another-class), but It causes the error mentioned in this question. Please help? – Greg May 30 '16 at 17:14
  • Sure. Code that you posted here wont even compile. You use default constructor `new CCINS_Comm()`, but you dont define it. Please post an actual code. – Diligent Key Presser May 31 '16 at 00:35

1 Answers1

0
public CCINS_Comm(IForm1Interface Form1)
{
   this.Form1 = Form1;
}

you only have 1 constructor for CCINS_Comm class. So that class must have IForm1Interface as parameter when you instantiate that into an object. Check this out

  • I'm confused. The code you're showing me, is already in the CCINS_Com class. Does it need to go somewhere else too? – Greg May 30 '16 at 15:40
  • ' CCINS_Comm ccinsComm = new CCINS_Comm(); // Generates a new instance of CCINS_Comm for me to work with' <- you create the instance without any parameter. You must inject the IForm1Interface on the instantiation. just like 'var ccinsComm = new CCINS_Comm(form1)' so SetupScanResultHandler doesn't give you null error – Reynaldi Pranata May 30 '16 at 16:37
  • if I try `var ccinsComm = new CCINS_Comm(form1)`, I get an error for var `The type or namespace 'var' could not be found. Also, for form1, I get the name form1 does not exist in the current context. Should I have this line in addition to the original line `CCINS_Comm ccinsComm = new CCINS_Comm()` or instead of? – Greg May 30 '16 at 17:06
  • I looked up `var` and it appears it is meant for method level scope, so I tried adding the line `var ccinsComm = new CCINS_Comm(form1)` to the SetupScanResultsHandler method, but it doesn't seem to help. I'll edit my post so that the program reflects what I've got right now. – Greg May 30 '16 at 17:51