Thursday, March 22, 2012

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

I use VS.NET and yes I'm a beginner in ASP.NET.

Here is the problem:

in "%webroot%\components" is the folowing vb code file:

framework.vb
------------
Imports System
Imports System.Configuration
Imports System.Web
Imports System.Data
Imports System.Collections

Public Class framework
Public framework_name As String = "Hello"
Public framework_logo As String = ""
End Class
------------

Then in %webroot%\ is the main (test) page for the site:
default.aspx.vb (code behind file):
------------
Imports System.Web
Imports AIS3

Public Class _default
Inherits System.Web.UI.Page
Protected label1 As System.Web.UI.WebControls.Label
Protected maintitle As System.Web.UI.HtmlControls.HtmlGenericControl

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init

Dim _framework As framework =
CType(HttpContext.Current.Items("framework"), framework)

label1.Text = _framework.framework_name

maintitle.InnerText = "New title for page"

End Sub
End Class
------------

There is no code in default.aspx and VS.NET creates the namespace
"AIS3" automatically. I imported it in default.aspx.vb file but im not
sure that was really needed.

But... when i run the thing i get:

------------
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 26: Dim _framework As framework =
CType(HttpContext.Current.Items("framework"), framework)
Line 27:
Line 28: label1.Text = _framework.framework_name
Line 29:
Line 30: maintitle.InnerText = "New title for page"

Source File: c:\inetpub\wwwroot\AIS3\default.aspx.vb Line: 28

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an
object.]
AIS3._default.Page_Init(Object sender, EventArgs e) in
c:\inetpub\wwwroot\AIS3\default.aspx.vb:28
System.Web.UI.Control.OnInit(EventArgs e) +67
System.Web.UI.Control.InitRecursive(Control namingContainer) +241
System.Web.UI.Page.ProcessRequestMain() +174
-------------

Line 28 is marked

What am i doing wrong?

Thanku :)

PatrickDim _framework as new framework =
CType(HttpContext.Current.Items("framework"), framework)

"PatricQ" <patricq1@.yahoo.com> wrote in message
news:lft4309h49pe4tl6plotnfhkdk69vsbskj@.4ax.com...
> I use VS.NET and yes I'm a beginner in ASP.NET.
> Here is the problem:
> in "%webroot%\components" is the folowing vb code file:
> framework.vb
> ------------
> Imports System
> Imports System.Configuration
> Imports System.Web
> Imports System.Data
> Imports System.Collections
> Public Class framework
> Public framework_name As String = "Hello"
> Public framework_logo As String = ""
> End Class
> ------------
>
>
> Then in %webroot%\ is the main (test) page for the site:
> default.aspx.vb (code behind file):
> ------------
> Imports System.Web
> Imports AIS3
>
> Public Class _default
> Inherits System.Web.UI.Page
> Protected label1 As System.Web.UI.WebControls.Label
> Protected maintitle As System.Web.UI.HtmlControls.HtmlGenericControl
>
> Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles MyBase.Init
> Dim _framework As framework =
> CType(HttpContext.Current.Items("framework"), framework)
> label1.Text = _framework.framework_name
> maintitle.InnerText = "New title for page"
> End Sub
> End Class
> ------------
>
> There is no code in default.aspx and VS.NET creates the namespace
> "AIS3" automatically. I imported it in default.aspx.vb file but im not
> sure that was really needed.
> But... when i run the thing i get:
> ------------
> 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 26: Dim _framework As framework =
> CType(HttpContext.Current.Items("framework"), framework)
> Line 27:
> Line 28: label1.Text = _framework.framework_name
> Line 29:
> Line 30: maintitle.InnerText = "New title for page"
>
> Source File: c:\inetpub\wwwroot\AIS3\default.aspx.vb Line: 28
> Stack Trace:
>
> [NullReferenceException: Object reference not set to an instance of an
> object.]
> AIS3._default.Page_Init(Object sender, EventArgs e) in
> c:\inetpub\wwwroot\AIS3\default.aspx.vb:28
> System.Web.UI.Control.OnInit(EventArgs e) +67
> System.Web.UI.Control.InitRecursive(Control namingContainer) +241
> System.Web.UI.Page.ProcessRequestMain() +174
> -------------
>
> Line 28 is marked
>
> What am i doing wrong?
> Thanku :)
> Patrick
Thank you,

When I tried it, it said "end of statement expected", it doesnt like
the = Ctype(... part.

Patrick

On Tue, 17 Feb 2004 15:13:55 -0600, "A Lonely Programmer"
<SpamaLinuxUser@.NoSpamForMe.com> wrote:

> Dim _framework as new framework =
>CType(HttpContext.Current.Items("framework"), framework)
>
>"PatricQ" <patricq1@.yahoo.com> wrote in message
>news:lft4309h49pe4tl6plotnfhkdk69vsbskj@.4ax.com...
>> I use VS.NET and yes I'm a beginner in ASP.NET.
>>
>> Here is the problem:
>>
>> in "%webroot%\components" is the folowing vb code file:
>>
>> framework.vb
>> ------------
>> Imports System
>> Imports System.Configuration
>> Imports System.Web
>> Imports System.Data
>> Imports System.Collections
>>
>> Public Class framework
>> Public framework_name As String = "Hello"
>> Public framework_logo As String = ""
>> End Class
>> ------------
>>
>>
>>
>>
>>
>> Then in %webroot%\ is the main (test) page for the site:
>> default.aspx.vb (code behind file):
>> ------------
>> Imports System.Web
>> Imports AIS3
>>
>>
>> Public Class _default
>> Inherits System.Web.UI.Page
>> Protected label1 As System.Web.UI.WebControls.Label
>> Protected maintitle As System.Web.UI.HtmlControls.HtmlGenericControl
>>
>>
>> Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
>> System.EventArgs) Handles MyBase.Init
>>
>> Dim _framework As framework =
>> CType(HttpContext.Current.Items("framework"), framework)
>>
>> label1.Text = _framework.framework_name
>>
>> maintitle.InnerText = "New title for page"
>>
>> End Sub
>> End Class
>> ------------
>>
>>
>> There is no code in default.aspx and VS.NET creates the namespace
>> "AIS3" automatically. I imported it in default.aspx.vb file but im not
>> sure that was really needed.
>>
>> But... when i run the thing i get:
>>
>> ------------
>> 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 26: Dim _framework As framework =
>> CType(HttpContext.Current.Items("framework"), framework)
>> Line 27:
>> Line 28: label1.Text = _framework.framework_name
>> Line 29:
>> Line 30: maintitle.InnerText = "New title for page"
>>
>>
>> Source File: c:\inetpub\wwwroot\AIS3\default.aspx.vb Line: 28
>>
>> Stack Trace:
>>
>>
>> [NullReferenceException: Object reference not set to an instance of an
>> object.]
>> AIS3._default.Page_Init(Object sender, EventArgs e) in
>> c:\inetpub\wwwroot\AIS3\default.aspx.vb:28
>> System.Web.UI.Control.OnInit(EventArgs e) +67
>> System.Web.UI.Control.InitRecursive(Control namingContainer) +241
>> System.Web.UI.Page.ProcessRequestMain() +174
>> -------------
>>
>>
>>
>> Line 28 is marked
>>
>>
>> What am i doing wrong?
>>
>> Thanku :)
>>
>> Patrick
Never mind, I found it ;)

in global.asax ive put the folowing:

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As
EventArgs)
' Fires at the beginning of each request
Context.Items.Add("framework", New framework)
End Sub

The sub New() fills the framework class. But the Context.Items.Add
adds it to the HttpContext.Current.Items thingy...

PatricQ

On Tue, 17 Feb 2004 21:22:44 +0100, PatricQ <patricq1@.yahoo.com>
wrote:

>I use VS.NET and yes I'm a beginner in ASP.NET.
>Here is the problem:
>in "%webroot%\components" is the folowing vb code file:
>framework.vb
>------------
>Imports System
>Imports System.Configuration
>Imports System.Web
>Imports System.Data
>Imports System.Collections
>Public Class framework
> Public framework_name As String = "Hello"
> Public framework_logo As String = ""
>End Class
>------------
>
>
>Then in %webroot%\ is the main (test) page for the site:
>default.aspx.vb (code behind file):
>------------
>Imports System.Web
>Imports AIS3
>
>Public Class _default
>Inherits System.Web.UI.Page
>Protected label1 As System.Web.UI.WebControls.Label
>Protected maintitle As System.Web.UI.HtmlControls.HtmlGenericControl
>
>Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
>System.EventArgs) Handles MyBase.Init
> Dim _framework As framework =
>CType(HttpContext.Current.Items("framework"), framework)
> label1.Text = _framework.framework_name
> maintitle.InnerText = "New title for page"
> End Sub
>End Class
>------------
>
>There is no code in default.aspx and VS.NET creates the namespace
>"AIS3" automatically. I imported it in default.aspx.vb file but im not
>sure that was really needed.
>But... when i run the thing i get:
>------------
>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 26: Dim _framework As framework =
>CType(HttpContext.Current.Items("framework"), framework)
>Line 27:
>Line 28: label1.Text = _framework.framework_name
>Line 29:
>Line 30: maintitle.InnerText = "New title for page"
>
>Source File: c:\inetpub\wwwroot\AIS3\default.aspx.vb Line: 28
>Stack Trace:
>
>[NullReferenceException: Object reference not set to an instance of an
>object.]
> AIS3._default.Page_Init(Object sender, EventArgs e) in
>c:\inetpub\wwwroot\AIS3\default.aspx.vb:28
> System.Web.UI.Control.OnInit(EventArgs e) +67
> System.Web.UI.Control.InitRecursive(Control namingContainer) +241
> System.Web.UI.Page.ProcessRequestMain() +174
>-------------
>
>Line 28 is marked
>
>What am i doing wrong?
>Thanku :)
>Patrick

0 comments:

Post a Comment