0

I create a setup file of a winform app using VS 2017 installer. When I test this setup file on my PC, the login runs fine. So I run this setup file on other PCs.

However, when I login the app in these PCs, the app say "Object reference not set to an instance of an object", though I am sure that the username and password were correct in the db.

I have seen What is a NullReferenceException, and how do I fix it?, but I do not think it is the null exception, because the installed app did not throw above error in my PC. This error only happens when I install the app in other PCs.

I also try replacing the .exe and .config files in folder of the installed app in other PCs with the same files of the app in my PC (which work well), and the app runs ok. But then I restart those PCs, and try to login the app, the same error happens.

This is the code for login. I believe that I have checked for the null exception properly. Am I right?

private void btnLogin_Click(object sender, EventArgs e)
    {
        try
        {
            var info = UsersBiz.Login(txtUserName.Text.Trim(), txtPassWord.Text);

            if (info != null && info.user.Id > 0)
            {
                Constants.USERINFO = info;

                this.Hide();
                var frm = new frmMain();
                frm.Show();

                if (ckbRemember.Checked)
                {
                    ManagementInventory.Properties.Settings.Default.User = txtUserName.Text;
                    ManagementInventory.Properties.Settings.Default.Pass = txtPassWord.Text;
                    ManagementInventory.Properties.Settings.Default.Save();
                }
            }
            else
            {
                MessageBox.Show("UserName or Password not correct!");
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
B.Green
  • 67
  • 7
  • _"but I do not think it is the null exception"_ - Short of deliberately throwing this exception yourself, there's only ever one cause of it: something is null. In your case, this means that one of these is null: `UsersBiz` (if not static), `txtUserName`, `txtUserName.Text`, `txtPassword`, `info`, `info.user`, `chkbRemember`, `ManagementInventory`, `ManagementInventory.Properties`, or `ManagementInventory.Settings`, `ManagementInventory.Properties.Settings.Default`. – ProgrammingLlama May 26 '20 at 04:41
  • Since it's WinForms, I expect the UI components have had values assigned to them, and that the text in those components is, at worst, an empty string. For me, the likely possibilities in the code above are that `info.user` is null, or that something in the chain of `ManagementInventory.Properties.Settings.Default` is `null`. I wonder if you perhaps have a problem with your settings file (not present or not in the correct directory). The file you're looking for would be the same as your program filename (e.g. `app.exe`) but with `.config` appended: `app.exe.config` – ProgrammingLlama May 26 '20 at 04:42
  • Such problems are the reason why most programs perform some kind of logging. As a quick solution, you might replace `MessageBox.Show(ex.Message)` by `MessageBox.Show(ex.ToString())`; then you can see the exact location where the exception is thrown including the call stack. – Klaus Gütter May 26 '20 at 05:31
  • I tried `MessageBox.Show(ex.ToString())` and it pointed to the `GetConnectionString` method (to connect db) inside `UserBiz.Login` method. I will see if there is a problem with `GetConnectionString` method or `ConnectionString` in `app.config`. Strange thing is if I copy `app.exe` and `app.exe.config` from app folder in my computer (which work well) and paste to the compute that app not worked, the app will run well, until I restart that computer (the problem returns). Thank you for the suggestion. – B.Green May 26 '20 at 09:27
  • [See this answer for some ideas](https://stackoverflow.com/a/25005864/129130). You must be lacking a runtime or something like that. Maybe see the section *"Generic Tricks?"* onwards. – Stein Åsmul May 26 '20 at 10:44

1 Answers1

1

The most easy way would be to use visual studio debugger on the computer where the program runs. If there is no visual studio on that computer, consider using remote debugging. See how to start debugging on a remote computer

If for security reasons remote debugging is out of the question, consider to log the exception, especially the stack trace. If you know where it occurs, simply surround it with a try-catch

try
{
    // Here are the statements that cause your ArgumentNullException
}
catch (ArgumentNullException exc)
{
    // log the exception
}

If your program has no logging facility, consider adding something like NLOG. Another possibility is to append it to a text file.

void WriteExceptionToTextFile(Exception exc, string fileName)
{
    using (var writer = File.AppendText(fileName))
    {
        writer.WriteLine("Exception {0}", exc.GetType());
        write.WriteLine("Stack trace" + exc.StackTrace);
        ... // etc
    }
}

If you don't know where the exception occurs, you can't try-catch it. In that case consider to catch the unhandled exception and log it:

See: catching unhandled exception

In your forms program is a file program.cs which contains the main:

public static void Main()
{
    AppDomain currentDomain = AppDomain.CurrentDomain;
    currentDomain.UnhandledException += OnUnhandledException;
    ...
}

static void OnUnhandledException(object sender, UnhandledExceptionEventArgs args)
{
    Exception undhandledException = (Exception) args.ExceptionObject;
    // TODO log the unhandled exception

}

Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116