VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 307,165 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,546 people online right now. Registration is fast and FREE... Join Now!




Array help?

 

Array help?, Reading from files/arrays

Matty919

10 May, 2009 - 06:06 AM
Post #1

New D.I.C Head
*

Joined: 10 May, 2009
Posts: 20


My Contributions
I'm trying to create a program which reads a text file into an array.

CODE

Imports System.IO
Module Module1

    Sub Main()
        Dim SR As New StreamReader("C:\hello.txt")
        Dim lines() As String
        Dim line As String
        Dim count As Integer = 0
        Do Until SR.EndOfStream
            line = SR.ReadLine()
            lines(count) = line
            count += 1
        Loop
        Console.WriteLine(lines(0))
        Console.ReadLine()

    End Sub

End Module


This code spits out an error "Object reference not set to an instance of an object." for the
CODE
lines(count) = line
line but Im not too sure why. I can fix this by declaring:

CODE
Dim lines() As String = {"**", "**", "**", "**", "**", "**", "**"}


But that's just silly so how can I fix this? Lines isn't being assigned a null value it's being assigned the first line of text in the file which has text in it so I'm confused.

Thanks.

User is offlineProfile CardPM
+Quote Post

 
Reply to this topicStart new topic
Replies(1 - 5)

Apache

RE: Array Help?

10 May, 2009 - 08:45 AM
Post #2

New D.I.C Head
*

Joined: 3 Nov, 2008
Posts: 45



Thanked: 10 times
My Contributions
Try this, I've just neatened it up a bit and set the Array to grow as needed.

CODE
    Sub Main()
        Dim myFile As New System.IO.StreamReader("C:\hello.txt")
        Dim strTextArray() As String = {}
        Dim intIndex As Integer = 0

        Do Until myFile.EndOfStream = True
            ReDim Preserve strTextArray(intIndex)
            strTextArray(intIndex) = myFile.ReadLine()
            intIndex += 1
        Loop

        For i As Integer = 0 To UBound(strTextArray)
            Console.WriteLine(strTextArray(i))
        Next

        Console.ReadKey()
    End Sub


This post has been edited by Apache: 10 May, 2009 - 08:59 AM
User is offlineProfile CardPM
+Quote Post

Apache

RE: Array Help?

10 May, 2009 - 08:58 AM
Post #3

New D.I.C Head
*

Joined: 3 Nov, 2008
Posts: 45



Thanked: 10 times
My Contributions
You could also do...

CODE
Sub Main()
        Dim strTextArray() As String = System.IO.File.ReadAllLines("C:\hello.txt")

        For i As Integer = 0 To UBound(strTextArray)
            Console.WriteLine(strTextArray(i))
        Next

        Console.ReadKey()
    End Sub

User is offlineProfile CardPM
+Quote Post

fixo

RE: Array Help?

10 May, 2009 - 11:12 AM
Post #4

D.I.C Head
**

Joined: 10 May, 2009
Posts: 78



Thanked: 5 times
My Contributions
Try this one too smile.gif


CODE
    Sub test()
        Dim count As Integer = 0
        Try
            Dim fr As String = "C:\hello.txt"
            Dim SR As StreamReader = File.OpenText(fr)
            Dim lines As String() = New String(10000) 'dummy size value
            Dim line As String = Nothing
            While (line = SR.ReadLine()) <> Nothing
                lines(count) = line
                Console.WriteLine(lines(count).ToString())
                count += 1
            End While
            Array.Resize(Of String)(lines, count)
            SR.Close()
            Console.ReadLine()
            Dim fw As String = "C:\hello2.txt"
            Dim SW As StreamWriter = New StreamWriter(fw)
            
            Dim i As Integer = 0
            While i < lines.Length
                SW.WriteLine(lines(i))
                System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
            End While

            SW.Flush()
            SW.Close()
            Console.ReadLine()
        Catch e As Exception
            Console.WriteLine("{0}", e.StackTrace + "" & Microsoft.VisualBasic.Chr(10) & "" + count.ToString())
            Console.ReadLine()
        End Try

    End Sub


~'J'~
User is offlineProfile CardPM
+Quote Post

Matty919

RE: Array Help?

10 May, 2009 - 01:27 PM
Post #5

New D.I.C Head
*

Joined: 10 May, 2009
Posts: 20


My Contributions
Thankyou - just what I wanted. icon_up.gif
User is offlineProfile CardPM
+Quote Post

fixo

RE: Array Help?

11 May, 2009 - 10:17 AM
Post #6

D.I.C Head
**

Joined: 10 May, 2009
Posts: 78



Thanked: 5 times
My Contributions
QUOTE(Matty919 @ 10 May, 2009 - 01:27 PM) *

Thankyou - just what I wanted. icon_up.gif

You're welcome
Cheers smile.gif

~'J'~
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 06:21PM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month