【问题标题】:variable "stat_in" is used before it has been assigned a value. a null exception could result at runtime变量“stat_in”在被赋值之前被使用。运行时可能会导致空异常
【发布时间】:2018-01-20 01:08:22
【问题描述】:
                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”在被赋值之前被使用。运行时可能会导致空异常。 台式机 = Windows 10,笔记本电脑 = Windows,7。 我还在 Windows 10 笔记本电脑上进行了尝试。它也不起作用。 相同版本的 Visual Studio。 Visual Studio 2015 社区。 我在考虑框架?请帮我。我是 vb.net 编码的初学者。

【问题讨论】:

  • 你有一个IfElseIf,所以可能这两个条件都不适用,然后变量保持未初始化状态。要么添加Else... 以分配回退值,要么首先给它一个值。在这种情况下,我猜你只想从Else中的方法中Return
  • 请阅读How to Ask 并使用tour。还要根据需要使用标点符号、句子和段落,以使帖子可读。你应该想让别人帮助你容易

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


【解决方案1】:

出现警告的原因是字符串实际上是引用类型,但它的行为类似于值类型。当你像下面这样声明它时,

Dim stat_in as string

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

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

Dim stat_in As String =""

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

stat_in=""

或默认值,例如

stat_in="IN"

等等

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多