none
CheckedListBox.DataSource赋值触发System.NullReferenceException异常 RRS feed

  • 问题

  • 最近使用Visual Studio 2015编写代码时,偶然发现在给CheckedListBox.DataSource赋值时,会触发System.NullReferenceException异常,IDE在即时窗口出现异常提示信息,但是程序似乎仍可以正常运行。

    于是我从新建了个项目进行测试,重现了该异常。

    Public Class Form1
        Public Sub New()
            ' 此调用是设计器所必需的。
            InitializeComponent()
    
            ' 在 InitializeComponent() 调用之后添加任何初始化。
            CheckedListBox1.DataSource = {"23", "dfa", "we", "2e2"}.ToArray
        End Sub
    End Class
    

    通过调试CheckListBox.cs,发现异常由以下代码引发:

            /// <include file='doc\CheckedListBox.uex' path='docs/doc[@for="CheckedListBox.RefreshItems"]/*' />
            /// <devdoc>
            /// Reparses the objects, getting new text strings for them.
            /// </devdoc>
            /// <internalonly/>
            protected override void RefreshItems() {
                Hashtable savedcheckedItems = new Hashtable();
                for (int i =0; i < Items.Count ; i ++)
                {
                    savedcheckedItems[i] = CheckedItems.GetCheckedState(i);
                }
                
                //call the base
                base.RefreshItems();
                // restore the checkedItems...
    
                for (int j =0; j < Items.Count; j++)
                {
                    CheckedItems.SetCheckedState(j, (CheckState)savedcheckedItems[j]);
                }
            }

    具体就是在“CheckedItems.SetCheckedState(j, (CheckState)savedcheckedItems[j]);”这里,savedcheckedItems没有元素。


    我在开发代码时,使用了这样的语句来生成列表项:

    dim A= (From RowA As DataRow In Table.Rows Select 分组 = RowA("分组") Distinct).ToArray

    所以用到了以下这样的语句。

    CheckedListBox1.DataSource = A

    至此,想请教高手,给DataSource赋值{"23", "dfa", "we", "2e2"}.ToArray应该没什么问题吧?如果没有,这似乎是原生代码的bug吧。有没有其他赋值形式可以避免该错误?


    编程是永无止境的,向大家学习

    2017年4月21日 4:08

全部回复

  • 你好 abcjackson,

    根据你的描述, 我做了一个简单的CheckListBox绑定数据远的例子,它能够正常显示,而且对CheckListBox进行遍历,也能够取到他的State。

    Dim dt As New DataTable
        Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim value As String() = {"Cherry", "Breeze", "Barry"}
            CheckedListBox2.DataSource = value
    
         End Sub
     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            For i As Integer = 0 To CheckedListBox2.Items.Count - 1
                Dim a As Boolean = CheckedListBox2.GetItemCheckState(i)
                a = Nothing
            Next
        End Sub

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年4月21日 8:16
    版主
  • 是的,正如我上面说的,程序是正常运行的,但是IDE在即时窗口中会出现异常提示信息。

    例如这样的信息:

    引发的异常:“System.NullReferenceException”(位于 System.Windows.Forms.dll 中)
    引发的异常:“System.NullReferenceException”(位于 System.Windows.Forms.dll 中)
    引发的异常:“System.NullReferenceException”(位于 System.Windows.Forms.dll 中)


    编程是永无止境的,向大家学习

    2017年4月26日 15:26
  • Hi abcjackson,

    Some code you provide is C# code, but there are also VB code, I don't know your project is vb or C#. In addition to, I find there are some issue with the HashTable, Each element is a key/value pair stored in a DictionaryEntry object.

    Dim openWith As New Hashtable()
    
            ' Add some elements to the hash table. There are no 
            ' duplicate keys, but some of the values are duplicates.
            openWith.Add("txt", "notepad.exe")
            openWith.Add("bmp", "paint.exe")
            openWith.Add("dib", "paint.exe")
    

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年5月4日 9:31
    版主
  • Thanks for help.

    Yes, I write my codes in VB.net. I just need to comfirm whether my code have a problem and how to fix it.

    I used .Net Reflector to check the original code of control CheckListBox , which is presented as cs code above.

    The abnormity does not cause any adverse effect to my code and the program running seems normal. the IDE just gave a tips like this:

    引发的异常:“System.NullReferenceException”(位于 System.Windows.Forms.dll 中)
    引发的异常:“System.NullReferenceException”(位于 System.Windows.Forms.dll 中)
    引发的异常:“System.NullReferenceException”(位于 System.Windows.Forms.dll 中)

    Should I wait for Microsoft to improve it? : )


    编程是永无止境的,向大家学习

    2017年5月16日 5:24