对象引用未设置为对象的实例(在WPF中)

时间:2015-11-05 06:28:06

标签: wpf vb.net

private Sub Btn_Show_Click(sender As Object, e As RoutedEventArgs)
       Dim dt As New DataTable()
       Dim SearchCriteria = cmbSearch.Text
       dt.Columns.Add("Title", GetType(String))
       dt.Columns.Add("Author", GetType(String))
       dt.Columns.Add("AccessionNo", GetType(String))
       dt.Columns.Add("Location", GetType(String))
       dt.Columns.Add("Status", GetType(String))
       Dim xmldoc As New XmlDocument()
       xmldoc.Load("C:\Users\Shahrukh\Documents\Visual Studio 2012\Projects\Simple search1\Simple search1\New folder\Data.xml")
       Dim nodeList As XmlNodeList = xmldoc.SelectNodes("/NewDataSet/Table")


       If SearchCriteria = "Title" Then
           For Each node As XmlNode In nodeList
               Dim dtrow As DataRow = dt.NewRow()
               If (LCase(node("title").InnerText).Contains(LCase(txtSearch.Text()))) Then
                   dtrow("Title") = node("title").InnerText
                   dtrow("Author") = node("Author").InnerText
                   dtrow("AccessionNo") = node("AccessionNo").InnerText
                   dtrow("Location") = node("location").InnerText
                   dtrow("Status") = node("status").InnerText
                   dt.Rows.Add(dtrow)
               End If
           Next
       End If

       If SearchCriteria = "Author" Then
           For Each nodeAuthor As XmlNode In nodeList
               Dim dtrow As DataRow = dt.NewRow()
               If (LCase(nodeAuthor("Author").InnerText).Contains(LCase(txtSearch.Text()))) Then
                   dtrow("title") = nodeAuthor("title").InnerText
                   dtrow("Author") = nodeAuthor("Author").InnerText
                   dtrow("AccessionNo") = nodeAuthor("AccessionNo").InnerText
                   dtrow("location") = nodeAuthor("location").InnerText
                   dtrow("status") = nodeAuthor("status").InnerText
                   dt.Rows.Add(dtrow)
               End If
           Next
       End If
       dGridResults.ItemsSource = dt.DefaultView
   End Sub

它给出错误“对象引用未设置为对象的实例。”

1 个答案:

答案 0 :(得分:0)

我认为我看到了不一致。您有一个表格,其中包含“标题”列。在node / nodeAuthor中,你两次都获取“title”(所以我认为没问题)。但是对于dtRow,你在第一个if和“title”中写入“Title”。将其更改为“标题”,然后重试。

               dtrow("Title") = node("title").InnerText