Arrays 使用数组和字典在循环中启动循环时抛出system.nullReferenceException
我有另一个系统。nullReferenceexcpetion。这是代码。在标记行抛出异常。如果需要,我可以给你使用的数组和字典的日化Arrays 使用数组和字典在循环中启动循环时抛出system.nullReferenceException,arrays,vb.net,loops,exception,nullreferenceexception,Arrays,Vb.net,Loops,Exception,Nullreferenceexception,我有另一个系统。nullReferenceexcpetion。这是代码。在标记行抛出异常。如果需要,我可以给你使用的数组和字典的日化 public sub mappen() Dim feld(Form1.arr1.Length) As String For i As Integer = 0 To Form1.arr1.Length - 1 feld(i) = Form1.DataGridView1.Rows(i).Cells(1).Value.T
public sub mappen()
Dim feld(Form1.arr1.Length) As String
For i As Integer = 0 To Form1.arr1.Length - 1
feld(i) = Form1.DataGridView1.Rows(i).Cells(1).Value.ToString
***For ix As Integer = 0 To Form1.Spaltennamen.Length - 1***
Select Case feld(i)
Case Form1.Spaltennamen(ix)
Form1.ComboauswahlD(Form1.Spaltennamen(ix)) = i
End Select
Next
Next
End Sub
该方法在模块1中。spaltennamen的初始格式为1
这是密码
Public Class Form1
Public Spaltennamen() As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim table As New DataTable
table = SkMTabelle.SLXADRIUMDEVDataSet.mappen
Dim i As Integer = 0
Dim Spaltennamen(table.Columns.Count) As String
For Each column As DataColumn In table.Columns
Spaltennamen(i) = column.ColumnName
i = i + 1
Next
编辑我在form1类中发现了一个bug。Spaltennamen在formloader和类的头部声明
然而,最大的错误不可能出现在mappen()方法中,因为如果将form1的过程去掉,在运行之前我在代码中填充spaltennamen,而不是填充spaltennamen,那么一切都可以正常运行…用
form1
的实例替换form1
,因为form1
是类型
示例
Dim f As New Form1()
feld(i) = f.DataGridView1.Rows(i).Cells(1).Value.ToString
调试
Public Class Form1
Friend Spaltennamen() As String '= Nothing aka no instance created.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'- Here you got a contradiction.
' 1) First you create a new instance of a `DataTable`.
' 2) Then you set it to what should have been a return value (DataTable) of `mappen`,
' but it's a `Sub` and not a `Function`.
Dim table As New DataTable
table = SkMTabelle.SLXADRIUMDEVDataSet.mappen
' |
' --------------------------
' |
' \|/
'Public Sub mappen()
'- Form1 is a type, not an instance.
'- For this to work, you could do one of the following suggestions:
' 1) Move all code inside `mappen` and place it inside this method (as shown here) and replace `Form1` with `Me`.
' 2) Change your method to accept an instance of Form1 `Public Sub mappen( f As Form1)`, change `Form1` to `f` and then
' just call `mappen(Me)`.
'- Is `arr1` instantiated? `forminstance.arr1 = New Object(?-1){}` (Replace `Object` with correct data type)
'- Arrays in vb.net use zero-base indexing. Here you create a `feld` array which can hold +1 more item
' than `arr1`.
Dim feld(Form1.arr1.Length) As String
For i As Integer = 0 To Form1.arr1.Length - 1
'- Form1 is a type, not an instance.
'- Are you sure `Rows(i)` exists?
'- Are you sure you want the second column? Cells(1) <- Zero-base indexing makes this column 2.
' The first column have index `0`.
feld(i) = Form1.DataGridView1.Rows(i).Cells(1).Value.ToString
For ix As Integer = 0 To Form1.Spaltennamen.Length - 1
Select Case feld(i)
'- Form1 is a type, not an instance.
'- Is Spaltennamen instantiated? `forminstance.Spaltennamen = New String(?-1){}`
Case Form1.Spaltennamen(ix)
'- Form1 is a type, not an instance.
'- Is `ComboauswahlD` instantiated? `forminstance.ComboauswahlD = New Object(?-1){}` (Replace `Object` with correct data type)
Form1.ComboauswahlD(Form1.Spaltennamen(ix)) = i
End Select
Next
Next
'End Sub
Dim i As Integer = 0
'- In vb.net, if you specify a length of an array, it is instantiated. (Finally)
'- Are you sure it should holde +1 more item than the number of columns?
Dim Spaltennamen(table.Columns.Count) As String
For Each column As DataColumn In table.Columns
Spaltennamen(i) = column.ColumnName
i = i + 1
Next
End Sub
End Class
公共类表单1
Friend Spaltennamen()作为字符串“=没有任何内容,也没有创建实例。
私有子表单1_Load(发送方作为对象,e作为事件参数)处理MyBase.Load
-这里有个矛盾。
“1)首先创建“DataTable”的新实例。
“2)然后将其设置为“mappen”的返回值(DataTable),
但它是一个“Sub”而不是一个“Function”。
Dim表作为新数据表
表=SkMTabelle.SLXADRIUMDEVDataSet.mappen
' |
' --------------------------
' |
' \|/
'公共子映射栏()
'-Form1是一种类型,而不是实例。
“-为此,您可以执行以下建议之一:
'1)将所有代码移到'mappen'中,并将其放在此方法中(如图所示),然后将'Form1'替换为'Me'。
“2)更改您的方法以接受Form1`Public Sub mappen(f作为Form1)`的实例,将`Form1`更改为`f`,然后
“只要打电话给‘mappen(我)’”。
'-是否实例化了“arr1”`forminstance.arr1=新对象(?-1){}`(用正确的数据类型替换`Object`)
'-vb.net中的数组使用零基索引。在这里,您可以创建一个'feld'数组,该数组可以容纳+1个以上的项目
'而不是'arr1'。
尺寸字段(格式1.arr1.Length)作为字符串
对于i作为整数=0到Form1.arr1.Length-1
'-Form1是一种类型,而不是实例。
“-你确定“行(i)”存在吗?
“-确实要第二列吗?单元格(1)什么是Form1.Spaltenname?
它是如何初始化的,何时初始化的?那么Form1
是模块中的一个变量吗?在引发异常的行上放置一个断点,然后将鼠标悬停在每个标识符上,您将发现哪个标识符为空。那么Form1
是模块中的一个变量吗?否。Spaltenname是已在表格1中本地化。我添加了code@user3360700检查我的编辑。除非arr1
,DataGridView1
和spaltennames
被声明为Shared
,否则您将需要Form1
的实例。我检查了共享声明。它不起作用。我得到一个异常,模块1子。。无法使用变量。在公众面前,我不明白。当我初始化表单1的一个新实例时,我在feld(i)=f.DataGridView1.Rows(i).Cells(1).Value行得到一个“System.ArgumentOutOfRangeException”。ToString@user3360700不要让别人分享。很抱歉,您的代码有太多错误,我不知道从哪里开始。我建议您将严格设置为并将显式设置为(这一直是建议的):此外,如果将table=SkMTabelle.SLXADRIUMDEVDataSet.mappen
替换为Public Sub-mappen()
中的所有内容,则更容易描述调用堆栈。