5 Replies - 256 Views - Last Post: 09 February 2019 - 03:52 PM Rate Topic: -----

#1 Omarado   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 91
  • Joined: 22-September 15

Object referance is not set to an instant of an object

Posted 09 February 2019 - 01:34 AM

This Topic is based of the one HERE

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 ...

This post has been edited by Omarado: 09 February 2019 - 01:37 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Object referance is not set to an instant of an object

#2 Omarado   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 91
  • Joined: 22-September 15

Re: Object referance is not set to an instant of an object

Posted 09 February 2019 - 03:01 PM

so anyone know how to help?!
Was This Post Helpful? 0
  • +
  • -

#3 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1533
  • View blog
  • Posts: 3,861
  • Joined: 01-February 13

Re: Object referance is not set to an instant of an object

Posted 09 February 2019 - 03:24 PM

Seems odd that the control on the form is not found if you actually have the correct name for the button used in the line below, and the LinkLabel is actually a direct child of the form.
thebtn = Me.Controls(btnname)


If the LinkLabels are actually in another container control like a Panel, GroupBox, FlowLayoutPanel, or other, then you need to search that container control's, controls. For example, if they are in a Panel named Panel1, then you would need to do it like shown below. Also, you should be casting the Control that is found to a LinkLabel type, also shown here.
Dim thebtn As LinkLabel = CType(Panel1.Controls("linklabel1"), LinkLabel) 'search Panel1 controls for 'LinkLabel1'


If this does not solve the problem, then all I can say is that you need to debug the code to see the name of the LinkLabel that it does not seem to be able to find. Double/Triple check to make sure the name is spelled the same as the LinkLabel control that is on the Form. For example, use a simple If Then statement to see when thebtn variable is Nothing, then check the name.
        Dim btnname As String = "PPlec" & lecnum & "PK"
        Dim thebtn As LinkLabel = CType(Me.Controls(btnname), LinkLabel)
        If thebtn Is Nothing Then
            MessageBox.Show(btnname, "LinkLabel Not Found...")
        End If



Those are the only two reasons that the LinkLabel would not be found using that code, either the name is not spelled right or the LinkLabel is actually a child control of another container control on the form.

PS - It is always better to use If Then statements to check if things are correct before continuing instead of using Try Catch blocks. Try catch blocks are good for things like trying to open or overwrite files because, the file may be in use by other applications and you can not use an If Then statement to test for something like that. 8)
Was This Post Helpful? 1
  • +
  • -

#4 Omarado   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 91
  • Joined: 22-September 15

Re: Object referance is not set to an instant of an object

Posted 09 February 2019 - 03:37 PM

View PostIronRazer, on 10 February 2019 - 12:24 AM, said:

Seems odd that the control on the form is not found if you actually have the correct name for the button used in the line below, and the LinkLabel is actually a direct child of the form.
thebtn = Me.Controls(btnname)


If the LinkLabels are actually in another container control like a Panel, GroupBox, FlowLayoutPanel, or other, then you need to search that container control's, controls. For example, if they are in a Panel named Panel1, then you would need to do it like shown below. Also, you should be casting the Control that is found to a LinkLabel type, also shown here.
Dim thebtn As LinkLabel = CType(Panel1.Controls("linklabel1"), LinkLabel) 'search Panel1 controls for 'LinkLabel1'


If this does not solve the problem, then all I can say is that you need to debug the code to see the name of the LinkLabel that it does not seem to be able to find. Double/Triple check to make sure the name is spelled the same as the LinkLabel control that is on the Form. For example, use a simple If Then statement to see when thebtn variable is Nothing, then check the name.
        Dim btnname As String = "PPlec" & lecnum & "PK"
        Dim thebtn As LinkLabel = CType(Me.Controls(btnname), LinkLabel)
        If thebtn Is Nothing Then
            MessageBox.Show(btnname, "LinkLabel Not Found...")
        End If



Those are the only two reasons that the LinkLabel would not be found using that code, either the name is not spelled right or the LinkLabel is actually a child control of another container control on the form.

PS - It is always better to use If Then statements to check if things are correct before continuing instead of using Try Catch blocks. Try catch blocks are good for things like trying to open or overwrite files because, the file may be in use by other applications and you can not use an If Then statement to test for something like that. 8)


Do you know how much I love you right now? omg! you are right. I did have the linklabels inside a tab in a TabControl! thus making them not controls of the form itself! I tried the code with specifying the TabControl but it didn't work until I even specified the name of the specific tab that linklabel exists in and now it works!

omg IronRazor I remember you since back in 2015 when you were always the one who helped me with my problems and 4 years later , still you manage to do so! I don't think I would have ever though of that trick there!
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14854
  • View blog
  • Posts: 59,272
  • Joined: 12-June 08

Re: Object referance is not set to an instant of an object

Posted 09 February 2019 - 03:51 PM

Quote

, 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

Side note - do not suppress errors with a try catch. That is not good programming.
Was This Post Helpful? 1
  • +
  • -

#6 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1533
  • View blog
  • Posts: 3,861
  • Joined: 01-February 13

Re: Object referance is not set to an instant of an object

Posted 09 February 2019 - 03:52 PM

Sometimes it is the simple things that can be overlook the easiest. I remember helping you in the past and, I'm glad I could help again. 8)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1