0

我是 .Net 的新手,不知道如何解决我的大部分问题。这是我的问题。我创建了一个表单来执行脚本列表。数据将来自数据库。我的问题是,当我调试时,我得到了NullReferenceException这段代码的错误

private void Load_RefreshForm_ScriptMgmt()
{
    try
    {
        SqlCommand ObjCmd = new SqlCommand();
        SqlConnection ObjConn = new SqlConnection();
        this.Ds_Settings1.Tables["Settings_RefreshForm_ScriptMgmt_SelectALL"].Clear();
        ObjConn.ConnectionString = this.MainConnection;
        ObjCmd.CommandType = CommandType.StoredProcedure;
        ObjCmd = new SqlCommand(this.da_LoadScripts.SelectCommand.CommandText.ToString(), ObjConn);
        this.da_LoadScripts.SelectCommand = ObjCmd;
        this.da_LoadScripts.Fill(this.Ds_Settings1);
    }
    catch (Exception exception)
    {
        ProjectData.SetProjectError(exception);
        this.DisplayOnly_ErrorHandler("ERROR LOADING REFRESH SCRIPTS ", exception.Message, MsgBoxStyle.Critical);
        ProjectData.ClearProjectError();
    }
}
4

2 回答 2

0

You can see which line throw that exception by setting a breakpoint on the first line in the try block and follow the execution.

There are many lines that can throw that exception. this.Ds_Settings1 can be null, this.da_LoadScripts too. If one of these objects is null and you are trying to call a function on it or access a property, you will get that error. Verify if these objects are defined before calling that function.

于 2013-10-03T20:01:07.550 回答
0

您可以通过在“异常”窗口中启用中断执行来轻松跟踪哪个语句引发了此异常:

调试 > 异常... (Ctrl + Alt + E)

并选中 Common Language Runtime Exceptions 'Thrown' 复选框。

至于异常本身 - 它非常不言自明 - 您正在尝试使用未分配给任何东西的引用变量(无处指向)。

于 2013-10-03T20:16:27.360 回答