Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataGridView的GetCachedPen函数中存在NullReferenceException_C#_.net_Winforms - Fatal编程技术网

C# DataGridView的GetCachedPen函数中存在NullReferenceException

C# DataGridView的GetCachedPen函数中存在NullReferenceException,c#,.net,winforms,C#,.net,Winforms,WinForms的DataGridView中存在NullReferenceException。事实上,我们使用的是来自DotNetBar的DataGridViewX组件,但是这是一个围绕标准DataGridView的相当简单的包装器,因此我有理由相信这与问题无关。堆栈跟踪是: Unhandled thread exception System.NullReferenceException: Object reference not set to an instance of an object.

WinForms的DataGridView中存在NullReferenceException。事实上,我们使用的是来自DotNetBar的DataGridViewX组件,但是这是一个围绕标准DataGridView的相当简单的包装器,因此我有理由相信这与问题无关。堆栈跟踪是:

Unhandled thread exception System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Windows.Forms.DataGridView.GetCachedPen(Color color)
   at System.Windows.Forms.DataGridView.PaintBorder(Graphics g, Rectangle clipRect, Rectangle bounds)
   at System.Windows.Forms.DataGridView.OnPaint(PaintEventArgs e)
   at DevComponents.DotNetBar.Controls.DataGridViewX.OnPaint(PaintEventArgs e)
   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
   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)
我在任何网站上都找不到类似错误的帖子

我在上查看了此函数的代码,此(内部/朋友)函数采用颜色并查看缓存笔的私有哈希表:

internal Pen GetCachedPen(Color color)
{
    Pen pen = (Pen) this.pens[color];
    if (pen == null)
    {
        pen = new Pen(color);
        this.pens.Add(color, pen);
    }
    return pen;
}

如果传入的颜色为null,则我可以看到此函数可能崩溃的唯一方式,但是我唯一可以看到调用此函数的时间是:

if (paintingNeeded)
{
    if (this.BorderStyle == BorderStyle.Fixed3D)
    {
        if (Application.RenderWithVisualStyles)
        {
            Pen pen = GetCachedPen(VisualStyleInformation.TextControlBorder);
            g.DrawRectangle(pen, new Rectangle(0, 0, bounds.Width - 1, bounds.Height - 1));
        }
        else
        {
            ControlPaint.DrawBorder3D(g, bounds, Border3DStyle.Sunken);
        }
    }
    else if (this.BorderStyle == BorderStyle.FixedSingle)
    {
        Pen pen = GetCachedPen(SystemColors.ControlText);
        g.DrawRectangle(pen, new Rectangle(0, 0, bounds.Width - 1, bounds.Height - 1));
    }
    else
    {
        Debug.Fail("DataGridView.PaintBorder - Unexpected BorderStyle value.");
    }
}
DGV的BorderStyle设置为BorderStyle.FixedSingle,因此传递给GetCachedPen的颜色参数应该是SystemColors.ControlText,对我来说,它永远不应该为null

我发现了一些关于DGV中与绘画相关的看似“随机”的崩溃帖子,但似乎没有一个解决方案,也没有一个与GetCachedPen函数相关。查看我们的错误日志数据库,我们可以看到,自从我们开始要求用户提交崩溃日志以来,在过去几年中,我们的软件已经崩溃了3次,因此这肯定不是系统性的错误,因为这是软件中使用良好的一部分


以前有没有人见过这次坠机,或者类似的事情?有解决办法吗?或者,在WinForms控件中是否有解决这些绘制错误的方法?

我也有同样的问题。但很奇怪,我只在我的笔记本电脑(Windows 10)上收到它,在我的工作站(Windows 7)上我没有这个问题。 在我的笔记本电脑上,我一直收到错误。打开表单进行编辑,我进行一些编辑,单击“保存”,然后关闭表单,我总是收到以下错误:

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Windows.Forms.DataGridView.GetCachedPen(Color color)
   at System.Windows.Forms.DataGridView.PaintBorder(Graphics g, Rectangle clipRect, Rectangle bounds)
   at System.Windows.Forms.DataGridView.OnPaint(PaintEventArgs e)
   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
   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.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
我不知道为什么只有一些电脑会出现这种情况,但我解决了如下问题。 在我的例子中,datagrid也包装在我自己的usercontrol中。在usercontrol中,可以重写OnPaint事件

Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    Try
        ' Dispose I will not do anything if done.
        If Not Me.IsDisposed Then
            MyBase.OnPaint(e)
        Else
            Debug.Print("SingleRecordDatagrid: Datagridview already disposed.")
        End If

    Catch ex As NullReferenceException
        ' Even if you give an exception, don't do anything.
        Debug.Print("SingleRecordDatagrid: " & ex.Message & "  StackTrace: " & vbCrLf & ex.StackTrace)
    End Try
End Sub

如果可以重现错误,您是否尝试过调试net framework源代码?不,谢谢你。我不知道这件事!不幸的是,到目前为止,我们还不能自己重现这个问题,但至少我们可以尝试通过单步执行代码来获得对涉及的值的更多理解。我会给它一个上帝,看看我能找到什么。我会先用.NET DGV组件替换自定义组件。然后您就可以确定它是自定义组件还是.NET组件。不幸的是,在这种状态下发布软件将是最后的选择,因为整个软件都使用自定义组件,并且与“gobal样式管理器”接口。有人花了一天时间试图在内部繁殖,但没有成功。如果您查看涉及的函数,我看不出它是如何成为自定义组件的:GetCachedPen是一个内部函数,pens是一个私有变量,而调用GetCachedPen的PaintBorder函数是私有的(这在我的原始帖子中没有提到)。除非我遗漏了一些明显的东西,否则这就排除了自定义组件?最后我们确实用标准的.NET DGV替换了自定义组件,但仍然存在崩溃。这篇文章并不是真正试图回答这个问题。请注意,这是一个问答网站,每个帖子都是一个问题或问题的答案。帖子也可以有这样的小句子,可以用来批评或要求作者澄清。这应该是一个评论或者一个感谢,最后我们用这个方法来解决问题。我们了解到的另一件事是,这种情况只发生在带有触摸屏的笔记本电脑上。在设备管理器中禁用触摸屏修复了此问题。。。