Server Error: Object reference not set

G

Guest

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.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:


Line 28: public void Upload_click(object sender, System.EventArgs e)
Line 29: {
Line 30: if (uploadFile.PostedFile != null)
Line 31: {
Line 32: string test = uploadFile.PostedFile.FileName;


Source File: c:\documents and settings\my
documents\myprojects\web\project\testwebapp\test.aspx.cs Line: 30

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an
object.]
TestWebApp.test.Upload_click(Object sender, EventArgs e) in c:\documents
and settings\my documents\myprojects\web\project\testwebapp\test.aspx.cs:30
System.Web.UI.WebControls.Button.OnClick(EventArgs e)

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()


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.PostedFile != 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.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace TestWebApp
{
/// <summary>
/// Summary description for test.
/// </summary>
public class test : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Upload;
protected System.Web.UI.HtmlControls.HtmlInputFile uploadFile;


private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

public void Upload_click(object sender, System.EventArgs e)
{
if (uploadFile.PostedFile != null)
{
string test = uploadFile.PostedFile.FileName;
}
}


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

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

}
#endregion
}
}


<%@ Page language="c#" Codebehind="test.aspx.cs" AutoEventWireup="false"
Inherits="TestWebApp.test" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>test</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.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,
 
B

Baski

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

HttpFileCollection HttpFiles = Request.Files;

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

{

HttpPostedFile _HttpPosted = HttpFiles;


if ( _HttpPosted.ContentLength > 0 )

{

string lineText = string.Empty;

using (StreamReader sr = new StreamReader(_HttpPosted.InputStream))

{

//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 said:
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.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:


Line 28: public void Upload_click(object sender, System.EventArgs e)
Line 29: {
Line 30: if (uploadFile.PostedFile != null)
Line 31: {
Line 32: string test = uploadFile.PostedFile.FileName;


Source File: c:\documents and settings\my
documents\myprojects\web\project\testwebapp\test.aspx.cs Line: 30

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an
object.]
TestWebApp.test.Upload_click(Object sender, EventArgs e) in c:\documents
and settings\my
documents\myprojects\web\project\testwebapp\test.aspx.cs:30
System.Web.UI.WebControls.Button.OnClick(EventArgs e)

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()


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.PostedFile != 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.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace TestWebApp
{
/// <summary>
/// Summary description for test.
/// </summary>
public class test : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Upload;
protected System.Web.UI.HtmlControls.HtmlInputFile uploadFile;


private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

public void Upload_click(object sender, System.EventArgs e)
{
if (uploadFile.PostedFile != null)
{
string test = uploadFile.PostedFile.FileName;
}
}


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

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

}
#endregion
}
}


<%@ Page language="c#" Codebehind="test.aspx.cs" AutoEventWireup="false"
Inherits="TestWebApp.test" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>test</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.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,
 
B

Baski

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

HttpFileCollection HttpFiles = Request.Files;

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

{

HttpPostedFile _HttpPosted = HttpFiles;


if ( _HttpPosted.ContentLength > 0 )

{

string lineText = string.Empty;

using (StreamReader sr = new StreamReader(_HttpPosted.InputStream))

{

//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.microsoft.com> wrote in message
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.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:


Line 28: public void Upload_click(object sender, System.EventArgs e)
Line 29: {
Line 30: if (uploadFile.PostedFile != null)
Line 31: {
Line 32: string test = uploadFile.PostedFile.FileName;


Source File: c:\documents and settings\my
documents\myprojects\web\project\testwebapp\test.aspx.cs Line: 30

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an
object.]
TestWebApp.test.Upload_click(Object sender, EventArgs e) in c:\documents
and settings\my
documents\myprojects\web\project\testwebapp\test.aspx.cs:30
System.Web.UI.WebControls.Button.OnClick(EventArgs e)

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()


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.PostedFile != 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.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace TestWebApp
{
/// <summary>
/// Summary description for test.
/// </summary>
public class test : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Upload;
protected System.Web.UI.HtmlControls.HtmlInputFile uploadFile;


private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

public void Upload_click(object sender, System.EventArgs e)
{
if (uploadFile.PostedFile != null)
{
string test = uploadFile.PostedFile.FileName;
}
}


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

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

}
#endregion
}
}


<%@ Page language="c#" Codebehind="test.aspx.cs" AutoEventWireup="false"
Inherits="TestWebApp.test" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>test</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.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,
 
G

Guest

Thanks Baski. I will give it a try.

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

HttpFileCollection HttpFiles = Request.Files;

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

{

HttpPostedFile _HttpPosted = HttpFiles;


if ( _HttpPosted.ContentLength > 0 )

{

string lineText = string.Empty;

using (StreamReader sr = new StreamReader(_HttpPosted.InputStream))

{

//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.microsoft.com> wrote in message
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.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:


Line 28: public void Upload_click(object sender, System.EventArgs e)
Line 29: {
Line 30: if (uploadFile.PostedFile != null)
Line 31: {
Line 32: string test = uploadFile.PostedFile.FileName;


Source File: c:\documents and settings\my
documents\myprojects\web\project\testwebapp\test.aspx.cs Line: 30

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an
object.]
TestWebApp.test.Upload_click(Object sender, EventArgs e) in c:\documents
and settings\my
documents\myprojects\web\project\testwebapp\test.aspx.cs:30
System.Web.UI.WebControls.Button.OnClick(EventArgs e)

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()


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.PostedFile != 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.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace TestWebApp
{
/// <summary>
/// Summary description for test.
/// </summary>
public class test : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Upload;
protected System.Web.UI.HtmlControls.HtmlInputFile uploadFile;


private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

public void Upload_click(object sender, System.EventArgs e)
{
if (uploadFile.PostedFile != null)
{
string test = uploadFile.PostedFile.FileName;
}
}


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

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

}
#endregion
}
}


<%@ Page language="c#" Codebehind="test.aspx.cs" AutoEventWireup="false"
Inherits="TestWebApp.test" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>test</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top