简体   繁体   English

对象引用的自定义消息未设置为对象的实例

[英]Custom message for Object reference not set to an instance of an object

Hi 你好
We have a huge windows application with poor exception handling. 我们有一个巨大的Windows应用程序,异常处理不佳。 The application throw object reference error from lot of places and the system error message is showing to users as it is using message boxes. 来自许多地方的应用程序抛出对象引用错误,系统错误消息显示给用户,因为它正在使用消息框。

I am looking for a simple solution which can be used to replace this message to something user friendly for the entire application 我正在寻找一个简单的解决方案,可用于将此消息替换为用户友好的整个应用程序

Thanks... 谢谢...

@Anz: Its not good to use Exception handling in every where in code so always keep this in mind and you must know what is the meaning of all different type of Exception. @Anz:在代码中的每个位置都使用异常处理并不好,所以始终牢记这一点,你必须知道所有不同类型的异常的含义。 In your scenario you are getting "object reference Exception" and the main reason of this exception is that you are not checking null while accessing variable like 在您的方案中,您将获得“对象引用异常”,并且此异常的主要原因是您在访问变量时未检查null

Exa_1:- Exa_1: -

DataSet ds; DataSet ds;

now if i acces it as ds.Table.count() it will give Exception, so here we should use 现在,如果我将其作为ds.Table.count()访问它将给出Exception,所以在这里我们应该使用

DataSet ds; DataSet ds;

 If(ds!=null)
 {
   int val = ds.Table.count();
 } 

Exa_2:- Exa_2: -

     string strVariable=txtInput.Text;

     int number = Convert.Int32(strVariable); // here if txtInput.Text is empty them     it will through exception so here we can use

if(!String.IsNullOrEmpty(strVariable)) int number = Convert.Int32(strVariable); if(!String.IsNullOrEmpty(strVariable))int number = Convert.Int32(strVariable);

And if you want to show custom Message in Exception handle then you can create your own Exception Class which will override Exception class then you can throw and catch like: 如果你想在Exception句柄中显示自定义Message,那么你可以创建自己的Exception Class,它将覆盖Exception类,然后你可以抛出并捕获:

public class MyException : Exception
{
    public string customMessage;
    public MyException(string sourceName)
    {
        customMessage = sourceName + " can not be null";
    }
    public MyException()
    {
        customMessage="ObjectReferenceException";
    }        
}

And in code where you are usng try catch use 在代码中你可以尝试使用catch

        try
        {

            throw new MyException("check");
        }
        catch (MyException ex)
        {
            MessageBox.Show(ex.customMessage);
        }

When you catch the exception and display the message box you just need to write a friendly message into the message box. 当您捕获异常并显示消息框时,您只需在消息框中写入友好消息。 I don't think displaying exception messages is a good idea - It could give away information about the structure of your application that a malicious user could use to attack the application. 我不认为显示异常消息是一个好主意 - 它可以提供有关恶意用户可以用来攻击应用程序的应用程序结构的信息。

** Additional ** **额外**

Either way it is a big change. 无论哪种方式,这都是一个很大的变化。 You either override the exception in lots of places, or override the display of the error message in lots of places. 您可以在许多地方覆盖异常,或覆盖许多地方的错误消息显示。 May I suggest that you consolidate the display of the error message in to one place so when you need new error messages you have one place to go, and therefore in future, one place to change if you need to change it. 我可以建议您将错误消息的显示合并到一个地方,这样当您需要新的错误消息时,您还有一个地方可以去,因此将来,如果您需要更改它,可以在一个地方进行更改。

In a WinForms application, you could use something like the technique described here for displaying a user-friendly error message. 在WinForms应用程序中,您可以使用类似于此处描述的技术来显示用户友好的错误消息。

That said, since the only reason for null reference exception is developer error, there is a strong code smell that the app has lots of overall problems. 也就是说,由于null引用异常的唯一原因是开发人员错误,因此应用程序存在很多整体问题。 I would recommend at least to put in a logger in the exception handler that you're going to put in place, so that the errors will not go unnoticed. 我建议至少在你将要放置的异常处理程序中放入一个记录器,这样就不会忽视错误。

You need to catch exception thrown by legacy application and log that exception in some log file and display appropriate user friendly message according to exception occured. 您需要捕获遗留应用程序抛出的异常,并在某些日志文件中记录该异常,并根据发生的异常显示相应的用户友好消息。

For example, instead of showing FileNotFound exception message along with stacktrace, you can just show "Application unable to find the file xyz". 例如,您可以只显示“应用程序无法找到文件xyz”,而不是显示FileNotFound异常消息以及stacktrace。

use Application_Error event in global.asax if it is Asp.Net application 如果它是Asp.Net应用程序,请在global.asax中使用Application_Error事件

 Application_Error

 {
   HttpContext context = HttpContext.Current;

   Exception ex = context.Server.GetLastError();
  //process your exception

    if ( context.IsCustomErrorEnabled )
   {
      context.Server.ClearError();
      context.Server.Transfer( "~/error.aspx" );
   }
 }

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

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