Object Referance is not set to an instance of an object

  • Thread starter Thread starter Omarado
  • Start date Start date
O

Omarado

Guest
So having created some code and test it out on a test application , I seemed to get the error of "Object referance is not set to an instance of an object" error but was able to supress it using a Try .. Catch block and would still get what I wanted done. However, that's not the case for my main application.

That's the complete code for my test application which seems to work well:

Imports System.IO
Public Class Form1
Dim CurDir As String = My.Application.Info.DirectoryPath
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim thelist As String
Dim eachfile As String
Dim lab As Label
Dim di As DirectoryInfo = My.Computer.FileSystem.GetDirectoryInfo(CurDir)
For Each fi As FileInfo In di.GetFiles()
eachfile = (System.IO.Path.GetFileNameWithoutExtension(fi.FullName))
thelist = thelist & (System.IO.Path.GetFileNameWithoutExtension(fi.FullName)) & vbCrLf
lab = Me.Controls(eachfile)
Try
lab.ForeColor = Color.Green
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Next fi
MsgBox(thelist)
Dim theline As Integer = 1 - 1
MsgBox(File.ReadAllLines(CurDir & "\testthing.txt").ElementAt(theline).ToString)

End Sub
End Class



However, writing a code basically almost the same in my actual application didn't work and even when supressing the same error , the code won't be applied this time and I don't know why. Main application code:

Public Sub FileOnlinePPlecPK()
Dim islec As String
Download_Dropbox("SOME URL", CurDir & "\PPlecPK.txt")
For inputlec As Integer = 0 To 15 Step 1
Dim lecnum As Integer = inputlec + 1
If lecnum = 15 Then
islec = File.ReadAllLines(CurDir & "\PPlecPK.txt").ElementAt(inputlec).ToString
If islec = "NO" Then
Dim btnname As String = "PPmidPK"
Dim thebtn As LinkLabel
thebtn = Me.Controls(btnname)
thebtn.Enabled = False
Else
Dim btnname As String = "PPmidPK"
Dim thebtn As LinkLabel
thebtn = Me.Controls(btnname)
thebtn.Enabled = True
If System.IO.File.Exists(My.Settings.DataFolder & "\PPlec\PPmidPK.pdf") = True Then
thebtn.LinkColor = Color.Green
Else
thebtn.LinkColor = Color.Red
End If

End If
ElseIf lecnum = 16 Then
islec = File.ReadAllLines(CurDir & "\PPlecPK.txt").ElementAt(inputlec).ToString
If islec = "NO" Then
Dim btnname As String = "PPfinalPK"
Dim thebtn As LinkLabel
thebtn = Me.Controls(btnname)
thebtn.Enabled = False
Else
Dim btnname As String = "PPfinalPK"
Dim thebtn As LinkLabel
thebtn = Me.Controls(btnname)
thebtn.Enabled = True
If System.IO.File.Exists(My.Settings.DataFolder & "\PPlec\PPfinalPK.pdf") = True Then
thebtn.LinkColor = Color.Green
Else
thebtn.LinkColor = Color.Red
End If

End If
Else
islec = File.ReadAllLines(CurDir & "\PPlecPK.txt").ElementAt(inputlec).ToString
If islec = "NO" Then
Dim btnname As String = "PPlec" & lecnum & "PK"
Dim thebtn As LinkLabel
thebtn = Me.Controls(btnname)
thebtn.Enabled = False
Else
Dim btnname As String = "PPlec" & lecnum & "PK"
Dim thebtn As LinkLabel
thebtn = Me.Controls(btnname)
thebtn.Enabled = True
If System.IO.File.Exists(My.Settings.DataFolder & "\PPlec\PPlec" & lecnum & "PK.pdf") = True Then
thebtn.LinkColor = Color.Green
Else
thebtn.LinkColor = Color.Red
End If

End If
End If
Next
End Sub



The problem here occurs once I try to change

thebtn
Enabled state which is my variable linklabel.
The code is supposed to run a loop through 0 to 15. each time it reads a specific line of a text file and based whether the text says "NO" or not then is decided whether to enable the button or not. The problem in the code happens when it tries to enable the linklabel named "PPlec1PK" which is an already placed linklabel. I have refered to the same linklabel here:

thebtn = Me.Controls(btnname)
. However , when it tries to change the Enabled state of that linklabel the error happens. I thought this error happens when I try to refer to a object with its value set to "Nothing" ? but how is that possible when I just assigned it a value ? I don't get how to eliminate this error ...

Continue reading...
 
Back
Top