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

  • Thread starter Thread starter William Mild
  • Start date Start date
W

William Mild

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
 
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
 
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?
 
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
 
Lucas,

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

Thanks,
Bill
 
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.ToString())

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.

--------------------
 
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" <[email protected]>
Sender: "KWOK" <[email protected]>
References: <[email protected]>
<[email protected]>
 
Back
Top