对象引用没有设置为对象的实例

时间:2022-06-12 20:34:24
private void btnmap_Click(object sender, EventArgs e)
{
    XmlDocument xmldoc = new XmlDocument();
    XmlNode xmlnode, xmlroot, docNode, Doc;
    XmlAttribute xmlatt;                                                  
    if (rchtextfile.Text == "")
    {
        MessageBox.Show("Please Select a Text file", "File Name Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }
    else
    {
        con = new System.Data.SqlClient.SqlConnection();
        DataSet ds = new DataSet();
        con.ConnectionString = @"Server=sql1; User ID=blah; Pwd=fubar; Initial Catalog=xml;";
        con.Open(); 
        MessageBox.Show("Database Connected");
        String sql = "select Styles from Xml_Tags,pdfelement where Xml_Tags.Mapping_Id=pdfelement.Mapping_Id AND Xml_Tags.Pdf_Tag=pdfelement.Element_Name AND pdfelement.Style=Xml_Tags.Styles";               
        com = new SqlCommand(sql);
        da = new System.Data.SqlClient.SqlDataAdapter(sql, con);
        da.Fill(ds, "xml");
        maxrows = ds.Tables["xml"].Rows.Count;
        StreamReader objReader = new StreamReader(file, Encoding.Default, true);                                                     
        do
        {                    
            for (int i = 0; i < maxrows; i++)
            {                            
                docNode = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null);
                //xmldoc.AppendChild(docNode);
                dRow = ds.Tables["xml"].Rows[i];                           
                line = objReader.ReadLine();
                string st1 = ">";
                string st2 = "</";
                int end = line.IndexOf(st2);
                if (end != -1 && end > 1)
                {
                    st = line.IndexOf(st1);
                    en = line.IndexOf(st2);
                    int v = en - st;
                    sub = line.Substring(st + 1, v - 1);
                    rchtext.Text = rchtext.Text + sub + "\r\n";
                }                              

                String sqll = "select Dtd_Tag from Xml_Tags,pdfelement where Xml_Tags.Mapping_Id=pdfelement.Mapping_Id AND Xml_Tags.Pdf_Tag=pdfelement.Element_Name AND pdfelement.Style=Xml_Tags.Styles";
                SqlCommand comm = new SqlCommand(sqll);
                SqlDataAdapter daa = new System.Data.SqlClient.SqlDataAdapter(sqll, con);
                DataSet ds1 = new DataSet();
                daa.Fill(ds1, "xml");
                dRow1 = ds1.Tables["xml"].Rows[i];
                //String sqlll = "select Dtd_Attribute_Name from Mapped_Tags_Attributes,Xml_Tags where Mapped_Tags_Attributes.Pdf_Tag=Xml_Tags.Pdf_Tag";
                String sqlll = "select Dtd_Attribute_Name from Mapped_Tags_Attributes,Xml_Tags where Mapped_Tags_Attributes.Pdf_Tag=Xml_Tags.Pdf_Tag AND Mapped_Tags_Attributes.Mapping_Id=Xml_Tags.Mapping_Id";
                SqlCommand cmd = new SqlCommand(sqlll);
                SqlDataAdapter dt = new System.Data.SqlClient.SqlDataAdapter(sqlll, con);
                DataSet ds2 = new DataSet();
                dt.Fill(ds2, "xml");
                dRow2 = ds2.Tables["xml"].Rows[i];
                name = XmlConvert.EncodeName(dRow1.ItemArray.GetValue(0).ToString());
                xmlnode = xmldoc.CreateElement(name);
                Doc = xmldoc.CreateDocumentType(name, null, "E:\\Rachana_mds\\proj\\pdfextraction\\docbook.dtd", null);
                //xmldoc.AppendChild(Doc); 
                xmlroot = xmldoc.CreateElement(name);
                //xmldoc.AppendChild(xmlroot);
                xmlatt = xmldoc.CreateAttribute(dRow2.ItemArray.GetValue(0).ToString());
                xmlroot.AppendChild(xmlnode);
                xmlnode.InnerText = sub;                                
            }
        }
        while (dRow[0].ToString() != line && objReader.Peek() != -1);

        MessageBox.Show("Done XML")                            
        saveFileDialog1.Filter = "XML Files (*.xml)|*.xml";
        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            StreamWriter ya=new StreamWriter (saveFileDialog1.FileName.ToString());   
            xmldoc.Save(ya);
        }
        else
        {
            return;
        }
    }
    MessageBox.Show("Successfully saved");                                    
    con.Close();               
    }       
}

I am getting error at the int end = line.IndexOf(st2); line

在int端= line.IndexOf(st2)行

4 个解决方案

#1


6  

If you're getting a NullReferenceException on

如果您正在获取NullReferenceException

int end = line.IndexOf(st2);

then line must be null. You should find out why - my guess is that there aren't as many lines in the file as you expect. (TextReader.ReadLine returns null when it's run out of data.)

那么行必须为空。您应该找出原因——我的猜测是,文件中没有您期望的那么多行。(TextReader。ReadLine在耗尽数据时返回null。

Additionally:

另外:

  • This method is large, and hard to read. Try to refactor it into smaller chunks.
  • 这个方法很大,很难阅读。试着把它重构成更小的块。
  • Use using statements for resources such as StreamReader and SqlConnection
  • 对资源(如StreamReader和SqlConnection)使用using语句
  • Using LINQ to XML would probably make the XML part simpler
  • 使用LINQ to XML可能会使XML部分更简单。
  • Rather than using objReader.Peek, check for the line being null to detect the end of input
  • 而不是使用objReader。Peek,检查行是否为空,以检测输入的结束
  • (Relatively advanced) If this is in a client app, doing all of this work in the UI thread is going to lock up the UI. You should perform long-running tasks in a different thread.
  • (相对高级)如果这是在客户端应用程序中,在UI线程中做所有这些工作将会锁定UI。您应该在不同的线程中执行长时间运行的任务。

#2


0  

probably objReader.ReadLine(); returns null, so

可能objReader.ReadLine();返回null,所以

line = objReader.ReadLine();

doesn't work as supposed

不应该工作

#3


0  

check that line is null or not:

检查该行是否为空:

int end;
if(line != null)
   end= line.IndexOf(st2);

#4


-1  

Firstly, Try to format a bit better the code is unreadeable.

首先,试着把代码格式化得更好一点,因为代码是不可读的。

The problem is that any of the object is not initialized.

问题是任何对象都没有初始化。

#1


6  

If you're getting a NullReferenceException on

如果您正在获取NullReferenceException

int end = line.IndexOf(st2);

then line must be null. You should find out why - my guess is that there aren't as many lines in the file as you expect. (TextReader.ReadLine returns null when it's run out of data.)

那么行必须为空。您应该找出原因——我的猜测是,文件中没有您期望的那么多行。(TextReader。ReadLine在耗尽数据时返回null。

Additionally:

另外:

  • This method is large, and hard to read. Try to refactor it into smaller chunks.
  • 这个方法很大,很难阅读。试着把它重构成更小的块。
  • Use using statements for resources such as StreamReader and SqlConnection
  • 对资源(如StreamReader和SqlConnection)使用using语句
  • Using LINQ to XML would probably make the XML part simpler
  • 使用LINQ to XML可能会使XML部分更简单。
  • Rather than using objReader.Peek, check for the line being null to detect the end of input
  • 而不是使用objReader。Peek,检查行是否为空,以检测输入的结束
  • (Relatively advanced) If this is in a client app, doing all of this work in the UI thread is going to lock up the UI. You should perform long-running tasks in a different thread.
  • (相对高级)如果这是在客户端应用程序中,在UI线程中做所有这些工作将会锁定UI。您应该在不同的线程中执行长时间运行的任务。

#2


0  

probably objReader.ReadLine(); returns null, so

可能objReader.ReadLine();返回null,所以

line = objReader.ReadLine();

doesn't work as supposed

不应该工作

#3


0  

check that line is null or not:

检查该行是否为空:

int end;
if(line != null)
   end= line.IndexOf(st2);

#4


-1  

Firstly, Try to format a bit better the code is unreadeable.

首先,试着把代码格式化得更好一点,因为代码是不可读的。

The problem is that any of the object is not initialized.

问题是任何对象都没有初始化。