简体   繁体   English

如何修复仅在某些计算机上发生的 NullReferenceException

[英]How to fix NullReferenceException occurring only on some computers

I create a setup file of a winform app using VS 2017 installer.我使用 VS 2017 安装程序创建了一个 winform 应用程序的安装文件。 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.但是,当我在这些 PC 中登录应用程序时,应用程序会说“对象引用未设置为对象的实例”,尽管我确信数据库中的用户名和密码是正确的。

I have seen What is a NullReferenceException, and how do I fix it?我已经看到什么是 NullReferenceException,我该如何解决? , but I do not think it is the null exception, because the installed app did not throw above error in my PC. ,但我不认为这是 null 异常,因为安装的应用程序没有在我的 PC 中引发上述错误。 This error only happens when I install the app in other PCs.仅当我在其他 PC 上安装该应用程序时才会发生此错误。

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.我还尝试将其他 PC 中已安装应用程序文件夹中的 .exe 和 .config 文件替换为我 PC 中该应用程序的相同文件(运行良好),并且该应用程序运行正常。 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.我相信我已经正确检查了 null 异常。 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);
        }

The most easy way would be to use visual studio debugger on the computer where the program runs.最简单的方法是在程序运行的计算机上使用 Visual Studio 调试器。 If there is no visual studio on that computer, consider using remote debugging.如果该计算机上没有 Visual Studio,请考虑使用远程调试。 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-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.如果您的程序没有日志记录功能,请考虑添加类似 NLOG 的内容。 Another possibility is to append it to a text file.另一种可能性是将 append 转换为文本文件。

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.如果你不知道异常发生在哪里,你不能 try-catch 它。 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:在您的 forms 程序中是一个文件 program.cs,其中包含主要内容:

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM