Click here to Skip to main content
15,937,186 members
Please Sign up or sign in to vote.
4.20/5 (5 votes)
I keep on getting a null exception when I attempt to parse an Html string built in a stringbuilder object to an list. If I swap out the string builder object for some hard-coded HTML, it goes off without a hitch. I know the string builder is actually doing it's job because I have it outputting to a textbox. It displays flawless HTML, but when I attempt to call that string out with the HtmlWorker class. Does anyone have any clue why it would generate output when it is referenced as a string, but not when I attempt to drop it in the list object? I've included my code, and I will bold the exact Line I receive the error.

C#
private void createPDF()
{
    openFileDialog.Filter = "HTML Files (.html)|*.html";
    openFileDialog.FilterIndex = 0;
    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        System.Text.StringBuilder store = new System.Text.StringBuilder();
        string fileName = openFileDialog.FileName;

        try
        {
            using (System.IO.StreamReader htmlReader = new System.IO.StreamReader(openFileDialog.FileName))
            {
                string line;
                while ((line = htmlReader.ReadLine()) != null)
                {
                    store.Append(line + Environment.NewLine);
                }
                this.textBox1.Text = store.ToString();
                string html = store.ToString();
                Document document = new Document(PageSize.LETTER, 20, 25, 35, 35);


                PdfWriter.GetInstance(document, new FileStream("c:\\Data/my.pdf", FileMode.Create));
                document.Open();
                System.Collections.Generic.List<IElement> htmlarraylist = new List<IElement>(HTMLWorker.ParseToList(new StringReader(html), new StyleSheet()));
                foreach (IElement element in htmlarraylist)
                {
                    document.Add(element);
                }
                document.Close();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }


Like I said, if I swap out the line that links the Stringbuilder object to the string 'html' it works fine. The Null reference occurs every time on the bold line.
Posted
Comments
[no name] 6-Jul-12 11:30am    
Try changing new StringReader(html) to new StringReader(html).ToString() and see what happens.
MikeVaros 6-Jul-12 11:43am    
"Cannot convert from 'string' to 'System.IO.TextReader'" I'm almost positive I tried that myself yesterday, but I figured it may be worth a shot. Thank you for the response though. It seems to think that the "store" String is empty. If I manually enter the html as a string, it works perfectly, but this isn't dynamic enough to suit my needs. I'm banging my head off the wall trying to figure this out.

I'm just going out on a limb here, but if the size of the string exceeds Stringbuilder's default capacity, could it return a null exception?
[no name] 6-Jul-12 11:48am    
Probably but it would have to be a really huge string to exceed the capacity. My bad I read your line wrong anyway. What is it on that line that is null?
MikeVaros 6-Jul-12 11:55am    
I'll paste in my stack trace. It's pretty large to describe easily.

at iTextSharp.text.html.simpleparser.HTMLWorker.CreateLineSeparator(IDictionary`2 attrs)
at iTextSharp.text.html.simpleparser.HTMLTagProcessors.HTMLTagProcessor_HR.StartElement(HTMLWorker worker, String tag, IDictionary`2 attrs)
at iTextSharp.text.html.simpleparser.HTMLWorker.StartElement(String tag, IDictionary`2 attrs)
at iTextSharp.text.xml.simpleparser.SimpleXMLParser.ProcessTag(Boolean start)
at iTextSharp.text.xml.simpleparser.SimpleXMLParser.Go(TextReader reader)
at iTextSharp.text.xml.simpleparser.SimpleXMLParser.Parse(ISimpleXMLDocHandler doc, ISimpleXMLDocHandlerComment comment, TextReader r, Boolean html)
at iTextSharp.text.html.simpleparser.HTMLWorker.Parse(TextReader reader)
at iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(TextReader reader, StyleSheet style, IDictionary`2 tags, Dictionary`2 providers)
at iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(TextReader reader, StyleSheet style, Dictionary`2 providers)
at iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(TextReader reader, StyleSheet style)

Thank you for your help,

Mike
[no name] 6-Jul-12 12:08pm    
Sorry. Nothing is standing out to me. The only thing I can think of is that the StringReader is returning null... ah ha.... I think I see it now. In your StringBuilder you are appending newlines (CR/LF) which in not good HTML. In HTML you would want <br> so I think that your StringReader is choking on that.

Glad you got it working. Solution here is to make the question drop off the unanswered list.

The problem was a null reference exception being thrown due to HTML tags that the parser was unable to handle. Solution was to remove the tags.
 
Share this answer
 
Yes,it done.There is problem in HTML rendering beacuse it some HTML tag not properly made during append by developer So that StringReader is not render properly .
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900