Reputation: 11
I'm trying to access the list personal
from some functions in the main program but I keep getting that it's not an instance of...
The class code:
[Serializable()]
class FaktNr
{
public int lopnummer;
public int year;
public List<string> personal = new List<string>();
public FaktNr()
{
personal = new List<string>();
}
}
The form code:
public partial class Form1 : Form
{
internal FaktNr faktNr = new FaktNr();
public Form1()
{
InitializeComponent();
}
private void laggTillPerson_Click(object sender, EventArgs e)
{
faktNr.personal.Add(ComboBox1.Text);
}
Code is shortened here but it shows the essentials. Nullreferenceexception occurs in function laggTillPerson_Click
.
I want to add that its not the ComboBox that is the problem since i already tried this: faktNr.personal.Add("uhiouh");
Upvotes: 1
Views: 120
Reputation: 223422
You are getting the exception on ComboBox1.Text
, your ComboBox1
could be null, not your list personal
, Try replacing your code with:
faktNr.personal.Add("test string");
and see if you still get the exception.
You are accessing the Text
property of the Combobox, instead you may use ComboBox.SelectedText Property
Or you can check for null in your event:
private void laggTillPerson_Click(object sender, EventArgs e)
{
if(ComboBox1 != null)
faktNr.personal.Add(ComboBox1.Text);
}
Upvotes: 1