By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,573 Members | 1,924 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,573 IT Pros & Developers. It's quick & easy.

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

P: n/a
I must be getting brain fried. I can't see the error.
Create a new web form with the following code begind:

Public Class test
Inherits System.Web.UI.Page

Public Class ReportCardData

Public Structure Attend
Dim DaysTardy As Double
Dim DaysAbsent As Double
Dim ExcessiveAbsences As Boolean
End Structure

Public Structure Grade
Dim Value As String
Dim GradeableItemID As String
Dim MarkingPeriod As Integer
End Structure

Friend StudentID As Double
Friend GradeLevel As String
Friend Teacher As String
Friend Student As String
Friend SchoolName As String
Friend Principal As String
Public Grades() As Grade
Public Attendance() As Attend
Friend Comments() As String
Friend SeeAttachedNarrative() As Boolean
Friend Promoted As Boolean
Friend Assigned As Boolean
Friend Year As String

End Class

Private rcData As New ReportCardData

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private
Sub InitializeComponent()

End Sub

'NOTE: The following placeholder declaration is
required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As
System.Object

Private Sub Page_Init(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web
Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load

rcData.Attendance(1).DaysAbsent = 0

End Sub

End Class
_______________________________________

The "System.NullReferenceException: Object reference not
set to an instance of an object." error occurs on line
that says rcDate.Attendance(1).DaysAbsent=0.

What do I need to do to get this running?

Thanks,
Bill
Nov 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"William Mild" <wm***@shemsystems.com> wrote in news:090501c38148$32ec6f10
$a*******@phx.gbl:
rcData.Attendance(1).DaysAbsent = 0

What do I need to do to get this running?


You are trying to access an element in an array. Where is this array
dimensioned?

Somewhere (presumably in the constructor for your ReportCardData class) you
need to allocate the array:

ReDim Me.Attendance(arrayupperbound) as Attend

Chris
Nov 20 '05 #2

P: n/a
"William Mild" <wm***@shemsystems.com> wrote in news:090501c38148$32ec6f10
$a*******@phx.gbl:
The "System.NullReferenceException: Object reference not
set to an instance of an object." error occurs on line
that says rcDate.Attendance(1).DaysAbsent=0.

What do I need to do to get this running?


Did you initialize the variable rcDate?

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 20 '05 #3

P: n/a
Chris,

Thanks for the suggesion, but when I put "ReDim
Me.Attendance(arrayupperbound) as Attend" into a
constructor (you mean, Sub New(), right?), then I
get "'Redim' statements can no longer be used to declare
array varialbes." If I change it from 'Redim' to 'Dim',
it tells me that the Me keyword is not a valid identifier.

If I take the "Me" out, then we have "Dim Attendance(4)
As Attend" in the consturctor. I am now back
to "System.NullReferenceException: Object reference not
set to an instance of an object."

Any other suggestions? I provided all my code in the
original post for this. Are you able to get a web form
to run without an error?

Thanks,
Bill

Nov 20 '05 #4

P: n/a
Lucas,

I think the answer is yes. Isn't "Private rcData As New
ReportCardData" (see initial post) a valid initialization
of rdData?

Thanks,
Bill
Nov 20 '05 #5

P: n/a
Hi Chris,

I changed the code as following.
Public Class test
Inherits System.Web.UI.Page

Public Class ReportCardData

Public Structure Attend
Dim DaysTardy As Double
Dim DaysAbsent As Double
Dim ExcessiveAbsences As Boolean
End Structure

Public Structure Grade
Dim Value As String
Dim GradeableItemID As String
Dim MarkingPeriod As Integer
End Structure

Friend StudentID As Double
Friend GradeLevel As String
Friend Teacher As String
Friend Student As String
Friend SchoolName As String
Friend Principal As String
Public Grades() As Grade
Public Attendance(2) As Attend
Friend Comments() As String
Friend SeeAttachedNarrative() As Boolean
Friend Promoted As Boolean
Friend Assigned As Boolean
Friend Year As String

End Class

Private rcData As New ReportCardData

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form(Designer)
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

rcData.Attendance(1).DaysAbsent = 100
Response.Write(rcData.Attendance(1).DaysAbsent.ToS tring())

End Sub

End Class

The code above works for me.
You may try to set a breakpoint at the line below
rcData.Attendance(1).DaysAbsent = 100
Press F5 to run the project after the program stoped at the breakpoint,
Ctrl+Alt+W,1 to open the WatchWindow and input rcData to observe the
Attendance under the rcData to see if it is Nothing.

I look forward to hearing from you.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
Content-Class: urn:content-classes:message
From: "William Mild" <wm***@shemsystems.com>
Sender: "William Mild" <wm***@shemsystems.com>
References: <09****************************@phx.gbl> <Xn**********************************@207.46.248.1 6>Subject: Re: System.NullReferenceException: Object reference not set to an instance of an objDate: Mon, 22 Sep 2003 19:25:15 -0700
Lines: 21
Message-ID: <00****************************@phx.gbl>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
Thread-Index: AcOBeey78UYhkGkdSk+d6k0TxzZB3A==
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Newsgroups: microsoft.public.dotnet.languages.vb
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:140480
NNTP-Posting-Host: TK2MSFTNGXA08 10.40.1.160
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

Chris,

Thanks for the suggesion, but when I put "ReDim
Me.Attendance(arrayupperbound) as Attend" into a
constructor (you mean, Sub New(), right?), then I
get "'Redim' statements can no longer be used to declare
array varialbes." If I change it from 'Redim' to 'Dim',
it tells me that the Me keyword is not a valid identifier.

If I take the "Me" out, then we have "Dim Attendance(4)
As Attend" in the consturctor. I am now back
to "System.NullReferenceException: Object reference not
set to an instance of an object."

Any other suggestions? I provided all my code in the
original post for this. Are you able to get a web form
to run without an error?

Thanks,
Bill


Nov 20 '05 #6

P: n/a
Hi k_kachi,

Can you follow my last post to debug your project?
The System.NullReferenceException is usually caused by you refer to a
object which is not initialized.
e.g.
The code below
Dim c As New Object
Console.WriteLine(c.GetType().ToString())
will not get the error message.
while
Dim c As Object
Console.WriteLine(c.GetType().ToString())
will get the error message.

Did I answer your question?

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
Content-Class: urn:content-classes:message
From: "KWOK" <k_*****@hotmail.com>
Sender: "KWOK" <k_*****@hotmail.com>
References: <09****************************@phx.gbl> <Xn**********************************@207.46.248.1 6>
<00****************************@phx.gbl>
<ce**************@cpmsftngxa06.phx.gbl>Subject: Re: System.NullReferenceException: Object reference not set to an instance of an objDate: Mon, 22 Sep 2003 23:50:27 -0700
Lines: 40
Message-ID: <0d****************************@phx.gbl>
MIME-Version: 1.0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Newsreader: Microsoft CDO for Windows 2000
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
Thread-Index: AcOBnvi67pNTObFiRPSXfu/PwN/N6g==
Newsgroups: microsoft.public.dotnet.languages.vb
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:140498
NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

Hi, Peter Hunag

I meet this error too. But I re-Run the program - it
sometimes able to run, it sometimes shows the same error.
I dont know what's wrong about my program.
The System.NullReferenceException point at different
objects that I checked these codes are OK.

So, Do you know what is the main problem that cause this
System.NullReferenceException.

Thanks,
KWOK
Chris,

Thanks for the suggesion, but when I put "ReDim
Me.Attendance(arrayupperbound) as Attend" into a
constructor (you mean, Sub New(), right?), then I
get "'Redim' statements can no longer be used to declare
array varialbes." If I change it from 'Redim' to 'Dim',
it tells me that the Me keyword is not a valididentifier.
If I take the "Me" out, then we have "Dim Attendance(4)
As Attend" in the consturctor. I am now back
to "System.NullReferenceException: Object reference not
set to an instance of an object."

Any other suggestions? I provided all my code in the
original post for this. Are you able to get a web form
to run without an error?

Thanks,
Bill


.


Nov 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.