locked
Getting System.NullReferenceException Error While Creating PDF Files RRS feed

  • Question

  • User-1062969975 posted

    Hello guys,

    I need some suggestions from any one that who can help me to solve my problem.

    I have created aspx with itextsharp and it works if I right click and open with browser. I can fill the forms which include textboxes and radiabuttonlists and press the print button to create PDF.

    But when I try to run project it gave System.NullReferenceException error at this point 

    Dim writer = PdfWriter.GetInstance(docoutput)
    

    Here is my aspx.vb codes

    Protected Sub Button1_Click(sender As Objecte As EventArgsHandles Button1.Click
            If TextBox1.Text = "" Then
                MsgBox("Lütfen Rapor Numarasını Giriniz"Title:="Teknik Servis Raporları")
            ElseIf TextBox2.Text = "" Then
                MsgBox("Lütfen Rapor Tarihini Giriniz"Title:="Teknik Servis Raporları")
            ElseIf TextBox3.Text = "" Then
                MsgBox("Lütfen Kurum Adını Giriniz"Title:="Teknik Servis Raporları")
            ElseIf TextBox4.Text = "" Then
                MsgBox("Lütfen Cihaz Markasını Giriniz"Title:="Teknik Servis Raporları")
            ElseIf TextBox5.Text = "" Then
                MsgBox("Lütfen Cihaz Modelini Giriniz"Title:="Teknik Servis Raporları")
            ElseIf TextBox6.Text = "" Then
                MsgBox("Lütfen Seri Numarasını Giriniz"Title:="Teknik Servis Raporları")
            Else
     
                Using output = New MemoryStream()
                    Dim doc = New Document(PageSize.A4, 111010)
                    Dim writer = PdfWriter.GetInstance(docoutput)
                    writer.CloseStream = False
                    doc.Open()
                    doc.NewPage()
                    Dim Helvetica = itextsharp.text.pdf.BaseFont.CreateFont("Helvetica""CP1254", itextsharp.text.pdf.BaseFont.NOT_EMBEDDED)
                    Dim t1 = New itextsharp.text.Font(Helvetica20PdfFontStyle.Bold)
                    Dim t2 = New itextsharp.text.Font(Helvetica14PdfFontStyle.Bold)
                    Dim t3 = New itextsharp.text.Font(Helvetica12PdfFontStyle.Bold)
                    Dim t4 = New itextsharp.text.Font(Helvetica12PdfFontStyle.Italic)
                    Dim t5 = New itextsharp.text.Font(Helvetica10PdfFontStyle.Italic)
     
                    Dim logo = itextsharp.text.Image.GetInstance(Server.MapPath("~/images/farmakim logo.png"))
                    logo.SpacingAfter = 10
                    logo.ScaleAbsolute(50210)
                    Dim Empty As New PdfPTable(3)
                    Empty.SetWidths(New Integer() {100440100})
                    Empty.DefaultCell.BorderColor = BaseColor.WHITE
                    Empty.TotalWidth = 150
                    Empty.SpacingBefore = 100
                    Empty.DefaultCell.VerticalAlignment = PdfAlignmentStyle.MiddleCenter
                    Empty.DefaultCell.FixedHeight = 100
                    Empty.AddCell("")
                    Empty.AddCell(logo)
                    Empty.AddCell("")
                    doc.Add(Empty)
     
                    Dim rn As New PdfPTable(1)
                    rn.DefaultCell.FixedHeight = 20
                    rn.DefaultCell.HorizontalAlignment = HorizontalAlign.Left
                    rn.AddCell(New Phrase(Label1.Text + " : " + TextBox1.Text, t3))
                    doc.Add(rn)
     
                    Dim header As New PdfPTable(1)
                    header.DefaultCell.FixedHeight = 40
                    header.DefaultCell.VerticalAlignment = PdfAlignmentStyle.MiddleCenter
                    header.DefaultCell.HorizontalAlignment = HorizontalAlign.Left
                    header.AddCell(New Phrase(Label2.Text, t1))
                    doc.Add(header)
     
                    Dim atable1 As New PdfPTable(1)
                    atable1.DefaultCell.BorderColorLeft = BaseColor.BLACK
                    atable1.DefaultCell.BorderColorRight = BaseColor.BLACK
                    atable1.DefaultCell.BorderColorBottom = BaseColor.WHITE
                    atable1.DefaultCell.BorderColorTop = BaseColor.WHITE
                    atable1.DefaultCell.FixedHeight = 20
                    atable1.AddCell(New Phrase("" & Label3.Text & "                                    " & Label4.Text & ""t5))
                    atable1.AddCell(New Phrase("" & Label5.Text & "              " & Label6.Text & "              " & Label7.Text & ""t5))
                    doc.Add(atable1)
     
                    Dim rTable = New PdfPTable(2)
                    rTable.SetWidths(New Integer() {200440})
                    rTable.HorizontalAlignment = PdfTextAlignment.Center
                    rTable.DefaultCell.FixedHeight = 35
                    rTable.DefaultCell.VerticalAlignment = PdfAlignmentStyle.MiddleCenter
                    rTable.AddCell(New Phrase(Label8.Text, t3))
                    rTable.AddCell(New Phrase(TextBox2.Text, t4))
                    rTable.AddCell(New Phrase(Label9.Text, t3))
                    rTable.AddCell(New Phrase(TextBox3.Text, t4))
                    rTable.AddCell(New Phrase(Label10.Text, t3))
                    rTable.AddCell(New Phrase(TextBox4.Text, t4))
                    rTable.AddCell(New Phrase(Label11.Text, t3))
                    rTable.AddCell(New Phrase(TextBox5.Text, t4))
                    rTable.AddCell(New Phrase(Label12.Text, t3))
                    rTable.AddCell(New Phrase(TextBox6.Text, t4))
                    rTable.AddCell(New Phrase(Label13.Text, t3))
                    rTable.AddCell(New Phrase(TextBox7.Text, t4))
                    rTable.AddCell(New Phrase(Label14.Text, t3))
                    rTable.AddCell(New Phrase(RadioButtonList1.SelectedItem.Text, t4))
                    rTable.AddCell(New Phrase(Label15.Text, t3))
                    rTable.AddCell(New Phrase(RadioButtonList2.SelectedItem.Text, t4))
                    doc.Add(rTable)
     
                    Dim dtable = New PdfPTable(2)
                    dtable.SetWidths(New Integer() {200440})
                    dtable.DefaultCell.FixedHeight = 50
                    dtable.AddCell(New Phrase(Label16.Text, t3))
                    dtable.AddCell(New Phrase(TextBox8.Text, t4))
                    doc.Add(dtable)
     
                    Dim wtable = New PdfPTable(2)
                    wtable.SetWidths(New Integer() {200440})
                    wtable.DefaultCell.FixedHeight = 120
                    wtable.AddCell(New Phrase(Label17.Text, t3))
                    wtable.AddCell(New Phrase(TextBox9.Text, t4))
                    doc.Add(wtable)
     
                    Dim stable = New PdfPTable(2)
                    stable.SetWidths(New Integer() {320320})
                    stable.DefaultCell.FixedHeight = 60
                    stable.AddCell(New Phrase("Servis Mühendisi"t3))
                    stable.AddCell(New Phrase("Kurum Yetkilisi"t3))
                    stable.AddCell(New Phrase("İmza"t3))
                    stable.AddCell(New Phrase("İmza"t3))
                    doc.Add(stable)
     
                    doc.Close()
                    writer.Close()
     
                    Response.ContentType = "application/pdf"
                    Response.AddHeader("content-disposition"String.Format("attachment;filename=Rapor-No-{0}.pdf", TextBox1.Text))
                    Response.OutputStream.Write(output.GetBuffer(), 0output.GetBuffer().Length)
                End Using
            End If
        End Sub

    And here is my aspx form.

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="TSR_Newreport.aspx.vb" Inherits="TSR_Web_vb.TSR_Newreport" %>
     
     
    <!DOCTYPE html>
     
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <link href="deneme.css" rel="stylesheet" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    <style type="text/css">
        .vertical {
            height:40px;
            display:flex;
            align-items:center;
        }
        .descs{
            height:80px;
            display:flex;
            align-items:center;
        }
        .works{
            height:140px;
            display:flex;
            align-items:center;
        }
    </style>
     
    </head>
    <body style="background-color:lightslategray">
        <center>
            <form id="form1" runat="server">
                <asp:Panel ID="Panel1" runat="server" style="height:842pxwidth:595px">
                <div style="height:842pxwidth:595px">
                    <div>
                        <asp:Image ID="Image1" runat="server" ImageUrl="~/images/farmakim logo.png" Height="50px"></asp:Image>
                    </div>
                    <br />
                    <div  style="border:solid thin black">
                        <table>
                            <tr>
                                <td>
                                    <asp:Label ID="Label1" runat="server" Text="Rapor Numarası" Width="150px" Font-Bold="True" Font-Names="Helvetica" Font-Size="14px"></asp:Label>
                                </td>
                                <td>
                                    <asp:TextBox ID="TextBox1" runat="server" Width="120px" Font-Names="Arial" Font-Size="12px"></asp:TextBox>
                                </td>
                            </tr>
                        </table>
                    </div>
                    <div style="border-left:thin solid blackborder-bottom:thin solid blackborder-right:thin solid black">
                        <asp:Label ID="Label2" runat="server" Text="TEKNİK SERVİS RAPORU" Font-Names="helvetica" Font-Size="24px" Font-Bold="true"></asp:Label>
                        <br />
                        <table>
                            <tr>
                                <td style="text-align:left">
                                    <asp:Label ID="Label3" runat="server" Text="Tekstilkent A 24 Blok No:11 Esenler/İstanbul/Türkiye" Font-Names="Helvetica" Font-Size="12px" Width="391px" Font-Italic="True"></asp:Label>
                                </td>
                                <td style="text-align:right">
                                    <asp:Label ID="Label4" runat="server" Text="Web : www.farmakim.com" Font-Names="Helvetica" Font-Size="12px" Width="189px" Font-Italic="True"></asp:Label>
                                </td>
                            </tr>
                        </table>
                        <table>
                            <tr>
                                <td style="text-align:left">
                                    <asp:Label ID="Label5" runat="server" Text="Telefon: +90 212 221 57 40" Font-Names="Helvetica" Font-Size="12px" Width="192px" Font-Italic="True"></asp:Label>
                                </td>
                                <td style="text-align:center">
                                    <asp:Label ID="Label6" runat="server" Text="Faks: +90 212 222 42 69" Font-Names="Helvetica" Font-Size="12px" Width="192px" Font-Italic="True"></asp:Label>
                                </td>
                                <td style="text-align:right">
                                    <asp:Label ID="Label7" runat="server" Text="Mail: farmakim@farmakim.com" Font-Names="Helvetica" Font-Size="12px" Width="192px" Font-Italic="True"></asp:Label>
                                </td>
                            </tr>
                        </table>
                    </div>
                    <div style="height:41pxtext-align:leftborder-left:thin solid blackborder-bottom:thin solid blackborder-right:thin solid black">
                        <div class="vertical" style="float:leftwidth:39%">
                            &nbsp&nbsp&nbsp<asp:Label ID="Label8" runat="server" Text="Rapor Tarihi" Font-Bold="True" Font-Names="Helvetica" Font-Size="12px"></asp:Label>                        
                        </div>
                        <div class="vertical" style="width:59%float:rightheight:40px; ">
                            <asp:TextBox ID="TextBox2" runat="server" Width="340px" TextMode="Date" Font-Names="Arial" Font-Size="12px"></asp:TextBox>
                            
                        </div>
                    </div>
                    <div style="height:41px;  text-align:leftborder-left:thin solid blackborder-bottom:thin solid blackborder-right:thin solid black ">
                        <div class="vertical" style="width:39%float:leftheight:40px;">
                            &nbsp&nbsp&nbsp<asp:Label ID="Label9" runat="server" Text="Kurum Adı" Font-Bold="True" Font-Names="Helvetica" Font-Size="12px"></asp:Label>
                        </div>
                        <div class="vertical" style="width:59%float:rightheight:40px; ">
                            <asp:TextBox ID="TextBox3" runat="server" Width="340px" Font-Names="Arial" Font-Size="12px"></asp:TextBox>
                        </div>
                    </div>
                    <div style="height:41px;  text-align:leftborder-left:thin solid blackborder-bottom:thin solid blackborder-right:thin solid black">
                        <div class="vertical" style="width:39%float:leftheight:40px; ">
                            &nbsp&nbsp&nbsp<asp:Label ID="Label10" runat="server" Text="Cihazın Markası" Font-Bold="True" Font-Names="Helvetica" Font-Size="12px"></asp:Label>
                        </div>
                        <div class="vertical" style="width:59%float:right;height:40px; ">
                            <asp:TextBox ID="TextBox4" runat="server" Width="340px" Font-Names="Arial" Font-Size="12px"></asp:TextBox>
                        </div>
                    </div>
                    <div style="height:41px;  text-align:leftborder-left:thin solid blackborder-bottom:thin solid blackborder-right:thin solid black">
                        <div class="vertical" style="width:39%float:leftheight:40px;">
                            &nbsp&nbsp&nbsp<asp:Label ID="Label11" runat="server" Text="Cihazın Modeli" Font-Bold="True" Font-Names="Helvetica" Font-Size="12px"></asp:Label>
                        </div>
                        <div class="vertical" style="width:59%float:rightheight:40px;">
                            <asp:TextBox ID="TextBox5" runat="server" Width="340px" Font-Names="Arial" Font-Size="12px"></asp:TextBox>
                        </div>
                    </div>
                    <div style="height:41px;  text-align:leftborder-left:thin solid blackborder-bottom:thin solid blackborder-right:thin solid black">
                        <div class="vertical" style="width:39%float:leftheight:40px;">
                            &nbsp&nbsp&nbsp<asp:Label ID="Label12" runat="server" Text="Seri Numarası" Font-Bold="True" Font-Names="Helvetica" Font-Size="12px"></asp:Label>
                        </div>
                        <div class="vertical" style="width:59%float:rightheight:40px;">
                            <asp:TextBox ID="TextBox6" runat="server" Width="340px" Font-Names="Arial" Font-Size="12px"></asp:TextBox>
                        </div>
                    </div>
                    <div style="height:41px;  text-align:leftborder-left:thin solid blackborder-bottom:thin solid blackborder-right:thin solid black">
                        <div class="vertical" style="width:39%float:leftheight:40px;">
                            &nbsp&nbsp&nbsp<asp:Label ID="Label13" runat="server" Text="Servis Mühendisi" Font-Bold="True" Font-Names="Helvetica" Font-Size="12px"></asp:Label>
                        </div>
                        <div class="vertical" style="width:59%float:rightheight:40px;">
                            <asp:TextBox ID="TextBox7" runat="server" Width="340px" Font-Names="Arial" Font-Size="12px"></asp:TextBox>
                        </div>
                    </div>
                    <div style="height:61px;  text-align:leftborder-left:thin solid blackborder-bottom:thin solid blackborder-right:thin solid black">
                        <div class="vertical" style="width:39%float:leftheight:60px;">
                            &nbsp&nbsp&nbsp<asp:Label ID="Label14" runat="server" Text="Servis Nedeni" Font-Bold="True" Font-Names="Helvetica" Font-Size="12px"></asp:Label>
                        </div>
                        <div class="vertical" style="width:59%float:right;height:60px;">
                            <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatColumns="4" RepeatDirection="Horizontal" Width="340px" Font-Bold="True" Font-Names="Helvetica" Font-Size="12px" AutoPostBack="True">
                                <asp:ListItem>Montaj</asp:ListItem>
                                <asp:ListItem>Bakım</asp:ListItem>
                                <asp:ListItem>Arıza</asp:ListItem>
                                <asp:ListItem>Kontrol</asp:ListItem>
                                <asp:ListItem>Eğitim</asp:ListItem>
                                <asp:ListItem>Uygulama</asp:ListItem>
                                <asp:ListItem>Değişiklik</asp:ListItem>
                            </asp:RadioButtonList>
                        </div>
                    </div>
                    <div style="height:41px;  text-align:leftborder-left:thin solid blackborder-bottom:thin solid blackborder-right:thin solid black">
                        <div class="vertical" style="width:39%float:leftheight:40px;">
                            &nbsp&nbsp&nbsp<asp:Label ID="Label15" runat="server" Text="Cihazın Durumu" Font-Bold="True" Font-Names="Helvetica" Font-Size="12px"></asp:Label>
                        </div>
                        <div class="vertical" style="width:59%float:rightheight:40px;">
                            <asp:RadioButtonList ID="RadioButtonList2" runat="server" RepeatDirection="Horizontal" Width="340px" Font-Bold="True" Font-Names="Helvetica" Font-Size="12px" AutoPostBack="True">
                                <asp:ListItem>Ücretli</asp:ListItem>
                                <asp:ListItem>Garantili</asp:ListItem>
                                <asp:ListItem>Kontratlı</asp:ListItem>
                            </asp:RadioButtonList>
                        </div>
                    </div>
                    <div style="height:80px;  text-align:leftborder-left:thin solid blackborder-bottom:thin solid blackborder-right:thin solid black">
                        <div class="descs" style="width:39%float:left">
                            &nbsp&nbsp&nbsp<asp:Label ID="Label16" runat="server" Text="Açıklama" Font-Bold="True" Font-Names="Helvetica" Font-Size="12px"></asp:Label>
                        </div>
                        <div class="descs" style="width:59%float:right">
                            <asp:TextBox ID="TextBox8" runat="server" Height="70px" Width="340px" TextMode="MultiLine" style="resize:none" Font-Names="Arial" Font-Size="12px"></asp:TextBox>
                        </div>
     
                    </div>
                    <div style="height:140px;  text-align:leftborder-left:thin solid blackborder-bottom:thin solid blackborder-right:thin solid black">
                        <div class="works" style="width:39%float:left">
                            &nbsp&nbsp&nbsp<asp:Label ID="Label17" runat="server" Text="Yapılan İşler" Font-Bold="True" Font-Names="Helvetica" Font-Size="12px"></asp:Label>
                        </div>
                        <div class="works" style="width:59%float:right">
                            <asp:TextBox ID="TextBox9" runat="server" Height="130px" Width="340px" TextMode="MultiLine" style="resize:none" Font-Names="Arial" Font-Size="12px"></asp:TextBox>
                        </div>
                    </div>
                    <div style="height:100pxborder-left:thin solid blackborder-bottom:thin solid blackborder-right:thin solid black">
                        <div style="height:100pxwidth:49%float:lefttext-align:leftborder-right:thin solid black">
                            &nbsp;&nbsp;&nbsp;
                            <asp:Label ID="Label18" runat="server" Text="Servis Mühendisi" Font-Bold="True" Font-Names="Helvetica" Font-Size="12px"></asp:Label>
                            <br />
                            <br />
                            &nbsp;&nbsp;&nbsp;
                            <asp:Label ID="Label19" runat="server" Text="İmza" Font-Bold="True" Font-Names="Helvetica" Font-Size="12px"></asp:Label>
                        </div>
                        <div style="height:100pxwidth:49%float:righttext-align:left">
                            <asp:Label ID="Label20" runat="server" Text="Kurum Yetkilisi" Font-Bold="True" Font-Names="Helvetica" Font-Size="12px"></asp:Label>
                            <br />
                            <br />
                            <asp:Label ID="Label21" runat="server" Text="İmza" Font-Bold="True" Font-Names="Helvetica" Font-Size="12px"></asp:Label>
                        </div>
                    </div>
                </div>
                </asp:Panel>
                <br />
                <br />
                <asp:Button ID="Button1" runat="server" Text="Print" OnClick="Button1_Click" Width="100px"></asp:Button>
                &nbsp&nbsp&nbsp<asp:Button ID="Button2" runat="server" Text="Formu Temizle" Width="100px"></asp:Button>
                &nbsp&nbsp&nbsp<asp:Button ID="Button3" runat="server" Text="İptal" Width="100px"></asp:Button>
            </form>
        </center>
    </body>
    </html>
    

    I believe there is someone that who can help me.

    Thank you

    Wednesday, October 14, 2020 8:17 PM

All replies

  • User-1330468790 posted

    Hi N.Elverdi,

      

    I did the test with your codes and found out that the error could not be thrown in the given point

    Dim writer = PdfWriter.GetInstance(docoutput)

    It seems impossible to get the Null-Reference exception since you don't even try to access the variables doc and output.

    However, if I don't select an item in lists "RadioButtonList1" and "RadioButtonList2", I will get a null reference exception from below codes:

    rTable.AddCell(New Phrase(RadioButtonList1.SelectedItem.Text, t4))
    rTable.AddCell(New Phrase(Label15.Text, t3))
    rTable.AddCell(New Phrase(RadioButtonList2.SelectedItem.Text, t4))             

    The reason is that the SelectedItem is null and you are trying to access the property "Text" of null.

    Test condition which successfully produces a PDF:

    •  TextBox 1~6 are assigned with value
    • Both RadioButtonList1 and RadioButtonList2 are assigned with a default selected value

     

    Could you please check if it is the reason?

     

    Reminder for protecting company/personal information:

    I noticed that you have included some company/personal information (Phone, Address, etc) in the codes. Please delete them (modify them with foo/bar-like information) to protect your secure information. We will not save any company/personal information but the forum is an open and public environment.

      

    Hope helps.

    Best regards,

    Sean

    Thursday, October 15, 2020 3:20 AM
  • User-1062969975 posted

    Dear Sean,

    Thank you for you quick reply. I have tried again with filling radiobuttonlists by me and it appears same error. I have add some screen shots.

    https://www.dosya.tc/server31/wzl0k0/01.JPG.html

    https://www.dosya.tc/server31/6dgvs9/02.JPG.html

    I hope it will help you to see more information.

    Thursday, October 15, 2020 4:11 AM
  • User-1062969975 posted

    Sean, I think I found problem,

    <a href="https://www.imageupload.net/image/tzUpk"><img src="https://img.imageupload.net/2020/10/15/02.jpg" alt="02.jpg" border="0" /></a>

    When I checked itextsharp.dll it worked

    Thursday, October 15, 2020 4:21 AM
  • User-1330468790 posted

    Hi N.Elverdi,

      

    Glad to see that you have solved the problem.

    I didn't expect that VS would miss a reference and trigger that error.

    From my side, I directly let VS download any iTextShart package using the quick action and refactoring helper. 

      

    Best regards,

    Sean

    Thursday, October 15, 2020 6:50 AM