变量" stat_in"在被赋值之前使用。在运行时可能会导致null异常

时间:2018-01-17 16:29:18

标签: vb.net visual-studio if-statement

                If timeCounter = 1 And stat = "ACTIVE" Then
                    stat_in = "IN"
                    Using SQLConnection As New MySqlConnection(ServerString)
                        Using command As New MySqlCommand()
                            With command
                                .CommandText = "INSERT into time_table(id,student_id,tag,date,time,status) VALUES(@id,@student_id,@tag,@date,@time,@status); UPDATE rfid_stud SET timeCounter = 2 WHERE tag = '" & txtReceived.Text & "'"
                                .Connection = SQLConnection
                                .CommandType = CommandType.Text
                                .Parameters.AddWithValue("@id", idnum)
                                .Parameters.AddWithValue("@student_id", txtStudNo.Text)
                                .Parameters.AddWithValue("@tag", txtReceived.Text)
                                .Parameters.AddWithValue("@date", txtDate.Text)
                                .Parameters.AddWithValue("@time", txtTime.Text)
                                .Parameters.AddWithValue("@status", stat_in)
                            End With
                            Try
                                SQLConnection.Open()
                                command.ExecuteNonQuery()
                            Catch ex As Exception
                            Finally
                                SQLConnection.Close()
                            End Try
                        End Using
                    End Using
                ElseIf timeCounter > 1 And stat = "ACTIVE" Then
                    stat_in = "OUT"
                    Using SQLConnection As New MySqlConnection(ServerString)
                        Using command As New MySqlCommand()
                            With command
                                .CommandText = "INSERT into time_table(id,student_id,tag,date,time,status) VALUES(@id,@student_id,@tag,@date,@time,@status); UPDATE rfid_stud SET timeCounter = 1 WHERE tag = '" & txtReceived.Text & "'"
                                .Connection = SQLConnection
                                .CommandType = CommandType.Text
                                .Parameters.AddWithValue("@id", idnum)
                                .Parameters.AddWithValue("@student_id", txtStudNo.Text)
                                .Parameters.AddWithValue("@tag", txtReceived.Text)
                                .Parameters.AddWithValue("@date", txtDate.Text)
                                .Parameters.AddWithValue("@time", txtTime.Text)
                                .Parameters.AddWithValue("@status", stat_in)
                            End With
                            Try
                                SQLConnection.Open()
                                command.ExecuteNonQuery()
                            Catch ex As Exception
                            Finally
                                SQLConnection.Close()
                            End Try

                        End Using
                    End Using
                Else
                    MsgBox("RFID Card is INACTIVE! Please Contact System Administrator for assisstance.", vbExclamation, "Warning!")
                End If

                For i = 0 To (grid.Rows.Count - 1)
                    stringfix = grid.Rows.Item(i).Cells(0).Value
                    string1 = Microsoft.VisualBasic.Mid(stringfix, 1, 12)
                    string2 = Microsoft.VisualBasic.Mid(stringfix, 2, 12)
                    If string1 = string2 Then
                        newtag = False
                        Exit For
                    Else
                        newtag = True
                    End If
                Next
                If newtag = True And stat = "ACTIVE" Then
                    Dim dr As Integer
                    dr = grid.Rows.Add()
                    grid.Rows.Item(dr).Cells.Item(0).Value = txtReceived.Text
                    grid.Rows.Item(dr).Cells(1).Value = txtStudNo.Text
                    grid.Rows.Item(dr).Cells(2).Value = txtFname.Text
                    grid.Rows.Item(dr).Cells(3).Value = txtLname.Text
                    grid.Rows.Item(dr).Cells.Item(4).Value = txtDate.Text + " at " + txtTime.Text


                    If stat_in = "IN" Then
                        grid.Rows.Item(dr).Cells(5).Style.BackColor = Color.Green
                        grid.Rows.Item(dr).Cells(5).Style.ForeColor = Color.White
                        grid.Rows.Item(dr).Cells.Item(5).Value = stat_in
                    ElseIf stat_in = "OUT" Then
                        grid.Rows.Item(dr).Cells(5).Style.BackColor = Color.Red
                        grid.Rows.Item(dr).Cells(5).Style.ForeColor = Color.White
                        grid.Rows.Item(dr).Cells.Item(5).Value = stat_in
                    End If

                End If
  

我在"上有警告;如果stat_in = IN"。   主要警告是" stat_in"   相同的代码在我的电脑上运行没有警告,但当我将我的系统转移到我的笔记本电脑时,它显示2警告。   该程序完全在我的电脑上运行。   警告是   警告BC42104变量' stat_in'在被赋值之前使用。空引用异常可能在运行时产生,而另一个异常是变量" stat_in"在被赋值之前使用。在运行时可能会导致null异常。   Desktop = windows 10,Laptop = windows,7。   我也试过Windows 10笔记本电脑。它不起作用。   相同版本的Visual Studio。 Visual Studio 2015社区。   我在考虑框架?请帮我。我是编码vb.net的初学者。

1 个答案:

答案 0 :(得分:0)

您收到警告的原因是因为字符串实际上是引用类型,但它的行为类似于值类型。当您声明如下时,

Dim stat_in as string

它仅将stat_in定义为String类型。与其他引用类型String类型一样,没有默认值,因此您需要先设置一个值,然后才能与任何内容进行比较。它们甚至没有分配空字符串。

避免这种情况的简单方法是在声明stat_in时使用类似

的内容
Dim stat_in As String =""

或者您可以在问题中提供的代码顶部指定一个值,例如

stat_in=""

或默认值,例如

stat_in="IN"