Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi i have a text file and an array of labels named
arraylabels
when i close my program each label text is saved as a line in the text file,i want to get the text back to each label when i reopen the program from the saved text file,Note:my labels are put in
flowlayoutpanel


What I have tried:

I tried many codes and approaches this is one of them
<pre>Dim lines As String() = File.ReadAllLines(del.filename)
        Dim v As Integer = 0
        For v = 0 To UBound(labelarray)
            If lines.Length <> 0 And lines(v) <> "" Then
                labelarray(v).Text = lines(v)
            Else
                Exit For

            End If
        Next

this one is returning null refernce at line
labelarrays(v).text=lines(v)
Posted
Updated 8-Jan-17 7:11am

1 solution

In order to get an null reference exception, the object of the left of the dot operator must be null. In this line, the only use of dot is to access the Text property of you control in order to set the string. So clearly, at least one of the elements of the labelarray does not contain anything.
Use the debugger to find out the value of v when the error occurs, and check the relevant array element to find out why it contains null.
When you know which element(s) is it, you can start looking back in your code to find out why it's null.

My guess would be that v is zero and you haven't loaded any controls into the array at all, just assigned the space for them - but without the code a guess is all it is.
 
Share this answer
 
Comments
Member 12939116 8-Jan-17 13:21pm    
@Originalgriff thank you for your reply,the labelarray contains 70 labels so its not null
and the debugger indicates v=0 as expected to be,so
labelarray(0)=label1
i find that makes sense,anyway i just want to save these labels text so when i reopen my program it shows back that's it,i already saved them now i want them back as lines(0) is label1 text and so on,if you need further information about the code i will provide it a help would be appreciated
OriginalGriff 9-Jan-17 2:46am    
If v is zero, and you get a null reference error, then the debugger will show you that labelarray(v) contains Nothing - not a Label.
So when you try to access the Text property, you get an error.
So where do you create labelarray, and where do you fill it with New Label instances? Because creating an array of Label objects does not create the Labels themselves, any more than creating a carpark automatically fills each parking bay with a new car!

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