[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 345
  • Last Modified:

Object reference not set to an instance of an object

This is the Unhandled error I am getting on a Timer event.  It is only running a listview refresh so the code isn't that complicated.  

System.NullReferenceException: Object reference not set to an instance of an object.
   at Mysqlsample.Form1.Refresh_Current_Listview()
   at Mysqlsample.Form1.tmrDelay_Tick(Object sender, EventArgs e)
   at System.Windows.Forms.Timer.OnTick(EventArgs e)
   at System.Windows.Forms.Timer.Callback(IntPtr hWnd, Int32 msg, IntPtr idEvent, IntPtr dwTime)

I put in an error handler 'On Error GoTo InitializeError' with a msgbox to let me know if it was still there rather than an Exit Sub at this stage but even removing all the code within the timer event it still goes to the error handler.  I suppose I could allow it to exit sub but why is there an error there.  I notice all the timers go to an error handler with or without code!!
If anyone can help I would appreciate it.

Hollymax
0
Hollymax
Asked:
Hollymax
  • 9
  • 8
1 Solution
 
AlexFMCommented:
Please show your code.
0
 
HollymaxAuthor Commented:
AlexFM this is the timer code, along with the code for one of the LoadDataListview, they are all the same just refresh the data shown in 3 seperate listviews.  Once you get an error as I have listed you can click continue and it is OK again, and even though this timer routine happens each 1 minute interval you may only see the error a couple of times a day.  I'm not having any trouble with LoadDataListview codes otherwise I would see their error messages.

Private Sub tmrRefresh_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrRefresh.Tick
               '\\ If in Maintenance Mode then do nothing
        If chkMaintenance.CheckState = CheckState.Checked Then
            Exit Sub
        Else
            '\ ReLoad Info back into Current Listview
            Call LoadDataListview()
            '\ Only Show Messages from Current Logged in User
            Call Filter_Current_User()
            '\\ Update the Completed Listview
            Call LoadDataListview2()
            '\\ Update the Users Listview
            Call LoadDataListview3()
            '\\ Count to see if any Message Number changes
            txtMessageNumber.Text = CountListViewItems(lvwCurrent)
        End If
    End Sub

Public Function LoadDataListview()
        On Error GoTo InitializeError
        Dim rs As New ADODB.Recordset
        rs = New ADODB.Recordset
        rs.Open("select * from CurrentMsgs", conn)
        'rs.Open("select * from Completed", conn)
        If rs.EOF Then
            lvwCurrent.Items.Clear()
        Else
            rs.MoveFirst()
            lvwCurrent.View = View.Details
            lvwCurrent.Items.Clear()
            Do While Not rs.EOF
                With lvwCurrent.Items.Add(CStr(rs.Fields("ID").Value))
                    .SubItems.Add(CStr(rs.Fields("AllDates").Value))
                    .SubItems.Add(CStr(rs.Fields("Subject").Value))
                    .SubItems.Add(CStr(rs.Fields("Message").Value))
                    .SubItems.Add(CStr(rs.Fields("Sender").Value))
                    .SubItems.Add(CStr(rs.Fields("Recipient").Value))
                End With
                rs.MoveNext()
            Loop
        End If
        rs.Close()
        If txtMessageNumber.Text = "" Then
            txtMessageNumber.Text = CountListViewItems(lvwCurrent)
        Else

        End If
        Exit Function

InitializeError:
        MsgBox("Error with Current Messages", vbExclamation + vbDefaultButton1, "ERROR")
    End Function
0
 
AlexFMCommented:
This doesn't match call stack: tmrDelay_Tick, Refresh_Current_Listview. Where are these functions?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
HollymaxAuthor Commented:
Sorry AlexFM you are right, and I'm glad you pointed that out I've been checking the wrong area...  It should be as listed below, the codes are similar in what they acheive.  Refresh_Current_Listview still calls the LoadDataListview but removes text from text boxes used to enter data into the database and makes sure the form is the correct size.  I have the delay timer routine just to make sure it gives time for the data to be saved or read.  It is used in quite a few areas and mostly doesn't give me any problems

Private Sub tmrDelay_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrDelay.Tick
 
        If chkMaintenance.CheckState = CheckState.Checked Then
            Exit Sub
        Else

            Dim iDl As Integer
            Count = txtDelay.Text
            For iDl = 1 To 3  ' allow time to communicate with database.mdb
                txtDelay.Text = Count + 1
            Next iDl
            If Count = 3 Then
                '\ ReLoad Info back into Current Listview & Remove Text Entries to Textboxes
                Call Refresh_Current_Listview()
                '\ Only Show Messages from Current Logged in User
                Call Filter_Current_User()
                '\ ReLoad Info back into Completed Listview
                Call LoadDataListview2()
                '\ Re-enable Listview Refreshing
                tmrRefresh.Enabled = True
                tmrRefresh.Interval = Val(txtTimerRefreshInterval.Text)
                '\ Check for new Messages
                tmrMessNum.Enabled = True
                tmrMessNum.Interval = 1000

                If txtMessageNumber.Text = "" Then
                    txtMessageNumber.Text = CountListViewItems(lvwCurrent)
                    txtMessageNumberOLD.Text = txtMessageNumber.Text
                Else
                    txtMessageNumber.Text = CountListViewItems(lvwCurrent)
                    txtMessageNumberOLD.Text = txtMessageNumber.Text

                End If
                tmrDelay.Enabled = False
                txtDelay.Text = 1
            End If
        End If
 
    End Sub


Private Sub Refresh_Current_Listview()
        txtID.Text = ""
        txtDateTime.Text = ""
        txtSubject.Text = ""
        txtMessage.Text = ""
        txtSender.Text = ""
        txtRecipient.Text = ""
        txtCompleted.Text = ""
        '\ ReLoad Info back into Current Listview
        Call LoadDataListview()
            If chkMaintenance.CheckState = CheckState.Checked Then
            Form1.ActiveForm.Width = 800
            Form1.ActiveForm.Height = 664
        Else
            Form1.ActiveForm.Width = 660
            Form1.ActiveForm.Height = 340
        End If

    End Sub
0
 
AlexFMCommented:
Can you break to debug mode and see what line throws exception? Looking at Refresh_Current_Listview function I can guess that ActiveForm can return null. But you need to debug this. Remove all error handling code before.

>> removing all the code within the timer event it still goes to the error handler.

I don't think that this is possible, how can Refresh_Current_Listview appear in the stack in this case? This is some problem with build or you remove code from other timer function.
0
 
HollymaxAuthor Commented:
As for the timer code I tried it on another computer and just putting in the lines;

Private Sub tmrDelay_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrDelay.Tick
        On Error GoTo InitializeErrorDelay
                 

InitializeErrorDelay:
        MsgBox("Timer Delay error")
     End Sub

every tick gives me the meassage 'Timer Delay error' so I've put it down to an error in the software.

I've only seen the exception error after it has been installed on another computer accessing the database across the network.  From memory it pointed to line which was the line for the Call Call LoadDataListview() but only in the tmrRefresh_Tick code ( I might be wrong there though).  Unfortunately this error doesn't occur very often  I have the last error in text form if that's any help.

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
   at Mysqlsample.Form1.Refresh_Current_Listview()
   at Mysqlsample.Form1.tmrDelay_Tick(Object sender, EventArgs e)
   at System.Windows.Forms.Timer.OnTick(EventArgs e)
   at System.Windows.Forms.Timer.Callback(IntPtr hWnd, Int32 msg, IntPtr idEvent, IntPtr dwTime)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 1.0.5000.0
    Win32 Version: 1.1.4322.573
    CodeBase: file:///c:/windows/microsoft.net/framework/v1.1.4322/mscorlib.dll
----------------------------------------
MyMessenger
    Assembly Version: 1.0.2264.28767
    Win32 Version: 1.0.2264.28767
    CodeBase: file:///C:/Program%20Files/Amajer%20Park%20Computers/Local%20Messenger/MyMessenger.exe
----------------------------------------
System.Windows.Forms
    Assembly Version: 1.0.5000.0
    Win32 Version: 1.1.4322.573
    CodeBase: file:///c:/windows/assembly/gac/system.windows.forms/1.0.5000.0__b77a5c561934e089/system.windows.forms.dll
----------------------------------------
System
    Assembly Version: 1.0.5000.0
    Win32 Version: 1.1.4322.573
    CodeBase: file:///c:/windows/assembly/gac/system/1.0.5000.0__b77a5c561934e089/system.dll
----------------------------------------
System.Drawing
    Assembly Version: 1.0.5000.0
    Win32 Version: 1.1.4322.573
    CodeBase: file:///c:/windows/assembly/gac/system.drawing/1.0.5000.0__b03f5f7f11d50a3a/system.drawing.dll
----------------------------------------
System.Xml
    Assembly Version: 1.0.5000.0
    Win32 Version: 1.1.4322.573
    CodeBase: file:///c:/windows/assembly/gac/system.xml/1.0.5000.0__b77a5c561934e089/system.xml.dll
----------------------------------------
Microsoft.VisualBasic
    Assembly Version: 7.0.5000.0
    Win32 Version: 7.10.3052.4
    CodeBase: file:///c:/windows/assembly/gac/microsoft.visualbasic/7.0.5000.0__b03f5f7f11d50a3a/microsoft.visualbasic.dll
----------------------------------------
ADODB
    Assembly Version: 7.0.3300.0
    Win32 Version: 7.10.3077
    CodeBase: file:///C:/Program%20Files/Amajer%20Park%20Computers/Local%20Messenger/ADODB.DLL
----------------------------------------

************** JIT Debugging **************
To enable just in time (JIT) debugging, the config file for this
application or machine (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the machine
rather than being handled by this dialog.
0
 
AlexFMCommented:
Private Sub tmrDelay_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrDelay.Tick
        On Error GoTo InitializeErrorDelay
                 
InitializeErrorDelay:
        MsgBox("Timer Delay error")
 End Sub

>> every tick gives me the meassage 'Timer Delay error'

Of course! Add Exit Sub line:

Private Sub tmrDelay_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrDelay.Tick
        On Error GoTo InitializeErrorDelay

        Exit Sub               ' !!                  
InitializeErrorDelay:
        MsgBox("Timer Delay error")
 End Sub
0
 
HollymaxAuthor Commented:
I actually put in code 'On Error GoTo InitializeErrorDelay' in the 'tmrDelay' but I only had the 'Exit Sub '  after InitializeErrorDelay: Could I have the 'Exit Sub'  after InitializeErrorDelay: as well, or should I have Resume Next ect..  I'm really just trying to avoid the 'System.NullReferenceException: Object reference not set to an instance of an object' message coming up and having to click continue to go past it, but I guess I don't understand why it is coming up and putting in the 'On Error GoTo InitializeErrorDelay' I thought might overcome it.
What do you think?
0
 
AlexFMCommented:
Private Sub tmrDelay_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrDelay.Tick
        On Error GoTo InitializeErrorDelay
 
        ' optionally: any code goes here

        Exit Sub
InitializeErrorDelay:
End Sub

This is possible, but this only hides error. I think it is better to debug and fix it.
0
 
HollymaxAuthor Commented:
Yes you are right.  You mentioned before "Can you break to debug mode and see what line throws exception?", what do you mean by 'break to debug mode ' just selecting F5 to run the code??
0
 
AlexFMCommented:
When program runs and there is unhandled exception, it shows exception dialog which allows to stop the program, ignore exception or break into debugger. Select "Break into debugger" option, and Visual Studio starts, opens source code and breaks on the line which caused exception.
Running under debugger (F5) is also possible, in this programs breaks immidiately in the line which causes exception.
0
 
HollymaxAuthor Commented:
That's my problem.  I haven't seen it when I run it on this computer & it is only when my software is installed on another computer that I have this Microsoft .Net Framework error,  and it tells me that the error is in the Form1.Refresh_Current_Listview area, yet that peice of code runs in quite a few other areas without problems, and of course it doesn't happen very often.
0
 
AlexFMCommented:
In this case you need to rewrite function by such way that is tests any reference before using it, for example:

Form1.ActiveForm.Width = 800

Shousd be replaced with:

if not Form1.ActiveForm is nothing then
    Form1.ActiveForm.Width = 800
end if

You need also to add some kind of logging to this function, which allows to see what line throws exception. Try to run this program in Release configuration, maybe this will help.
0
 
HollymaxAuthor Commented:
Thank you I will try this.  I saw sometimes that the form shows as the wrong size that's why I put in the code to show the form as a certain size, because it resizes to 3 different sizes depending which point it is at.  What's the difference between debug & release?
Again I thankyou for the help so far.
0
 
AlexFMCommented:
Release build contains optimizations and code generated by compiler sometimes doesn't match source code exactly, this makes debugging difficult. Debug build works exactly as written in source code.
0
 
HollymaxAuthor Commented:
OK thankyou, I should get a chance tomorrow to look at the code and make some changes you have suggested.  The worst part of this is the error isn't frequent enough and is probably linked to a certain chain of events that may be hard to track down, unless it is having a problem accessing across the network, or perhaps I have duplicate code somewhere doing the same thing but not always at the same time??
0
 
HollymaxAuthor Commented:
Sorry AlexFM I haven't been been back earlier.  Thank you for your suggestions.  I decided to go with putting in an error handler at this stage.  I haven't been able to show the same error on my network and the same error is still happening on the network where it is installed.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now