vb.net - system.nullReferenceException throwed, while starting a loop in a loop using arrays and dictionaries -
i have system.nullreferenceexcpetion. here code. exception thrown @ marked line. if needed can give inizializing of used arrays , dictionaries.
public sub mappen() dim feld(form1.arr1.length) string integer = 0 form1.arr1.length - 1 feld(i) = form1.datagridview1.rows(i).cells(1).value.tostring ***for ix integer = 0 form1.spaltennamen.length - 1*** select case feld(i) case form1.spaltennamen(ix) form1.comboauswahld(form1.spaltennamen(ix)) = end select next next end sub
the method in module1. spaltennamen initilized in form1
here code
public class form1 public spaltennamen() string private sub form1_load(sender object, e eventargs) handles mybase.load dim table new datatable table = skmtabelle.slxadriumdevdataset.mappen dim integer = 0 dim spaltennamen(table.columns.count) string each column datacolumn in table.columns spaltennamen(i) = column.columnname = + 1 next
edit found 1 bug @ form1 class. spaltennamen declared in formloader , in head of class.
nevertheless big bug cant in mappen() method because if take procedur of form1 out, fill spaltennamen , instead fill spaltennamen in code before runtime, works fine...
replace form1
instance of form1
form1
type
.
example
dim f new form1() feld(i) = f.datagridview1.rows(i).cells(1).value.tostring
debug
public class form1 friend spaltennamen() string '= nothing aka no instance created. private sub form1_load(sender object, e eventargs) handles mybase.load '- here got contradiction. ' 1) first create new instance of `datatable`. ' 2) set should have been return value (datatable) of `mappen`, ' it's `sub` , not `function`. dim table new datatable table = skmtabelle.slxadriumdevdataset.mappen ' | ' -------------------------- ' | ' \|/ 'public sub mappen() '- form1 type, not instance. '- work, 1 of following suggestions: ' 1) move code inside `mappen` , place inside method (as shown here) , replace `form1` `me`. ' 2) change method accept instance of form1 `public sub mappen( f form1)`, change `form1` `f` , ' call `mappen(me)`. '- `arr1` instantiated? `forminstance.arr1 = new object(?-1){}` (replace `object` correct data type) '- arrays in vb.net use zero-base indexing. here create `feld` array can hold +1 more item ' `arr1`. dim feld(form1.arr1.length) string integer = 0 form1.arr1.length - 1 '- form1 type, not instance. '- sure `rows(i)` exists? '- sure want second column? cells(1) <- zero-base indexing makes column 2. ' first column have index `0`. feld(i) = form1.datagridview1.rows(i).cells(1).value.tostring ix integer = 0 form1.spaltennamen.length - 1 select case feld(i) '- form1 type, not instance. '- spaltennamen instantiated? `forminstance.spaltennamen = new string(?-1){}` case form1.spaltennamen(ix) '- form1 type, not instance. '- `comboauswahld` instantiated? `forminstance.comboauswahld = new object(?-1){}` (replace `object` correct data type) form1.comboauswahld(form1.spaltennamen(ix)) = end select next next 'end sub dim integer = 0 '- in vb.net, if specify length of array, instantiated. (finally) '- sure should holde +1 more item number of columns? dim spaltennamen(table.columns.count) string each column datacolumn in table.columns spaltennamen(i) = column.columnname = + 1 next end sub end class
Comments
Post a Comment