none
未将对象引用设置到对象的实例。 RRS feed

  • 问题

  • Imports System.Data
    Imports System.Data.OleDb
    
    Public Class Form1
      Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=公交信息数据库.accdb;Jet OLEDB:Database Password=MyDbPassword")
      Dim command As New OleDbCommand("Select * From 公交信息", conn)
    
      Dim a(), b(), c(), d() As String
      Dim l, z As Boolean
    
      Private Sub w1(ByVal x As String)
        Dim t() As String
        Dim j, k, r As Integer
        Dim temp2, temp3 As String
        Dim V As String
        t = Split(x, "——")
        For k = 0 To UBound(t)
          If t(k) = TextBox1.Text Or t(k) = TextBox2.Text Then
          Else
            For j = 0 To UBound(b)
              temp2 = InStrRev(b(j), t(k) & "——", InStr(b(j), TextBox2.Text))
              If temp2 <> 0 Then
                For r = 0 To k
                  temp3 = temp3 & t(r) & "——"
                Next
                V = Val(temp2) + Len(t(k)) + 1
                temp3 = temp3 & Mid(b(j), V, InStr(b(j), TextBox2.Text) - Val(V) + 1)
                ListBox6.Items.Add(temp3)
                If ListBox6.Items.Count = 0 Then
                  ListBox6.Items.Add(temp3) : My.Application.DoEvents()
                Else
                  For r = 0 To ListBox6.Items.Count - 1
                    If ListBox6.Items(r) = temp3 Then Exit For
                    If ListBox6.Items.Count = r And ListBox6.Items(r) <> temp3 Then _
                      ListBox6.Items.Add(temp3) : My.Application.DoEvents() : l = True
                  Next
                End If
                temp3 = ""
              End If
            Next
          End If
        Next
      End Sub
    
      Private Sub w2(ByVal x As String)
        Dim t() As String
        Dim j, k, r As Integer
        Dim temp2, temp3 As String
        Dim V As String
        t = Split(x, "——")
        For k = 0 To UBound(t)
          If t(k) = TextBox1.Text Or t(k) = TextBox2.Text Then
          Else
            For j = 0 To UBound(d)
              temp2 = InStrRev(d(j), t(k) & "——", InStr(d(j), TextBox2.Text))
              If temp2 <> 0 Then
                For r = 0 To k
                  temp3 = temp3 & t(r) & "——"
                Next
                V = Val(temp2) + Len(t(k)) + 1
                temp3 = temp3 & Mid(d(j), V, InStr(d(j), TextBox2.Text) - Val(V) + 1)
                ListBox7.Items.Add(temp3)
                If ListBox7.Items.Count = 0 Then
                  ListBox7.Items.Add(temp3) : My.Application.DoEvents()
                Else
                  For r = 0 To ListBox7.Items.Count - 1
                    If ListBox7.Items(r) = temp3 Then Exit For
                    If ListBox7.Items.Count = r And ListBox7.Items(r) <> temp3 Then _
                      ListBox7.Items.Add(temp3) : My.Application.DoEvents() : z = True
                  Next
                End If
                temp3 = ""
              End If
            Next
          End If
        Next
      End Sub
    
      Private Sub s1()
        Dim m, n As Integer
        For n = 0 To ListBox6.Items.Count
          w1(a(n))
        Next
        Do
          For n = 0 To ListBox6.Items.Count
            w1(ListBox6.Items(n))
            If l = True Then m = 1
          Next
          If m <> 1 Then Exit Do
          m = 2
          l = False
        Loop
        Dim t2 As Threading.Thread
        t2 = New Threading.Thread(AddressOf s2)
        t2.Start()
        If MsgBox("路线1已搜索完成,是否继续搜索路线2?", vbYesNo + MsgBoxStyle.Question) = MsgBoxResult.No _
          Then t2.Abort() : Button3.Enabled = False
      End Sub
    
      Private Sub s2()
        Dim m, n As Integer
        For n = 0 To ListBox7.Items.Count
          w2(c(n))
        Next
        Do
          For n = 0 To ListBox7.Items.Count
            w2(ListBox7.Items(n))
            If z = True Then m = 1
          Next
          If m <> 1 Then Exit Do
          m = 2
          z = False
        Loop
        MsgBox("路线1已搜索完成" & vbCrLf & "换乘搜索完成!")
      End Sub
    
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        conn.Open() '打开数据库连接
        Dim reader As OleDbDataReader = command.ExecuteReader() '执行SQL语句,返回OleDbDataReader 对象
        Do While reader.Read() '读取一条数据
          ComboBox1.Items.Add(reader("线路"))
        Loop
        reader.Close() '关闭OleDbDataReader
      End Sub
    
      Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ListBox3.Items.Clear() : ListBox4.Items.Clear()
        If RadioButton1.Checked = True Then
          Dim reader As OleDbDataReader = command.ExecuteReader() '执行SQL语句,返回OleDbDataReader 对象
          Dim temp, temp1 As Integer
          Dim m, n As Integer
          Do While reader.Read() '读取一条数据
            temp = InStr(reader("路线1"), TextBox1.Text)
            temp1 = InStr(reader("路线1"), TextBox2.Text)
            If temp <> 0 And temp1 <> 0 Then
              ListBox3.Items.Add(reader("线路"))
              n = n + 1
              ReDim Preserve b(n)
              b(n) = reader("路线1")
              m = m + 1
              ReDim Preserve a(m)
              a(m) = reader("路线1")
            ElseIf temp = 0 And temp1 <> 0 Then
              n = n + 1
              ReDim Preserve b(n)
              b(n) = reader("路线1")
            ElseIf temp <> 0 And temp1 = 0 Then
              m = m + 1
              ReDim Preserve a(m)
              a(m) = reader("路线1")
            End If
          Loop
          reader.Close()
    
          Dim reader2 As OleDbDataReader = command.ExecuteReader() '执行SQL语句,返回OleDbDataReader 对象
          Dim tmp, tmp1 As Integer
          Dim o, p As Integer
          Do While reader2.Read() '读取一条数据
            tmp = InStr(reader2("路线2"), TextBox1.Text)
            tmp1 = InStr(reader2("路线2"), TextBox2.Text)
            If tmp <> 0 And tmp1 <> 0 Then
              ListBox4.Items.Add(reader2("线路"))
              p = p + 1
              ReDim Preserve d(p)
              d(p) = reader2("路线2")
              o = o + 1
              ReDim Preserve c(o)
              c(o) = reader2("路线2")
            ElseIf tmp = 0 And tmp1 <> 0 Then
              p = p + 1
              ReDim Preserve d(p)
              d(p) = reader2("路线2")
            ElseIf tmp <> 0 And tmp1 = 0 Then
              o = o + 1
              ReDim Preserve c(o)
              c(o) = reader2("路线2")
            End If
          Loop
          reader2.Close()
          If ListBox3.Items.Count = 0 And ListBox4.Items.Count = 0 Then
            ListBox6.Items.Clear() : ListBox7.Items.Clear()
            Dim t1 As Threading.Thread
            t1 = New Threading.Thread(AddressOf s1)
            t1.Start()
            If MsgBox("无法直接到达。是否进行换乘搜索?" & vbCrLf _
                 & "换乘搜索时间可能较长,您可以随时按下停止按钮停止操作", _
                 vbYesNo + MsgBoxStyle.Question) = MsgBoxResult.No Then
              t1.Abort()
              ListBox3.Visible = True : ListBox4.Visible = True
              ListBox6.Visible = False : ListBox7.Visible = False
            Else
              ListBox3.Visible = False : ListBox4.Visible = False
              ListBox6.Visible = True : ListBox7.Visible = True
              Button3.Enabled = True
            End If
          End If
        End If
      End Sub
    End Class
    
    

    在过程s1中 w1(a(n))出现此问题,怎么解决?
    2011年8月19日 9:59

答案

  • Hi

    因为你的w1是一个子函数,而错误行在该行而不是其内部,所以,应该是参数出错。

    我看到你有定义a为字符串数组,但似乎没有将其实例化,所以a应该指向空,所以你不能直接使用。

    为了简单的验证这一点, 你可以在该行设置一个断点,当执行到此行时,看一下a是不是为空。

    找到了问题点,怎么解决就看你怎么实例化它了,主要就是赋点值。

    Best regards,


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年8月23日 8:32
    版主

全部回复

  • 跟踪一下,a(n)=?具体出错代码?
    http://feiyun0112.cnblogs.com/
    2011年8月19日 10:02
    版主
  • a(n)等于什么不知道,因为a(n)的值是根据文本框变的。反正a(n)为字符串。

    这是我复制下来的信息:

    未处理 System.NullReferenceException
      Message=未将对象引用设置到对象的实例。
      Source=WindowsApplication1
      StackTrace:
           在 WindowsApplication1.Form1.s1() 位置 C:\Users\HOME\Documents\Visual Studio 2010\Projects\WindowsApplication1\WindowsApplication1\Form1.vb:行号 82
           在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           在 System.Threading.ExecutionContext.runTryCode(Object userData)
           在 System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
           在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
           在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
           在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           在 System.Threading.ThreadHelper.ThreadStart()
      InnerException:

    2011年8月19日 10:06
  • Hi

    因为你的w1是一个子函数,而错误行在该行而不是其内部,所以,应该是参数出错。

    我看到你有定义a为字符串数组,但似乎没有将其实例化,所以a应该指向空,所以你不能直接使用。

    为了简单的验证这一点, 你可以在该行设置一个断点,当执行到此行时,看一下a是不是为空。

    找到了问题点,怎么解决就看你怎么实例化它了,主要就是赋点值。

    Best regards,


    Mike Feng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年8月23日 8:32
    版主