System.NullReferenceException - 未将对象引用设置为对象的实例 [英] System.NullReferenceException - Object reference not set to an instance of an object

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

问题描述

我收到此异常错误,这让我疯了。

System.NullReferenceException - 对象引用未设置为实例

对象


如果我对此行发表评论,我不会收到任何错误:

string Total_Dollar_Amount = GetTotal(sql," test");

命名空间SalesReport

{

///< summary>

/// SalesReport的摘要说明。

///< / summary>

公共类SalesReport:System.Web.UI.Page

{

protected System.Web.UI.WebControls.Button Button1;


private void Page_Load(object sender,System.EventArgs e)

{

//将用户代码置于此处初始化页面

}


#region Web窗体设计器生成的代码

覆盖保护void OnInit(EventArgs e)

{

//

// CODEGEN:ASP.NET Web窗体设计器需要此调用。

//

InitializeComponent();

base.OnInit(e);

}


///< summary>

/// Designer支持所需的方法 - 不要使用代码编辑器修改

///此方法的内容。

///< / summary>

private void InitializeComponent()

{

this.Button1。单击+ = new System.EventHandler(this.Button1_Click);

this.Load + = new System.EventHandler(this.Page_Load);


} < br $>
#endregion


private ArrayList GetRowsArray()

{

ArrayList Ar = new ArrayList() ;

SqlConnection Conn = Common.GetConnection(" sonny");

string sql =" SELECT * from Sn_SalesReport" ;;

SqlCommand Command = new SqlCommand(sql,conn);

Command.Connection.Open();

SqlDataReader r = Command.ExecuteReader();

while(r.Read())

{

string sqlstatement = r [ " sqlstatement"]。ToString();

Ar.Add(sqlstatement);

Response.Write(sqlstatement +"< br>"); < br $>
}

r.Close();

Conn.Close();

返回Ar;

}

private void Button1_Click(object sender,System.EventArgs e)

{

ArrayList Ar = GetRowsArray();

//创建线程数组

线程[] t =新线程[Ar.Count];

int lIdx;

ProcessSQL sr;


for(lIdx = 0; lIdx< Ar.Count; lIdx ++)

{

sr =新ProcessSQL(Ar [lIdx] .ToString());

t [lIdx] =新主题(新的ThreadStart(sr.UpdateSalesReport));

t [lIdx] .Start();

}


for(lIdx = 0; lIdx< Ar.Count; lIdx ++)

{

//等待所有线程完成。

t [ lIdx] .Join();

}


}

}


公共类ProcessSQL

{

私有字符串sql;

公共ProcessSQL(字符串sql)

{

this.sql = sql;

}


public void UpdateSalesReport()

{

try

{

string Total_Dollar_Amount = GetTotal(sql," test");

}

catch(exception ex)

{

HttpContext.Current.Response.Write(" Error ########:&q​​uot; + ex.Message );

}

}


public string GetTotal(string sql,string database)

{

sql ="从SN_SalesReport中选择reportname ;;

string result = string.Empty;

try

{

SqlConnection Conn = Common.GetConnection(数据库);

SqlCommand Command = new SqlCommand(sql,Conn);

Command.Connection.Open();

SqlDataReader r = Command.ExecuteReader();

if(r.Read())

{

/ /得到总数

result = r.GetString(0);

}

r.Close();

如果(Conn!= null)

{

Conn.Close();

}


}

catch(例外情况)

{

HttpContext.Current.Response.Write(" Error ###### ##:&q​​uot; + ex.Message);

}

返回结果;

}

}



}

解决方案

Rodusa< rc ***** *****@yahoo.com>写道:

我收到这个异常错误,这让我疯了。
System.NullReferenceException - 对象引用未设置为对象的实例
字符串Total_Dollar_Amount = GetTotal(sql," test");




而不是只打印出消息,打印出完整的堆栈

跟踪。这样你应该能够看到代码的哪一行是

抛出异常。


注意,为了使你的代码更强大,你应该使用为您的连接/命令/数据读取器使用

语句,而不是直接调用

关闭。 (目前如果抛出异常,连接

并未关闭。)


-

Jon Skeet - < sk *** @ pobox.com>
http:// www。 pobox.com/~skeet

如果回复小组,请不要给我发邮件





***通过开发人员指南 http://www.developersdex.com ***


感谢Joh,但很抱歉,我没有太多的经验,在Asp中使用

。净。当你说话时,你可以更加特别地说打印出完整的堆栈

跟踪。 "我该怎么做?此外,我觉得它与ADO和线程有关。

。每当有几个线程因为它们同时尝试使用相同的数据库访问方法而需要b $ b。在关闭连接之前我尝试使用thread.sleep

但它不起作用。

我仍​​然得到异常。我几乎要回到线性的

编程。我明天需要把这些东西搞定。


谢谢


Rodrigo Lueneberg


***通过开发人员指南 http://www.developersdex.com 发送***

I am getting this exception error which is driving me nuts.
System.NullReferenceException - Object reference not set to an instance
of an object

If I comment this line, I don''t get any errors:
string Total_Dollar_Amount = GetTotal(sql,"test");

namespace SalesReport
{
/// <summary>
/// Summary description for SalesReport.
/// </summary>
public class SalesReport : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private ArrayList GetRowsArray()
{
ArrayList Ar = new ArrayList();
SqlConnection Conn = Common.GetConnection("sonny");
string sql = "SELECT * from Sn_SalesReport";
SqlCommand Command = new SqlCommand(sql, Conn);
Command.Connection.Open();
SqlDataReader r = Command.ExecuteReader();
while (r.Read())
{
string sqlstatement = r["sqlstatement"].ToString();
Ar.Add(sqlstatement);
Response.Write(sqlstatement+"<br>");
}
r.Close();
Conn.Close();
return Ar;
}
private void Button1_Click(object sender, System.EventArgs e)
{
ArrayList Ar = GetRowsArray();
// create the Thread array
Thread[] t = new Thread[ Ar.Count ];
int lIdx;
ProcessSQL sr;

for ( lIdx = 0; lIdx < Ar.Count; lIdx ++ )
{
sr = new ProcessSQL(Ar[lIdx].ToString());
t[lIdx] = new Thread( new ThreadStart( sr.UpdateSalesReport ) );
t[lIdx].Start();
}

for ( lIdx = 0; lIdx < Ar.Count; lIdx ++ )
{
// waiting for all the Threads to finish.
t[lIdx].Join();
}

}
}

public class ProcessSQL
{
private string sql;
public ProcessSQL (string sql)
{
this.sql = sql;
}

public void UpdateSalesReport()
{
try
{
string Total_Dollar_Amount = GetTotal(sql,"test");
}
catch(Exception ex)
{
HttpContext.Current.Response.Write("Error########: "+ex.Message);
}
}

public string GetTotal(string sql, string database)
{
sql = "Select reportname from SN_SalesReport";
string result = string.Empty;
try
{
SqlConnection Conn = Common.GetConnection(database);
SqlCommand Command = new SqlCommand(sql, Conn);
Command.Connection.Open();
SqlDataReader r = Command.ExecuteReader();
if(r.Read())
{
// gets total
result = r.GetString(0);
}
r.Close();
if (Conn!=null)
{
Conn.Close();
}

}
catch(Exception ex)
{
HttpContext.Current.Response.Write("Error########: "+ex.Message);
}
return result;
}
}


}

解决方案

Rodusa <rc**********@yahoo.com> wrote:

I am getting this exception error which is driving me nuts.
System.NullReferenceException - Object reference not set to an instance
of an object

If I comment this line, I don''t get any errors:
string Total_Dollar_Amount = GetTotal(sql,"test");



Rather than printing out just the message, print out the full stack
trace. That way you should be able to see which line of your code is
throwing the exception.

Note that to make your code more robust you should be using "using"
statements for your connection/command/data reader, rather than calling
Close directly. (Currently if an exception is thrown, the connection
isn''t closed.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too




*** Sent via Developersdex http://www.developersdex.com ***


Thanks Joh, but I am sorry, I don''t have too much experience with
debuging in Asp.net. Could you please be more especific when you talk
about "print out the full stack
trace. " How do I do it? Furthermore, I have a feeling it is something
to do with ADO and threading. Whenever several threads because they are
trying to use the same database access method at the same time. I tried
to use thread.sleep before closing the connection, but it does not work.
I still get the exception. I am almost going back to a linear
programming. I need to get this stuff working by tomorrow.

Thank you

Rodrigo Lueneberg

*** Sent via Developersdex http://www.developersdex.com ***


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

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