469,296 Members | 2,147 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,296 developers. It's quick & easy.

Server Error: Object reference not set

SAL
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.a spx.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.a spx.cs:30
System.Web.UI.WebControls.Button.OnClick(EventArgs e)

System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection 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,

Oct 6 '06 #1
3 2352
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[i];
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" <SA*@discussions.microsoft.comwrote in message
news:37**********************************@microsof t.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.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.a spx.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.a spx.cs:30
System.Web.UI.WebControls.Button.OnClick(EventArgs e)

System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String
eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection 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,

Oct 6 '06 #2

"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[i];
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.comwrote in message
news:37**********************************@microsof t.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.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.a spx.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.a spx.cs:30
System.Web.UI.WebControls.Button.OnClick(EventArgs e)

System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String
eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection 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,

Oct 6 '06 #3
SAL
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

HttpFileCollection HttpFiles = Request.Files;

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

{

HttpPostedFile _HttpPosted = HttpFiles[i];
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.comwrote in message
news:37**********************************@microsof t.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.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.a spx.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.a spx.cs:30
System.Web.UI.WebControls.Button.OnClick(EventArgs e)

System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String
eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection 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,


Oct 6 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Daniel Rimmelzwaan | last post: by
2 posts views Thread by James Wallace | last post: by
3 posts views Thread by Web Team | last post: by
4 posts views Thread by David Lozzi | last post: by
2 posts views Thread by Richard Coltrane | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.