获取错误:对象引用未设置为对象的实例。 [英] getting error: Object reference not set to an instance of an object.

查看:61
本文介绍了获取错误:对象引用未设置为对象的实例。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,



a项目,我现在正在工作,需要从其他网页获取数据。



我的系统本地一切都很好......但是当我把它上传到godaddy时...它给出了错误请查看下面的图片



http://postimg.org/image/6v0u503uv/ [ ^ ]



请告诉我解决方案



谢谢你

hi friends,

a project which i am working now, it needs to get data from another webpage.

everything is fine in my system locally... but when i upload it to godaddy... it's giving error pls check the image below

http://postimg.org/image/6v0u503uv/[^]

pls let me know the solution

thank you

推荐答案

你有两个问题......一个你没有防御性编码,所以容易出现这些错误。无法保证您的会话变量存在,因此您必须先检查。其次看起来你可能将int存储为字符串,你不应该这样做。存储int使用



You have two issues...one you are not coding defensively so are prone to these errors. There is no guarantee that your session variables exist so you have to check first. Secondly it looks like you might be storing ints as strings, which you shouldn't. To store an int use

Session["session"] = myIntVar;





读回来;





to read it back;

int x = 0;
if (Session["session"] != null)
{
    x = (int)Session["session"];
}





这将停止错误,但现在您的代码需要知道在没有会话时该怎么做变量,它必须做一些明智的事情,如显示消息,重定向到登录页面等等。



您的下一个问题是会话为空的原因。可能是godaddy正在减少你的会话超时很少,可能会话根本不起作用。我们不知道。



最后,祝你的体重问题好运,但请远离互联网上的蛇油产品:)



That'll stop the error, but now you code needs to know what to do when there is no session variable, it has to do something sensible like show a message, redirect to a login page, whatever.

Your next issue is why the session is empty. It could be that godaddy are reducing your session timeouts quite low, it could be that sessions aren't working at all. We don't know.

Lastly, good luck with your weight issues but please stay away from snake-oil products from the internet :)


会话[会话] 可能为空,您需要找到原因。为了避免异常,你可以先检查对象为null然后转换为整数,如下所示

Session["session"] may be null, you need to find the reason for that. to avoid the exception you can first check object for null and then convert to integer as below
if(Session["session"]!=null)
{
  int a =(int)Session["session"];
}


在该行代码中只有一个可能的原因是空引用。您的会话[会话] 调用返回null。您的代码永远不会检查该调用是否返回任何内容。它只是假设会话中的一个值在名为session的密钥下。
There's only one possible cause for a null reference in that line of code. Your Session["session"] call returned null. Your code never checks to see if that call returned anything. It just assumes that there was a value in the Session under the key called "session".


这篇关于获取错误:对象引用未设置为对象的实例。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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