Results 1 to 11 of 11

Thread: [RESOLVED] Unexpected Error

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    113

    Resolved [RESOLVED] Unexpected Error

    Ok i made a program it worked fine. I changed Net Framework to 3.5, then back to version 4 (i was testing something)

    It now works fine.

    I run the program from vb.net in debug mode and everythign works fine.

    I clean the build,

    I then build the project.

    I then run the built exe but then it shows an error. The error point to this code:

    Code:
        Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
            If Not My.Computer.FileSystem.DirectoryExists(wdir) Then
                My.Computer.FileSystem.CreateDirectory(wdir)
            End If
    
            If Not My.Settings.prevsearch Is Nothing Then
                If My.Settings.prevsearch <> "" Then
                    txt_g_url.Text = My.Settings.prevsearch
                    MsgBox(wdir)
                End If
            End If
    
            For Each item As String In My.Settings.prevurl
                If Not txturl.Items.Contains(item) Then
                    txturl.Items.Add(item)
                End If
            Next
        End Sub
    I cant figure out why this is. The main point is it works in debug but not after its built.


    heres the exception error:
    Code:
    ************** Exception Text **************
    System.NullReferenceException: Object reference not set to an instance of an object.
       at RAF_WEB.Form1.Form1_Load(Object sender, EventArgs e) in H:\Files\VB NET\Projects\RAF_WEB\RAF_WEB\Form1.vb:line 2311
       at System.EventHandler.Invoke(Object sender, EventArgs e)
       at System.Windows.Forms.Form.OnLoad(EventArgs e)
       at System.Windows.Forms.Form.OnCreateControl()
       at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       at System.Windows.Forms.Control.CreateControl()
       at System.Windows.Forms.Control.WmShowWindow(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.Form.WmShowWindow(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    Last edited by rafhelp; Jun 28th, 2012 at 07:36 AM.

  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    21

    Re: Unexpected Error

    The issue is likely related to your development environment. I believe by default your development environment expects YOU to handle certain exceptions, Null Reference being one of them.

    To fix, in VS 2008 (Similar for other versions of VS) go to:

    (On the toolbar) Debug > Exceptions (Or CTRL + ALT + E)

    This brings up a list of all exceptions and how they're handled.

    Navigate the Tree List to:

    Common Runtime Exceptions > System > System.NullReferenceException

    Check the checkbox that says "THROWN". Click OK and run the program via Debug Mode. The program should now throw that same exception and you'll see exactly what's going on.

    Enjoy

    P.S. This happened to me 2 days ago ;-).
    Last edited by nycdev; Jun 28th, 2012 at 11:37 AM.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    113

    Re: Unexpected Error

    Quote Originally Posted by nycdev View Post
    The issue is likely related to your development environment. I believe by default your development environment expects YOU to handle certain exceptions, Null Reference being one of them.

    To fix, in VS 2008 (Similar for other versions of VS) go to:

    (On the toolbar) Debug > Exceptions (Or CTRL + ALT + E)

    This brings up a list of all exceptions and how they're handled.

    Navigate the Tree List to:

    Common Runtime Exceptions > System > System.NullReferenceException

    Check the checkbox that says "THROWN". Click OK and run the program via Debug Mode. The program should now throw that same exception and you'll see exactly what's going on.

    Enjoy

    P.S. This happened to me 2 days ago ;-).
    tried that, also selected THROWN for all system exceptions. still same thing. works in debug, no errors, after i build, double click on exe in the bin/release folder and the error is shown, if i press continue it works fine...

    Code:
    Object reference not set to an instance of an object

  4. #4
    Junior Member
    Join Date
    Jun 2012
    Posts
    21

    Re: Unexpected Error

    Hm, strange. Does the exe in the debug file have a modified date of just when you built the project? Maybe it's not being updated?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    113

    Re: Unexpected Error

    its being updated, i checked by deleting the old one

  6. #6
    Junior Member
    Join Date
    Jun 2012
    Posts
    21

    Re: Unexpected Error

    Since you have so few lines of code, at this point I would just suggest stepping through it.

    Btw not sure if you're aware, but you can run the program via the EXE and then still debug it using VS by click Tools > Attach to Process > [YourFile.exe].

    I would do that and step through the Form1_Load carefully and examine each item and look for a "Nothing" somewhere.

    For example maybe My.Settings is "Nothing".

    I also notice you have
    vb Code:
    1. If Not My.Settings.prevsearch Is Nothing Then
    2. ...
    3. End If
    4.  
    5.         For Each item As String In My.Settings.prevurl
    6. ...
    7. Next

    You're checking if prevurl is nothing in the first block but not the second, so if there's a chance it's nothing you should not do the loop without first checking the same "if not my.settings.prevurl is nothing"

  7. #7
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Unexpected Error

    1. Which line exactly is throwing the exception
    2. Have you looked at the settings file (user.config)


    Use the following to get the user.config path

    Code:
    Imports System.Configuration
    Module FolderLocation
        ''' <summary>
        ''' Get the user's config file
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks>
        ''' add a reference to System.Configuration.dll
        ''' </remarks>
        Public Function UserConfigFolder() As String
            Return ConfigurationManager.OpenExeConfiguration(
                ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath.Replace("\user.config", "")
        End Function
    End Module
    Uage
    Code:
    MsgBox(UserConfigFolder)

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    113

    Re: Unexpected Error

    Quote Originally Posted by nycdev View Post
    Since you have so few lines of code, at this point I would just suggest stepping through it.

    Btw not sure if you're aware, but you can run the program via the EXE and then still debug it using VS by click Tools > Attach to Process > [YourFile.exe].

    I would do that and step through the Form1_Load carefully and examine each item and look for a "Nothing" somewhere.

    For example maybe My.Settings is "Nothing".

    I also notice you have
    vb Code:
    1. If Not My.Settings.prevsearch Is Nothing Then
    2. ...
    3. End If
    4.  
    5.         For Each item As String In My.Settings.prevurl
    6. ...
    7. Next

    You're checking if prevurl is nothing in the first block but not the second, so if there's a chance it's nothing you should not do the loop without first checking the same "if not my.settings.prevurl is nothing"
    Done...

    The last thing you said worked. Logically does not make sense as it worked before.. is there any way to modify settings so that it does not throw an error if the my.computer.settings.setting is empty.

    for example i create a collection of string setting in the program which is always starts as an empty collection but if you try to add to it it always throws an error, so you HAVE to check if the setting is "Nothing"

  9. #9
    Junior Member
    Join Date
    Jun 2012
    Posts
    21

    Re: [RESOLVED] Unexpected Error

    I'm not familiar with the My.Settings but can';t you do one check at the beginning of the program and if it's empty instantiate it to an empty collection?

    Something like:
    vb Code:
    1. If My.Settings is nothing Then
    2. My.Settings = New SomeCollection(of SomeType)
    3. End If

  10. #10
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Unexpected Error

    Quote Originally Posted by rafhelp View Post
    Done...

    The last thing you said worked. Logically does not make sense as it worked before.. is there any way to modify settings so that it does not throw an error if the my.computer.settings.setting is empty.

    for example i create a collection of string setting in the program which is always starts as an empty collection but if you try to add to it it always throws an error, so you HAVE to check if the setting is "Nothing"
    Look at the following thread
    http://www.vbforums.com/showthread.php?t=580949

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Dec 2009
    Posts
    113

    Re: [RESOLVED] Unexpected Error

    Quote Originally Posted by nycdev View Post
    I'm not familiar with the My.Settings but can';t you do one check at the beginning of the program and if it's empty instantiate it to an empty collection?

    Something like:
    vb Code:
    1. If My.Settings is nothing Then
    2. My.Settings = New SomeCollection(of SomeType)
    3. End If
    thats what i normally do

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width