Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written a code to read a text box character by character and copy the characters into another array. As soon as the space character occurs the process should stop. the Program is giving argument null exception at runtime. Any solutions.

Here is the code.

VB
Private Sub file_open_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles file_open.Click
Dim objreader As New System.IO.StreamReader(file_name.Text)
    TextBox1.Text = objreader.ReadLine

    TextBox1.Text = TextBox1.Text & objreader.ReadLine & vbCrLf

    Dim myArray() As Char
    Dim myArray2() As Char

    myArray = Me.TextBox1.Text.ToCharArray
    For i As Integer = 1 To 70
        If myArray(i) <> " " Then

        Else
            Array.Copy(myArray, myArray2, i)     'exception here

        End If
    Next

End Sub
Posted
Updated 24-Sep-11 5:01am
v2

you haven't initialized myArray2 so it's null just like your exception tells you.

However the code you posted makes no sense, all you're trying to do is copying all the elements in myArray to myArray2 except any space(s) that might be at the end of myArray. And you're doing it again and again..
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Sep-11 3:42am    
Correct, my 5.
--SA
Simon Bang Terkildsen 25-Sep-11 9:00am    
Thank you
Try using a list of chars List(Of Char)
And once ready call ToArray method. In this way you do not need to deal with array sizes.

Something like

VB
Dim myArray1 As List(Of Char) = TextBox1.Text.ToCharArray
Dim myArray2 As List(Of Char) = New List(Of Char)

For Each c As Char In myArray1
    If c <> " " Then
        myArray2.Add(c)
    Else
        Exit For
    End If
Next



Cheers
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 25-Sep-11 3:43am    
Makes sense, my 5.
--SA
Mario Majčica 25-Sep-11 7:02am    
But to Razanust it doesn't! :) Love people who ask and then disappear! :)
Simon Bang Terkildsen 25-Sep-11 9:05am    
you got my 5 too
About the OP not accepting, voting or commenting thats just the way some inquirers behave, once they post they expect a satisfying answer and if they don't get one, just posts the question on some other forum, or even reposts on the same forum.
Before answering a question I usually look up the previous quesions the OP has posted, if he isn't engaged in this questions then I don't put more than a minute or two into my answer, if I even bother answering :)
Mario Majčica 25-Sep-11 10:31am    
Internet made people lazy and rude!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900