Server Error: Object reference not set

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SAL

    Server Error: Object reference not set

    I am getting the following ERROR in my WebApp on line 30:

    Server Error in '/TestWebApp' Application.
    --------------------------------------------------------------------------------

    Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the
    current web request. Please review the stack trace for more information about
    the error and where it originated in the code.

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

    Source Error:


    Line 28: public void Upload_click(ob ject sender, System.EventArg s e)
    Line 29: {
    Line 30: if (uploadFile.Pos tedFile != null)
    Line 31: {
    Line 32: string test = uploadFile.Post edFile.FileName ;


    Source File: c:\documents and settings\my
    documents\mypro jects\web\proje ct\testwebapp\t est.aspx.cs Line: 30

    Stack Trace:


    [NullReferenceEx ception: Object reference not set to an instance of an
    object.]
    TestWebApp.test .Upload_click(O bject sender, EventArgs e) in c:\documents
    and settings\my documents\mypro jects\web\proje ct\testwebapp\t est.aspx.cs:30
    System.Web.UI.W ebControls.Butt on.OnClick(Even tArgs e)

    System.Web.UI.W ebControls.Butt on.System.Web.U I.IPostBackEven tHandler.RaiseP ostBackEvent(St ring eventArgument)
    System.Web.UI.P age.RaisePostBa ckEvent(IPostBa ckEventHandler
    sourceControl, String eventArgument)
    System.Web.UI.P age.RaisePostBa ckEvent(NameVal ueCollection postData)
    System.Web.UI.P age.ProcessRequ estMain()


    I thought I was doing something wrong in my WebApp, so I created a brand new
    solution to test with I get the same error. Below is all the code for my
    test. Basically, all this webpage has is a HTML File Field Control, and a
    Web Forms Button Control on it. After you click the File Field and select
    your file, then you click the Button which calls Upload_click. When it reads
    if (uploadFile.Pos tedFile != null) is when I get the error.

    I'm new with ASP.net and this seems pretty straight-forward, but I can't
    seem to see what I am doing wrong. Can someone tell me what I am doing wrong?


    using System;
    using System.Collecti ons;
    using System.Componen tModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.Sess ionState;
    using System.Web.UI;
    using System.Web.UI.W ebControls;
    using System.Web.UI.H tmlControls;

    namespace TestWebApp
    {
    /// <summary>
    /// Summary description for test.
    /// </summary>
    public class test : System.Web.UI.P age
    {
    protected System.Web.UI.W ebControls.Butt on Upload;
    protected System.Web.UI.H tmlControls.Htm lInputFile uploadFile;


    private void Page_Load(objec t sender, System.EventArg s e)
    {
    // Put user code to initialize the page here
    }

    public void Upload_click(ob ject sender, System.EventArg s e)
    {
    if (uploadFile.Pos tedFile != null)
    {
    string test = uploadFile.Post edFile.FileName ;
    }
    }


    #region Web Form Designer generated code
    override protected void OnInit(EventArg s e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeCompo nent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeCompo nent()
    {
    this.Load += new System.EventHan dler(this.Page_ Load);

    }
    #endregion
    }
    }


    <%@ Page language="c#" Codebehind="tes t.aspx.cs" AutoEventWireup ="false"
    Inherits="TestW ebApp.test" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>test</title>
    <meta name="GENERATOR " Content="Micros oft Visual Studio .NET 7.1">
    <meta name="CODE_LANG UAGE" Content="C#">
    <meta name="vs_defaul tClientScript" content="JavaSc ript">
    <meta name="vs_target Schema"
    content="http://schemas.microso ft.com/intellisense/ie5">
    </HEAD>
    <body>
    <form id="Form1" method="post" runat="server">
    <INPUT id="uploadFile " type="file">
    <asp:Button id="Upload" OnClick="Upload _click" runat="server"
    Text="Upload"></asp:Button>
    </form>
    </body>
    </HTML>

    Thanks,

  • Baski

    #2
    Re: Server Error: Object reference not set

    You are using uploadFile control to access your uploaded file, you should
    use Request.Files as shown below

    HttpFileCollect ion HttpFiles = Request.Files;

    for (int i = 0; i < HttpFiles.Count ; i ++)

    {

    HttpPostedFile _HttpPosted = HttpFiles[i];


    if ( _HttpPosted.Con tentLength 0 )

    {

    string lineText = string.Empty;

    using (StreamReader sr = new StreamReader(_H ttpPosted.Input Stream))

    {

    //here you can use the stream directly and process the file, without saving
    it to the server //or save it if that's your rquirement.

    while (sr.Peek() >= 0)

    {

    lineText = sr.ReadLine();

    }

    }

    }

    }

    Thanks

    baski

    "SAL" <SAL@discussion s.microsoft.com wrote in message
    news:3728389F-8ACD-4F30-908F-13EB6F50C554@mi crosoft.com...
    >I am getting the following ERROR in my WebApp on line 30:
    >
    Server Error in '/TestWebApp' Application.
    --------------------------------------------------------------------------------
    >
    Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the
    current web request. Please review the stack trace for more information
    about
    the error and where it originated in the code.
    >
    Exception Details: System.NullRefe renceException: Object reference not set
    to an instance of an object.
    >
    Source Error:
    >
    >
    Line 28: public void Upload_click(ob ject sender, System.EventArg s e)
    Line 29: {
    Line 30: if (uploadFile.Pos tedFile != null)
    Line 31: {
    Line 32: string test = uploadFile.Post edFile.FileName ;
    >
    >
    Source File: c:\documents and settings\my
    documents\mypro jects\web\proje ct\testwebapp\t est.aspx.cs Line: 30
    >
    Stack Trace:
    >
    >
    [NullReferenceEx ception: Object reference not set to an instance of an
    object.]
    TestWebApp.test .Upload_click(O bject sender, EventArgs e) in c:\documents
    and settings\my
    documents\mypro jects\web\proje ct\testwebapp\t est.aspx.cs:30
    System.Web.UI.W ebControls.Butt on.OnClick(Even tArgs e)
    >
    System.Web.UI.W ebControls.Butt on.System.Web.U I.IPostBackEven tHandler.RaiseP ostBackEvent(St ring
    eventArgument)
    System.Web.UI.P age.RaisePostBa ckEvent(IPostBa ckEventHandler
    sourceControl, String eventArgument)
    System.Web.UI.P age.RaisePostBa ckEvent(NameVal ueCollection postData)
    System.Web.UI.P age.ProcessRequ estMain()
    >
    >
    I thought I was doing something wrong in my WebApp, so I created a brand
    new
    solution to test with I get the same error. Below is all the code for my
    test. Basically, all this webpage has is a HTML File Field Control, and a
    Web Forms Button Control on it. After you click the File Field and select
    your file, then you click the Button which calls Upload_click. When it
    reads
    if (uploadFile.Pos tedFile != null) is when I get the error.
    >
    I'm new with ASP.net and this seems pretty straight-forward, but I can't
    seem to see what I am doing wrong. Can someone tell me what I am doing
    wrong?
    >
    >
    using System;
    using System.Collecti ons;
    using System.Componen tModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.Sess ionState;
    using System.Web.UI;
    using System.Web.UI.W ebControls;
    using System.Web.UI.H tmlControls;
    >
    namespace TestWebApp
    {
    /// <summary>
    /// Summary description for test.
    /// </summary>
    public class test : System.Web.UI.P age
    {
    protected System.Web.UI.W ebControls.Butt on Upload;
    protected System.Web.UI.H tmlControls.Htm lInputFile uploadFile;
    >
    >
    private void Page_Load(objec t sender, System.EventArg s e)
    {
    // Put user code to initialize the page here
    }
    >
    public void Upload_click(ob ject sender, System.EventArg s e)
    {
    if (uploadFile.Pos tedFile != null)
    {
    string test = uploadFile.Post edFile.FileName ;
    }
    }
    >
    >
    #region Web Form Designer generated code
    override protected void OnInit(EventArg s e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeCompo nent();
    base.OnInit(e);
    }
    >
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeCompo nent()
    {
    this.Load += new System.EventHan dler(this.Page_ Load);
    >
    }
    #endregion
    }
    }
    >
    >
    <%@ Page language="c#" Codebehind="tes t.aspx.cs" AutoEventWireup ="false"
    Inherits="TestW ebApp.test" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>test</title>
    <meta name="GENERATOR " Content="Micros oft Visual Studio .NET 7.1">
    <meta name="CODE_LANG UAGE" Content="C#">
    <meta name="vs_defaul tClientScript" content="JavaSc ript">
    <meta name="vs_target Schema"
    content="http://schemas.microso ft.com/intellisense/ie5">
    </HEAD>
    <body>
    <form id="Form1" method="post" runat="server">
    <INPUT id="uploadFile " type="file">
    <asp:Button id="Upload" OnClick="Upload _click" runat="server"
    Text="Upload"></asp:Button>
    </form>
    </body>
    </HTML>
    >
    Thanks,
    >

    Comment

    • Baski

      #3
      Re: Server Error: Object reference not set


      "SAL" <SAL@discYou are using uploadFile control to access your uploaded
      file, you should
      use Request.Files as shown below

      HttpFileCollect ion HttpFiles = Request.Files;

      for (int i = 0; i < HttpFiles.Count ; i ++)

      {

      HttpPostedFile _HttpPosted = HttpFiles[i];


      if ( _HttpPosted.Con tentLength 0 )

      {

      string lineText = string.Empty;

      using (StreamReader sr = new StreamReader(_H ttpPosted.Input Stream))

      {

      //here you can use the stream directly and process the file, without saving
      it to the server //or save it if that's your rquirement.

      while (sr.Peek() >= 0)

      {

      lineText = sr.ReadLine();

      }

      }

      }

      }

      Thanks

      baski
      ussions.microso ft.comwrote in message
      news:3728389F-8ACD-4F30-908F-13EB6F50C554@mi crosoft.com...
      >I am getting the following ERROR in my WebApp on line 30:
      >
      Server Error in '/TestWebApp' Application.
      --------------------------------------------------------------------------------
      >
      Object reference not set to an instance of an object.
      Description: An unhandled exception occurred during the execution of the
      current web request. Please review the stack trace for more information
      about
      the error and where it originated in the code.
      >
      Exception Details: System.NullRefe renceException: Object reference not set
      to an instance of an object.
      >
      Source Error:
      >
      >
      Line 28: public void Upload_click(ob ject sender, System.EventArg s e)
      Line 29: {
      Line 30: if (uploadFile.Pos tedFile != null)
      Line 31: {
      Line 32: string test = uploadFile.Post edFile.FileName ;
      >
      >
      Source File: c:\documents and settings\my
      documents\mypro jects\web\proje ct\testwebapp\t est.aspx.cs Line: 30
      >
      Stack Trace:
      >
      >
      [NullReferenceEx ception: Object reference not set to an instance of an
      object.]
      TestWebApp.test .Upload_click(O bject sender, EventArgs e) in c:\documents
      and settings\my
      documents\mypro jects\web\proje ct\testwebapp\t est.aspx.cs:30
      System.Web.UI.W ebControls.Butt on.OnClick(Even tArgs e)
      >
      System.Web.UI.W ebControls.Butt on.System.Web.U I.IPostBackEven tHandler.RaiseP ostBackEvent(St ring
      eventArgument)
      System.Web.UI.P age.RaisePostBa ckEvent(IPostBa ckEventHandler
      sourceControl, String eventArgument)
      System.Web.UI.P age.RaisePostBa ckEvent(NameVal ueCollection postData)
      System.Web.UI.P age.ProcessRequ estMain()
      >
      >
      I thought I was doing something wrong in my WebApp, so I created a brand
      new
      solution to test with I get the same error. Below is all the code for my
      test. Basically, all this webpage has is a HTML File Field Control, and a
      Web Forms Button Control on it. After you click the File Field and select
      your file, then you click the Button which calls Upload_click. When it
      reads
      if (uploadFile.Pos tedFile != null) is when I get the error.
      >
      I'm new with ASP.net and this seems pretty straight-forward, but I can't
      seem to see what I am doing wrong. Can someone tell me what I am doing
      wrong?
      >
      >
      using System;
      using System.Collecti ons;
      using System.Componen tModel;
      using System.Data;
      using System.Drawing;
      using System.Web;
      using System.Web.Sess ionState;
      using System.Web.UI;
      using System.Web.UI.W ebControls;
      using System.Web.UI.H tmlControls;
      >
      namespace TestWebApp
      {
      /// <summary>
      /// Summary description for test.
      /// </summary>
      public class test : System.Web.UI.P age
      {
      protected System.Web.UI.W ebControls.Butt on Upload;
      protected System.Web.UI.H tmlControls.Htm lInputFile uploadFile;
      >
      >
      private void Page_Load(objec t sender, System.EventArg s e)
      {
      // Put user code to initialize the page here
      }
      >
      public void Upload_click(ob ject sender, System.EventArg s e)
      {
      if (uploadFile.Pos tedFile != null)
      {
      string test = uploadFile.Post edFile.FileName ;
      }
      }
      >
      >
      #region Web Form Designer generated code
      override protected void OnInit(EventArg s e)
      {
      //
      // CODEGEN: This call is required by the ASP.NET Web Form Designer.
      //
      InitializeCompo nent();
      base.OnInit(e);
      }
      >
      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      private void InitializeCompo nent()
      {
      this.Load += new System.EventHan dler(this.Page_ Load);
      >
      }
      #endregion
      }
      }
      >
      >
      <%@ Page language="c#" Codebehind="tes t.aspx.cs" AutoEventWireup ="false"
      Inherits="TestW ebApp.test" %>
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
      <HTML>
      <HEAD>
      <title>test</title>
      <meta name="GENERATOR " Content="Micros oft Visual Studio .NET 7.1">
      <meta name="CODE_LANG UAGE" Content="C#">
      <meta name="vs_defaul tClientScript" content="JavaSc ript">
      <meta name="vs_target Schema"
      content="http://schemas.microso ft.com/intellisense/ie5">
      </HEAD>
      <body>
      <form id="Form1" method="post" runat="server">
      <INPUT id="uploadFile " type="file">
      <asp:Button id="Upload" OnClick="Upload _click" runat="server"
      Text="Upload"></asp:Button>
      </form>
      </body>
      </HTML>
      >
      Thanks,
      >

      Comment

      • SAL

        #4
        Re: Server Error: Object reference not set

        Thanks Baski. I will give it a try.

        "Baski" wrote:
        >
        "SAL" <SAL@discYou are using uploadFile control to access your uploaded
        file, you should
        use Request.Files as shown below
        >
        HttpFileCollect ion HttpFiles = Request.Files;
        >
        for (int i = 0; i < HttpFiles.Count ; i ++)
        >
        {
        >
        HttpPostedFile _HttpPosted = HttpFiles[i];
        >
        >
        if ( _HttpPosted.Con tentLength 0 )
        >
        {
        >
        string lineText = string.Empty;
        >
        using (StreamReader sr = new StreamReader(_H ttpPosted.Input Stream))
        >
        {
        >
        //here you can use the stream directly and process the file, without saving
        it to the server //or save it if that's your rquirement.
        >
        while (sr.Peek() >= 0)
        >
        {
        >
        lineText = sr.ReadLine();
        >
        }
        >
        }
        >
        }
        >
        }
        >
        Thanks
        >
        baski
        ussions.microso ft.comwrote in message
        news:3728389F-8ACD-4F30-908F-13EB6F50C554@mi crosoft.com...
        I am getting the following ERROR in my WebApp on line 30:

        Server Error in '/TestWebApp' Application.
        --------------------------------------------------------------------------------

        Object reference not set to an instance of an object.
        Description: An unhandled exception occurred during the execution of the
        current web request. Please review the stack trace for more information
        about
        the error and where it originated in the code.

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

        Source Error:


        Line 28: public void Upload_click(ob ject sender, System.EventArg s e)
        Line 29: {
        Line 30: if (uploadFile.Pos tedFile != null)
        Line 31: {
        Line 32: string test = uploadFile.Post edFile.FileName ;


        Source File: c:\documents and settings\my
        documents\mypro jects\web\proje ct\testwebapp\t est.aspx.cs Line: 30

        Stack Trace:


        [NullReferenceEx ception: Object reference not set to an instance of an
        object.]
        TestWebApp.test .Upload_click(O bject sender, EventArgs e) in c:\documents
        and settings\my
        documents\mypro jects\web\proje ct\testwebapp\t est.aspx.cs:30
        System.Web.UI.W ebControls.Butt on.OnClick(Even tArgs e)

        System.Web.UI.W ebControls.Butt on.System.Web.U I.IPostBackEven tHandler.RaiseP ostBackEvent(St ring
        eventArgument)
        System.Web.UI.P age.RaisePostBa ckEvent(IPostBa ckEventHandler
        sourceControl, String eventArgument)
        System.Web.UI.P age.RaisePostBa ckEvent(NameVal ueCollection postData)
        System.Web.UI.P age.ProcessRequ estMain()


        I thought I was doing something wrong in my WebApp, so I created a brand
        new
        solution to test with I get the same error. Below is all the code for my
        test. Basically, all this webpage has is a HTML File Field Control, and a
        Web Forms Button Control on it. After you click the File Field and select
        your file, then you click the Button which calls Upload_click. When it
        reads
        if (uploadFile.Pos tedFile != null) is when I get the error.

        I'm new with ASP.net and this seems pretty straight-forward, but I can't
        seem to see what I am doing wrong. Can someone tell me what I am doing
        wrong?


        using System;
        using System.Collecti ons;
        using System.Componen tModel;
        using System.Data;
        using System.Drawing;
        using System.Web;
        using System.Web.Sess ionState;
        using System.Web.UI;
        using System.Web.UI.W ebControls;
        using System.Web.UI.H tmlControls;

        namespace TestWebApp
        {
        /// <summary>
        /// Summary description for test.
        /// </summary>
        public class test : System.Web.UI.P age
        {
        protected System.Web.UI.W ebControls.Butt on Upload;
        protected System.Web.UI.H tmlControls.Htm lInputFile uploadFile;


        private void Page_Load(objec t sender, System.EventArg s e)
        {
        // Put user code to initialize the page here
        }

        public void Upload_click(ob ject sender, System.EventArg s e)
        {
        if (uploadFile.Pos tedFile != null)
        {
        string test = uploadFile.Post edFile.FileName ;
        }
        }


        #region Web Form Designer generated code
        override protected void OnInit(EventArg s e)
        {
        //
        // CODEGEN: This call is required by the ASP.NET Web Form Designer.
        //
        InitializeCompo nent();
        base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeCompo nent()
        {
        this.Load += new System.EventHan dler(this.Page_ Load);

        }
        #endregion
        }
        }


        <%@ Page language="c#" Codebehind="tes t.aspx.cs" AutoEventWireup ="false"
        Inherits="TestW ebApp.test" %>
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
        <HTML>
        <HEAD>
        <title>test</title>
        <meta name="GENERATOR " Content="Micros oft Visual Studio .NET 7.1">
        <meta name="CODE_LANG UAGE" Content="C#">
        <meta name="vs_defaul tClientScript" content="JavaSc ript">
        <meta name="vs_target Schema"
        content="http://schemas.microso ft.com/intellisense/ie5">
        </HEAD>
        <body>
        <form id="Form1" method="post" runat="server">
        <INPUT id="uploadFile " type="file">
        <asp:Button id="Upload" OnClick="Upload _click" runat="server"
        Text="Upload"></asp:Button>
        </form>
        </body>
        </HTML>

        Thanks,
        >
        >
        >

        Comment

        Working...