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

P

PatricQ

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
 
A

A Lonely Programmer

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

PatricQ

Thank you,

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

Patrick



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


PatricQ said:
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
 
P

PatricQ

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

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
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top