473,408 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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

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
6 22187
"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
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Terrence | last post by:
I am doing some of the C# walkthroughs to transition from VB to C#. When I try to execute static void Main() { Aplication.Run(new Form1()) } I raise a 'System.NullReferenceException" in...
7
by: mike p. | last post by:
I have a docbook xml file, and am using standard docbook 1.61.3 xsl stylesheets to do xhtml transform. Transform works fine when using MSXML. When I try to do the following using asp.net 1.1: ...
0
by: muralidharan | last post by:
WebForm1.aspx Code: <%@ Register TagPrefix="ComponentArt" Namespace="ComponentArt.Web.UI" Assembly="ComponentArt.Web.UI" %> <ComponentArt:TreeView id="TreeView1" Height="520"...
5
by: Vitling | last post by:
For no apparent reason, a NullReference exception is thrown in system.dll (System.Net.Sockets.OverlappedAsyncResult.CompletionPortCallback). Since I only get a disassembly from Visual Studio, it...
2
by: Raed Sawalha | last post by:
i have a windows form(Main) with listview, when click an item in listview i open other window form (Sub) which generate the selected item from parent window in as treeview items when click any item...
1
by: msnews.microsoft.com | last post by:
I'm trying to fill an array of objects but when I add the first object I get a NullReferenceException. ----------------------------------------------------------------------------...
4
by: Terry Mulvany | last post by:
I have a 'BasePage' (BasePage.cs) derived from System.Web.UI.Page that all my pages inherit from. I need to set some properties (either in the OnInit or constructor) based on a potential...
2
by: sxiao | last post by:
Hi, there I got a NullReferenceException when there are more than one users trying to open the same page at the same time. The senerio is: Two users logged into the web application using the...
3
by: Brano | last post by:
HI all, I have a problem i have a web application that was working fine and this morning when i run it and click on a button that does Reponse.Redirect to a page i get this error : Server...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.