![](/img/trans.png)
[英]Object reference not set to an instance of an object (null DataSet??)
[英]object reference not set to an instance of an object In dataset
我正在使用以下代碼填充數據集
public static DataSet GetDataSetBySP(string spName)
{
try
{
using (SqlConnection conn = new SqlConnection(ConnString))
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = spName;
cmd.CommandTimeout = 60;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
return ds;
}
}
catch
{
return null;
}
}
在我第一次調用該函數時,我只獲得填充了數據的第一個表,而其他表看起來像這樣
和數據可視化器中的錯誤就像
DataSet Visualizer
Unhandled exception has occurred in a component in your application.If you click Continue,the application will ignore this error and attempt to continue.
Object referance not set to an instance of an object
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at EnhancedDataSetVisualizer.VisualizerCommunicator.GetTableCellValue(Int32 rowIndex, Int32 colIndex)
at EnhancedDataSetVisualizer.DataSetForm.dataGridView_CellValueNeeded(Object sender, DataGridViewCellValueEventArgs e)
at System.Windows.Forms.DataGridView.OnCellValueNeeded(DataGridViewCellValueEventArgs e)
at System.Windows.Forms.DataGridView.OnCellValueNeeded(Int32 columnIndex, Int32 rowIndex)
at System.Windows.Forms.DataGridViewCell.GetValue(Int32 rowIndex)
at System.Windows.Forms.DataGridViewCell.PaintWork(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, Int32 rowIndex, DataGridViewElementStates cellState, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
at System.Windows.Forms.DataGridViewRow.PaintCells(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow, DataGridViewPaintParts paintParts)
at System.Windows.Forms.DataGridViewRow.Paint(Graphics graphics, Rectangle clipBounds, Rectangle rowBounds, Int32 rowIndex, DataGridViewElementStates rowState, Boolean isFirstDisplayedRow, Boolean isLastVisibleRow)
at System.Windows.Forms.DataGridView.PaintRows(Graphics g, Rectangle boundingRect, Rectangle clipRect, Boolean singleHorizontalBorderAdded)
at System.Windows.Forms.DataGridView.PaintGrid(Graphics g, Rectangle gridBounds, Rectangle clipRect, Boolean singleVerticalBorderAdded, Boolean singleHorizontalBorderAdded)
at System.Windows.Forms.DataGridView.OnPaint(PaintEventArgs e)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.DataGridView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
Microsoft.VisualStudio.CommonIDE
Assembly Version: 9.0.0.0
Win32 Version: 9.0.30729.1
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.VisualStudio.CommonIDE/9.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.CommonIDE.dll
----------------------------------------
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
Microsoft.VisualStudio.DebuggerVisualizers
Assembly Version: 9.0.0.0
Win32 Version: 9.0.30729.1
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/Microsoft.VisualStudio.DebuggerVisualizers/9.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.DebuggerVisualizers.dll
----------------------------------------
Microsoft.VisualStudio.Debugger.DataSetVisualizer
Assembly Version: 9.0.0.0
Win32 Version: 9.0.30729.1
CodeBase: file:///C:/Program%20Files/Microsoft%20Visual%20Studio%209.0/Common7/Packages/Debugger/Visualizers/Microsoft.VisualStudio.Debugger.DataSetVisualizer.dll
----------------------------------------
System.Data
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.3053 (netfxsp.050727-3000)
CodeBase: file:///C:/WINDOWS/assembly/GAC_32/System.Data/2.0.0.0__b77a5c561934e089/System.Data.dll
----------------------------------------
************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.
For example:
<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>
When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.
有人可以告訴我為什么我看到這個嗎? 但是第二次當我刷新我的aspx頁面時,我得到了所有帶有適當數據的表格。
提前致謝
新編輯
大家好,謝謝您的快速回復。 但是我沒有遇到任何錯誤。數據集已完全填充成功,即使第一個表的數據也變得正確,但是在2個表的其余部分,我發現此異常與代碼無關。
最新更新
我也在另一項測試中發現,數據集可視化工具未顯示該數據表的內容。它包含記錄/表,但仍顯示上述一個錯誤。可能是由於某些問題
您是否正在檢查返回的值是否不為null? 您的函數中可能存在一些異常。 我會把抓到的部分重拍
catch
{
return new DataSet();
}
甚至更好,我將進行一些Error事件,以顯示一些帶有錯誤的消息框。
catch(Exception exc)
{
Error(exc);
return new DataSet();
}
我的猜測是與此有關:
catch
{
return null;
}
這意味着無論出什么問題 ,您都將忽略該問題(甚至不記錄該問題),而只返回null。 任何嘗試使用返回值而不檢查是否為空的東西都會給出您所顯示的異常。
您永遠不應該擁有這種全面的“全部捕獲並忽略”處理程序。 如果您確實想捕獲所有異常(在調用堆棧的根部可能是合理的),則應始終記錄該異常-那時,您不應嘗試進行更多工作。
在這種情況下,您可能應該有一個更高的異常處理程序,但根本不要在這里捕獲異常-讓它冒泡到堆棧上,而是通過中止當前用戶操作來在更高級別上處理異常。 假裝沒有任何問題,但是給出目前顯然無法處理的返回值並不是一個好主意。
我敦促您也不要返回一個空的DataSet
我說在這里進行任何錯誤“處理”可能是不合適的,因為您實際上並不知道如何處理它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.