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.
answered Jul 14, 2010 at 7:10 68.2k 28 28 gold badges 158 158 silver badges 197 197 bronze badgesYou have got a System.NullReferenceException. One of you objects was null and can not be used:
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.
answered Jul 14, 2010 at 7:09 49.4k 39 39 gold badges 196 196 silver badges 281 281 bronze badgesIt 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.
answered Jul 14, 2010 at 7:10 46.3k 9 9 gold badges 82 82 silver badges 112 112 bronze badges"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()
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.
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.