none
NullReferenceException only at Startup RRS feed

  • Question

  • Hello everyone,

    I have a program that runs as startup because I add it to the startup folder with: Enviroment.GetFolderPath(Enviroment.SpecialFolder.Startup);

    Now, this application runs well when you click on it in its folder, but when the computer starts up, I get this error:

    ************* Exception Text **************
    System.NullReferenceException: Object reference not set to an instance of an object.
       at SlashA004.Form1.exeOrdersA() in C:\Users\John\Desktop\John\SCAR\OperationMonkeyRaider-v2\visual studio\real\SlashA004\SlashA004\Form1.cs:line 391
       at SlashA004.Form1.runCode() in C:\Users\John\Desktop\John\SCAR\OperationMonkeyRaider-v2\visual studio\real\SlashA004\SlashA004\Form1.cs:line 276
       at SlashA004.Form1.reloadCodeTimer() in C:\Users\John\Desktop\John\SCAR\OperationMonkeyRaider-v2\visual studio\real\SlashA004\SlashA004\Form1.cs:line 290
       at SlashA004.Form1.waitTimer1_Tick(Object sender, EventArgs e) in C:\Users\John\Desktop\John\SCAR\OperationMonkeyRaider-v2\visual studio\real\SlashA004\SlashA004\Form1.cs:line 306
       at System.Windows.Forms.Timer.OnTick(EventArgs e)
       at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

    I think it is unable to reference some files (but that is just a hunch). Any ideas on how to fix this?

    Monday, January 15, 2018 2:41 AM

Answers

  • I believe the objects my code wants to reference are in the program files, which are not in the startup folder. When adding my application to the startup folder, should I:

    All executables like DLL(s) that a programname.exe needs should be in the same folder as the programname.exe itself.

    or include a shortcut to the application at startup

    The shortcut is a pointer to the location of the programname.exe, and the shortcut should be the only thing in the startup folder.

    Normally, one installs the programname.exe and all related DLL(s) the programname.exe needs in the Program Files directory.

    The issue you have is not that something cannot be found, which would be an entirely different error message other than there is not an object in memory at the time the code is referencing the object. during code execution.

    Your problem seems to be in the exeOrdersA() method where code is executing and referencing an object that is not there in memory.

     

    • Marked as answer by Wilkyrl Monday, January 15, 2018 6:41 PM
    Monday, January 15, 2018 5:27 PM

All replies

  • Check the instructions above the line 392 of the file Form1.cs, and also other areas mentioned in the error messages, and show some details.

    Monday, January 15, 2018 6:18 AM
  •  Object reference not set to an instance of an object.

    I think it is unable to reference some files (but that is just a hunch). Any ideas on how to fix this?

    class vs object vs instanced

    A class is a blueprint for an object. A class is instanced into an object. When class is instanced into an object, usually by using the new keyword, it lives in memory.

    The link talks about the principles of OOP, and Java and .NET are OO language platforms.

    http://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    Basically, your code is trying to reference/access an object that is not there in memory for whatever reasons that you'll have to debug your code with the debugger to find out why.

    Monday, January 15, 2018 8:15 AM
  • DA924x, thank you for this insight.

    I believe the objects my code wants to reference are in the program files, which are not in the startup folder. When adding my application to the startup folder, should I:

    • include the entire folder for the project with all packages, built files, etc.
    • only include the application and have it reference the necessary files
    • or include a shortcut to the application at startup

    I am not sure which is the proper procedure when including an application at startup.

    Monday, January 15, 2018 2:31 PM
  • Viorel_:

    This is line 392 and its surrounding code:

     private void button1_Click(object sender, EventArgs e)  //execute order a by button click
            {
                Console.WriteLine("button pressed");
                exeOrdersA();
    
    
            }
    So, using this as an example, is it the object sender that is missing? Or somethig else?

    Monday, January 15, 2018 2:34 PM
  • Somehow you seems to run your program from your Visual Studio Project Folder which you have located in your desktop. 

    Install your program and dependencies in the Program Files Folder

    We don't know what OS you are using but probably the user folder is not available at startup


    Success Cor



    Monday, January 15, 2018 3:43 PM
  • I believe the objects my code wants to reference are in the program files, which are not in the startup folder. When adding my application to the startup folder, should I:

    All executables like DLL(s) that a programname.exe needs should be in the same folder as the programname.exe itself.

    or include a shortcut to the application at startup

    The shortcut is a pointer to the location of the programname.exe, and the shortcut should be the only thing in the startup folder.

    Normally, one installs the programname.exe and all related DLL(s) the programname.exe needs in the Program Files directory.

    The issue you have is not that something cannot be found, which would be an entirely different error message other than there is not an object in memory at the time the code is referencing the object. during code execution.

    Your problem seems to be in the exeOrdersA() method where code is executing and referencing an object that is not there in memory.

     

    • Marked as answer by Wilkyrl Monday, January 15, 2018 6:41 PM
    Monday, January 15, 2018 5:27 PM
  • Hello,

    I'm surprised that this works

    Enviroment.GetFolderPath(Enviroment.SpecialFolder.Startup)

    Should it not be

    Environment.GetFolderPath(Environment.SpecialFolder.Startup)


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, January 15, 2018 6:13 PM
    Moderator