random()中的System.nullreferenceexception问题; [英] System.nullreferenceexception issue in random();

查看:80
本文介绍了random()中的System.nullreferenceexception问题;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我试图生成随机数并将其从a级传递给b级会话



A类

  public   static   void  SendPwd( string 电子邮件)
{
using var client = new SmtpClient())
{
使用 var message = new MailMessage(EmailFrom,Email))
{
Random rnd = new Random();
var random = rnd.Next();
if (random!= null
{
系统。 Web.HttpContext.Current.Session [ random] = random;
}
message.Subject = 请验证您的帐户;
message.Body = < html>< head>< meta content = \text / HTML; charset = utf-8 \/>< / head>< body>< p>亲爱的 +电子邮件+
,< / p>< p>请注意文本框中的随机数和类型< / p> + < p>您的随机数是< / p> + random
+ < / a>< / p>< div>最好的问候,< / div>< div>团队< / div>< ; p>请勿转发
+ 此电子邮件。验证链接是私有的。 < / p>< /体>< / HTML>中;
message.IsBodyHtml = true ;
client.EnableSsl = true ;
client.Send(message);
};
};
}



B级

<前lang =C#> var password = con.tblEmployees.Where(a = > a.Emp_Email == user.EmailId).FirstOrDefault();
password.Emp_TempPwd =( int )HttpContext.Current.Session [ 随机];
con.SaveChanges();
return success ;



我这样做时遇到了这个错误



发生了'System.NullReferenceException'类型的异常在ServerControl.dll中但未在用户代码中处理







我尝试过什么:



我在谷歌搜索过但找不到解决方案所以请帮我解决这个问题

提前感谢

解决方案

这是我们遇到的最常见的问题之一,也是我们最不能回答的问题,但你最多能够回答你自己。



让我解释一下错误的含义:你试图使用变量,属性或方法返回值,但它包含null - 这意味着变量中没有类的实例。

它是有点像口袋:你的衬衫里有一个口袋,用来握笔。如果你进入口袋并发现那里没有笔,你就不能在一张纸上签名 - 如果你尝试的话,你会得到非常有趣的外观!空口袋给你一个空值(这里没有笔!)所以你不能做任何你检索笔通常做的事情。它为什么空?这就是问题 - 可能是你今天早上离开家时忘了拿起你的笔,或者你昨晚把它拿到昨天的衬衫口袋里时可能会把笔留下来。



我们无法分辨,因为我们不在那里,更重要的是,我们甚至看不到你的衬衫,更不用说口袋里的东西了!



回到计算机,你做了同样的事情,不知何故 - 我们看不到你的代码,更不用说运行它了,找不到包含null的东西。

但是你可以 - 而Visual Studio将在这里帮助你。在调试器中运行您的程序,当它失败时,VS将向您显示它发现问题的行。然后,您可以开始查看它的各个部分,以查看哪个值为null,并开始回顾代码以找出原因。因此,在包含错误行的方法的开头放置一个断点,然后从头再次运行程序。这一次,VS会在错误发生前停止,让你通过查看代码来查看你的价值来检查发生了什么。



但我们做不到那 - 我们没有您的代码,如果我们拥有它,我们不知道如何使用它,我们没有您的数据。所以尝试一下 - 看看你能找到多少信息!


  protected   void  Application_PostAuthorizeRequest()
{
System.Web.HttpContext.Current.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Required);
}



添加

到global.asax


你没有显示抛出对象引用未设置为对象实例消息的异常。



不用担心。这是最容易检测和修复的案例之一。它只是意味着某些引用类型的某个成员/变量通过使用及其实例(非静态)成员进行解引用,这需要此成员/变量为非null,但实际上它似乎为null。只需在调试器下执行它;它将停止抛出异常的执行。在该行上设置一个断点,重新启动应用程序并再次到达这一点。评估下一行中涉及的所有引用,并查看哪一个为null,而不需要为null。解决这个问题之后,修复代码:要么确保将成员/变量正确初始化为非空引用,要么将其检查为null,如果为null,则执行其他操作。



另请参阅:想要在按钮点击时显示下一条记录。但是如果下一个记录功能的条件对象引用未设置为对象的实例则会出错。



有时候,你不能这样做在调试器下,由一个或另一个原因。一个非常讨厌的情况是,只有在调试信息不​​可用时构建软件时才会出现问题。在这种情况下,你必须使用更难的方式。首先,你需要确保你永远不会通过静默处理异常来阻止异常的传播(这是开发人员对自己的犯罪,但很常见)。您需要在每个线程的最顶层堆栈帧上捕获绝对所有异常。如果处理类型 System.Exception 的异常,则可以执行此操作。在处理程序中,您需要记录所有异常信息,尤其是 System.Exception.StackTrace

http://msdn.microsoft.com/en-us/library/system.exception.aspx

http://msdn.microsoft.com/en-us/library/ system.exception.stacktrace.aspx



堆栈跟踪只是一个字符串,显示从throw语句到处理程序的异常传播的完整路径。通过阅读,您总能找到目的。对于日志记录,使用类 System.Diagnostics.EventLog

http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx



祝你好运,

-SA


Hi,

Am trying to generate random number and pass it from class a to class b with session

Class A

public static void SendPwd(string Email)
{
    using (var client = new SmtpClient())
    {
        using (var message = new MailMessage(EmailFrom, Email))
        {
            Random rnd = new Random();
            var random = rnd.Next();
            if (random != null)
            {
                System.Web.HttpContext.Current.Session["random"] = random;
            }
            message.Subject = "Please Verify Your Account";
            message.Body = "<html><head><meta content=\"text/html; charset=utf-8\" /></head><body><p>Dear " + Email +
            ", </p><p>Please note the random Number and type in the Text box</p>" + "<p> Your Random Number is </p>" + random
            + "</a></p><div>Best regards,</div><div>Team</div><p>Do not forward "
            + "this email. The verify link is private.</p></body></html>";
            message.IsBodyHtml = true;
            client.EnableSsl = true;
            client.Send(message);
        };
    };
}


Class B

var password = con.tblEmployees.Where(a => a.Emp_Email == user.EmailId).FirstOrDefault();
password.Emp_TempPwd = (int)HttpContext.Current.Session["random"];
con.SaveChanges();
return "success";


while am doing this I got this error

An exception of type 'System.NullReferenceException' occurred in ServerControl.dll but was not handled in user code



What I have tried:

I have searched in Google to find it but I can’t find solution so kindly help me to solve this issue
thanks in advance

解决方案

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!


protected void Application_PostAuthorizeRequest() 
{
System.Web.HttpContext.Current.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Required);
}


add
to global.asax


You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown.

Not to worry. This is one of the easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferences by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger; it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object".

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx,
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx.

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx.

Good luck,
—SA


这篇关于random()中的System.nullreferenceexception问题;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆