473,244 Members | 1,824 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,244 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 22173
"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...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.