Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. What is object reference not set to an instance of an object?

What is object reference not set to an instance of an object?

Scheduled Pinned Locked Moved Visual Basic
questiondatabasedebugging
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sweehin18
    wrote on last edited by
    #1

    Do While lcp_objReader.Peek() <> -1

                str\_lcp\_Tmp = lcp\_objReader.ReadLine()
    
                str\_lcp\_data(a) = str\_lcp\_Tmp.Split(lcp\_delimiter, StringSplitOptions.RemoveEmptyEntries)
    
    
    
                If str\_lcp\_data(a).Length > 0 Then
    
    
                    For index As Integer = 0 To str\_lcp\_data(a).Length - 1
    
                        Console.Write(str\_lcp\_data(a)(index) + "|")
              
                    Next
    
                    Console.Write(vbCrLf)
                    a += 1
    
                End If
    
            Loop
    

    i would like to print out the row 120, so i put

    Console.Write(str_lcp_data(120)(index) + "|")

    After debug, it gave the message "object reference not set to an instance of an object". How come it become like this? If i want to print the rows after row 120 (from 121 until end of the file), should i initialise a to 120 before do while loop?

    T T 2 Replies Last reply
    0
    • S sweehin18

      Do While lcp_objReader.Peek() <> -1

                  str\_lcp\_Tmp = lcp\_objReader.ReadLine()
      
                  str\_lcp\_data(a) = str\_lcp\_Tmp.Split(lcp\_delimiter, StringSplitOptions.RemoveEmptyEntries)
      
      
      
                  If str\_lcp\_data(a).Length > 0 Then
      
      
                      For index As Integer = 0 To str\_lcp\_data(a).Length - 1
      
                          Console.Write(str\_lcp\_data(a)(index) + "|")
                
                      Next
      
                      Console.Write(vbCrLf)
                      a += 1
      
                  End If
      
              Loop
      

      i would like to print out the row 120, so i put

      Console.Write(str_lcp_data(120)(index) + "|")

      After debug, it gave the message "object reference not set to an instance of an object". How come it become like this? If i want to print the rows after row 120 (from 121 until end of the file), should i initialise a to 120 before do while loop?

      T Offline
      T Offline
      Tom Deketelaere
      wrote on last edited by
      #2

      this usualy accurs when you forget the initialize the object (the new keyword) for example: Dim ll As List(Of String) ll.Add("test") --> will give you're error Dim ll As new List(Of String) ll.Add("test") --> will not give the error so in you're case I think you forgot to initialize the str_lcp_data object

      S 1 Reply Last reply
      0
      • T Tom Deketelaere

        this usualy accurs when you forget the initialize the object (the new keyword) for example: Dim ll As List(Of String) ll.Add("test") --> will give you're error Dim ll As new List(Of String) ll.Add("test") --> will not give the error so in you're case I think you forgot to initialize the str_lcp_data object

        S Offline
        S Offline
        sweehin18
        wrote on last edited by
        #3

        i got initialise str_lcp_data object

        dim str_lcp_data (1000)() as string

        T 1 Reply Last reply
        0
        • S sweehin18

          i got initialise str_lcp_data object

          dim str_lcp_data (1000)() as string

          T Offline
          T Offline
          Tom Deketelaere
          wrote on last edited by
          #4

          that is not initialized (I think correct me if I'm wrong) that is declared to initialize the variable you need to enter data try this (just to see if this is the problem) str_lcp_data(120)="test" and then run and see if you still get the error if not you've found the cause wich is that there was no data to display if you still get the error then I don't really know what is going on

          1 Reply Last reply
          0
          • S sweehin18

            Do While lcp_objReader.Peek() <> -1

                        str\_lcp\_Tmp = lcp\_objReader.ReadLine()
            
                        str\_lcp\_data(a) = str\_lcp\_Tmp.Split(lcp\_delimiter, StringSplitOptions.RemoveEmptyEntries)
            
            
            
                        If str\_lcp\_data(a).Length > 0 Then
            
            
                            For index As Integer = 0 To str\_lcp\_data(a).Length - 1
            
                                Console.Write(str\_lcp\_data(a)(index) + "|")
                      
                            Next
            
                            Console.Write(vbCrLf)
                            a += 1
            
                        End If
            
                    Loop
            

            i would like to print out the row 120, so i put

            Console.Write(str_lcp_data(120)(index) + "|")

            After debug, it gave the message "object reference not set to an instance of an object". How come it become like this? If i want to print the rows after row 120 (from 121 until end of the file), should i initialise a to 120 before do while loop?

            T Offline
            T Offline
            TwoFaced
            wrote on last edited by
            #5

            You've created a jagged array (an array of arrays). When you do this each element points to another array. The array's initially aren't created. I think this is why you get the error. Take this for example

                Dim data(5)() As String
                data(0) = New String() {"Data1", "Data2", "Data3"}
            
                Console.WriteLine(data(0)(0))
            
                ' The next line will throw an error because the 2nd element
                ' doesn't point to an array yet.
                Console.WriteLine(data(1)(0))
            

            What's probably happening is you are only reading x number of rows, so only the first x elements have been initialized properly. Trying to read beyond x will throw an error. You could initialize all the elements first like so:

                For i As Integer = 0 To data.Length - 1
                    data(i) = New String() {}
                Next
            

            Or you can just make sure you don't read beyond the number of lines read.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups