locked
NullReference error on file upload RRS feed

  • Question

  • User1216627406 posted

    Greetings again,

    I am getting the following error when attempting to email an uploaded file:

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    A brief history:

    We use a script (we got from aspsnippets.com).

    This script allows you to attach multiple files. In my example, I was able to attach three files successfully.

    Then I click Submit to send the attached files via email to user

    However, when I click to Submit, I am getting the NullReferencException error.

    I can confirm that when I check the Uploads folder where the files were stored, they are there.

    I am not sure what the issue is.

    Here is just relevant code that I am using:

    //HTML

    	                 <table style="width:90%;border:0; margin:auto;padding:10px;text-align:center;border-spacing:0;">
                          <tr>
                            <td colspan="3">&nbsp;</td>
                          </tr>
                          <tr>
                          <td colspan="2"></td>
                           <td><strong>File Attachments: </strong>
                           <asp:FileUpload ID="FileUpload1" runat="server" /></td>
                          </tr>
                          <tr>
                          <td></td>
                           <td>
                           <table id = "attachedfiles"></table>
                           </td>
                         </tr>
                        </table>	

    //VB:

                    Dim msg As MailMessage = New MailMessage()
                    Dim address As MailAddress = New MailAddress("myemail@gmail.com")
                    msg.To.Add(address)
                    msg.From = New MailAddress(email.Text.ToString())
                    msg.Subject = "Job Application from " & lblfname.Text & "&nbsp;" & lblmname.Text & "&nbsp;" & lbllname.Text
                    msg.Body = "Employment Application From " & lblfname.Text & "&nbsp;" & lblmname.Text & "&nbsp;" & lbllname.Text & "<br />Sent at:" & DateTime.Now & " <br />"
                    msg.Attachments.Add(New Attachment(New MemoryStream(bytes), lblfname.Text & "&nbsp;" & lblmname.Text & "&nbsp;" & lbllname.Text & DateTime.Now.Ticks & ".pdf"))
                    Dim files As List(Of HttpPostedFile) = DirectCast(Cache(Me.Key), List(Of HttpPostedFile))
                    For Each file As HttpPostedFile In files
                        msg.Attachments.Add(New Attachment(file.InputStream, Path.GetFileName(file.FileName), file.ContentType))
                    Next
                    msg.IsBodyHtml = True
                    Dim smtp As New SmtpClient()
                    Dim client As SmtpClient = New SmtpClient()
                    client.Host = "111.11.111.111"
                    smtp.UseDefaultCredentials = False
                    'smtp.Credentials = NetworkCred
                    smtp.Port = 25
                    client.Send(msg)

    Any ideas what I am doing wrong?

    Thanks in advance

    Friday, May 10, 2019 4:07 PM

All replies

  • User1120430333 posted

    Well, you have to debug the code with the debugger and single-step the until it hits the line of code that is throwing the exception. The error message means thae code is trying to reference/use an object that is a null valued object that is not in memory. You need to find out why.

    OO is OO Java or .NET

    https://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    Saturday, May 11, 2019 4:12 PM
  • User409696431 posted
    You need to look at what is null. You haven't said what code line, what value is null.
    Saturday, May 11, 2019 6:44 PM