What does "Object reference not set to an instance of an object" mean?
stri开发者_如何学Cng[] splitstr =
this.strArraylist1[this.comboBox1.SelectedIndex].Split(delimeter, 5);
this.textBox4.Text = splitstr[1];
At this statement I'm getting the error:
Object reference not set to an instance of an object
I'm guessing strArraylist1
is null
, or one of it's items is null
. If you try and perform a method on something that is null
then you will get that error.
For example, this will throw an Object reference not set to an instance of an object error.
string[] strArrayList1 = new string[] { null, "Bar" };
string result = strArrayList[0].Split(delimeter, 5); // Error will occur here
The following would work fine:
string[] strArrayList1 = new string[] { "Foo", "Bar" };
string result = strArrayList1[0]; // Result will be "Foo"
Other possibilities are that comboBox1
or textBox4
are null. Although as they're probably controls on your form, that's unlikely, so my bet is that strArrayList1
is the culprit, so check the contents of strArrayList1
. If you are calling Split
on a null item, as per my example, that'll be the culprit.
You have got a System.NullReferenceException. One of you objects was null
and can not be used:
- strArraylist1
- comboBox1
- textBox4
Use the debugger, set a breakpoint, hover with the mouse over each object to see wich one. Once you found it make sure that is instanciated at the time you use it.
It means the result of this.strArraylist1[this.comboBox1.SelectedIndex]
is null, so when you try to call .Split(delimeter, 5)
the object it is being called by is null, hence the exception.
"Object reference not set to an instance of an object" means that you're trying to use the value that is associated with a variable, as an object reference, but that value is null, rather than point to an object. Which means that any operations you do on that supposed object will not work, because you're effectively doing: null.Something()
Consider this:
string[] splitstr = null;
this.textBox4.Text = splitstr[1];
This doesn't work, because the variable splitstr doesn't hold an object, it holds null. Then, when you go on to do splitstr[1], you effectively trying to do null[1] - which can't work, since null isn't an array, let alone one with at least two elements in it.
In your example, you could have nulls stored in:
strArraylist1 this.comboBox1 strArraylist1[this.comboBox1.SelectedIndex] this.textBox4
An additional potential problem with your code is that you may go out of bounds on the arrays splitstr and strArraylist1.
Check that:
splitstr will always hold at least two elements (because of [1]) and that strArraylist1 will always hold enough to accomodate the number produced by this.comboBox1.SelectedIndex.
Add a break point to the first line in your give code and look for a null value or an index out of range. That should give you the clue to catch the error.
I had this problem in WPF, my problem was caused by the root, instead of using the project root, it was using the root of the class, all objects where visible in the class but it did not get passed the class root when i called it, the way around it is to include the object trough the function in the class.
//Class
public void call(object b)
{
//Do stuff
}
//main Thread
Core Menu = new Core();
Menu.call(Object);
精彩评论