J
Joey Johnson
I'm used to Java and am new to C#! I'm getting the following error, which
I believe is suggesting my code is referencing something that has not
been instanciated yet, however I can't seem to spot the bug...
"Object reference not set to an instance of an object"
What I'm TRYING to do is declare a publicly accessible class instance in
MAIN. Then other classes would create a reference to the MAIN class's
instance. My actual code snippets are:
----- FormMain.cs -----
public class FormMain : System.Windows.Forms.Form
{
...
public static FormMain formMain = new FormMain();
public DataHandler dispatcher = new DataHandler();
static void Main()
{
formMain.initializeData();
Application.Run(formMain);
}
...
}
----- DataHandler.cs -----
public class DataHandler : System.Windows.Forms.Form
{
...
public DataHandler dispatcher = FormMain.formMain.dispatcher;
...
dispatcher.setDay("Sunday");
...
}
Although this creates an error, getting rid of the 2 lines and above and
replacing them with this, works:
FormMain.formMain.dispatcher.setDay("Sunday");
Why!? What the heck is the difference and is there a workaround to get my
original code to work!?
-Joey
I believe is suggesting my code is referencing something that has not
been instanciated yet, however I can't seem to spot the bug...
"Object reference not set to an instance of an object"
What I'm TRYING to do is declare a publicly accessible class instance in
MAIN. Then other classes would create a reference to the MAIN class's
instance. My actual code snippets are:
----- FormMain.cs -----
public class FormMain : System.Windows.Forms.Form
{
...
public static FormMain formMain = new FormMain();
public DataHandler dispatcher = new DataHandler();
static void Main()
{
formMain.initializeData();
Application.Run(formMain);
}
...
}
----- DataHandler.cs -----
public class DataHandler : System.Windows.Forms.Form
{
...
public DataHandler dispatcher = FormMain.formMain.dispatcher;
...
dispatcher.setDay("Sunday");
...
}
Although this creates an error, getting rid of the 2 lines and above and
replacing them with this, works:
FormMain.formMain.dispatcher.setDay("Sunday");
Why!? What the heck is the difference and is there a workaround to get my
original code to work!?
-Joey