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);
}
}
}