473,774 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Object reference exception error for using Hashtable, why?

I posted early about Hashtable use and the shallow/deep copy and storing a
reference answered that question.

I've got a larger implementation of this, but am not sure why I'm getting
this error. Object reference not set to an instance of an object. I don't
know what I'm missing. What needs to be fixed for this implementation to
work?

using System;
using System.Collecti ons;
public class SamplesHashtabl e
{
static Hashtable HT = new Hashtable();
public static Q[] qrec = new Q[2];
public static void Main()
{
qrec[0].symbol = "HI";
qrec[0].price = 4.0;
HT.Add("First", qrec[0]);
qrec[1].symbol = "BYE";
qrec[1].price = 5.0;
HT.Add("Second" , qrec[1]);
qrec[0] = (Q)HT["First"];
qrec[0].price = qrec[0].price - 1.0;
}
public class Q
{
public string symbol;
public double price;
}
}
Nov 16 '05 #1
3 4890
qrec[0] contains an empty reference to a Q object because it has not been
initialized with a new instance. You need to create an instance of Q and put
that into qrec[0] so that it contains an instance of Q, then you can
dereference it they way you are.

eg.
qrec[0] = new Q();

The same goes for qrec[1].

--
John Wood
EMail: first name, dot, last name, at priorganize.com

"A. Burch" <a1****@msn.com > wrote in message
news:uC******** ******@tk2msftn gp13.phx.gbl...
I posted early about Hashtable use and the shallow/deep copy and storing a
reference answered that question.

I've got a larger implementation of this, but am not sure why I'm getting
this error. Object reference not set to an instance of an object. I don't
know what I'm missing. What needs to be fixed for this implementation to
work?

using System;
using System.Collecti ons;
public class SamplesHashtabl e
{
static Hashtable HT = new Hashtable();
public static Q[] qrec = new Q[2];
public static void Main()
{
qrec[0].symbol = "HI";
qrec[0].price = 4.0;
HT.Add("First", qrec[0]);
qrec[1].symbol = "BYE";
qrec[1].price = 5.0;
HT.Add("Second" , qrec[1]);
qrec[0] = (Q)HT["First"];
qrec[0].price = qrec[0].price - 1.0;
}
public class Q
{
public string symbol;
public double price;
}
}

Nov 16 '05 #2
Q is a class and therefore a reference type. When you create an array of
reference types you get an array with all the members set to null. You have
to populate each member (calling Initialize() on the array will NOT work for
reference types).

So Q[0]==null until you go Q[0]=new Q();

Richard

--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
"A. Burch" <a1****@msn.com > wrote in message
news:uC******** ******@tk2msftn gp13.phx.gbl...
I posted early about Hashtable use and the shallow/deep copy and storing a
reference answered that question.

I've got a larger implementation of this, but am not sure why I'm getting
this error. Object reference not set to an instance of an object. I don't
know what I'm missing. What needs to be fixed for this implementation to
work?

using System;
using System.Collecti ons;
public class SamplesHashtabl e
{
static Hashtable HT = new Hashtable();
public static Q[] qrec = new Q[2];
public static void Main()
{
qrec[0].symbol = "HI";
qrec[0].price = 4.0;
HT.Add("First", qrec[0]);
qrec[1].symbol = "BYE";
qrec[1].price = 5.0;
HT.Add("Second" , qrec[1]);
qrec[0] = (Q)HT["First"];
qrec[0].price = qrec[0].price - 1.0;
}
public class Q
{
public string symbol;
public double price;
}
}

Nov 16 '05 #3
Keep in mind that allocating an array of reference-types (e.g. "new Q[2]")
just creates an array with null in each of its elements. You need to
individually allocate the elements and assign them into the array. Because
you haven't done that, the first line of Main() generates a
NullReferenceEx ception since q[0] hasn't been initialized to an instance of
Q.

Ken
"A. Burch" <a1****@msn.com > wrote in message
news:uC******** ******@tk2msftn gp13.phx.gbl...
I posted early about Hashtable use and the shallow/deep copy and storing a
reference answered that question.

I've got a larger implementation of this, but am not sure why I'm getting
this error. Object reference not set to an instance of an object. I don't
know what I'm missing. What needs to be fixed for this implementation to
work?

using System;
using System.Collecti ons;
public class SamplesHashtabl e
{
static Hashtable HT = new Hashtable();
public static Q[] qrec = new Q[2];
public static void Main()
{
qrec[0].symbol = "HI";
qrec[0].price = 4.0;
HT.Add("First", qrec[0]);
qrec[1].symbol = "BYE";
qrec[1].price = 5.0;
HT.Add("Second" , qrec[1]);
qrec[0] = (Q)HT["First"];
qrec[0].price = qrec[0].price - 1.0;
}
public class Q
{
public string symbol;
public double price;
}
}

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
10548
by: Pkpatel | last post by:
Hi, I keep getting this error every time I try to load crystalreportviewer on a webform with a dataset. Here is the error: -------------------------------------------------------- Server Error in '/Cr_Dataset' Application. ----------------------------------------------------------- ---------------------
0
2230
by: Kenneth Baltrinic | last post by:
I am getting the following error when deserializing an object that has a couple of dozen dependant objects in its object graph. Anyone who can suggest where I might begin to look to resolve problem I would greatly in debted to. Serializing the object works fine. When I try to deserialize it, I get the following error: A first chance exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll Additional...
0
9781
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" AutoPostBackOnNodeMove="false" DragAndDropEnabled="true" NodeEditingEnabled="False" KeyboardEnabled="true" CssClass="TreeView" NodeCssClass="TreeNode" SelectedNodeCssClass="SelectedTreeNode" HoverNodeCssClass="HoverTreeNode" NodeEditCssClass="NodeEdit"
5
11308
by: Fernando Cacciola | last post by:
Hi, I need to produce an ID number that uniquely identifies a given object instance. (If the object is a boxed value, well, it won't really matter, but for _shared_ reference-types I really need a unique ID) What's the most efficient way to do this?
3
9695
by: Fred | last post by:
I'm trying to build a hashtable and a arraylist as object value I'm not able to retrieve stored object from the hashtable. Hashtable mp = new Hashtable(); // THE HASHTABLE ArrayList atemp = new ArrayList(); // THE ARRAY StreamWriter sw = new StreamWriter(@"C:\temp\fred.html");
3
455
by: nemo | last post by:
Hi, My application works fine on the localhost but spits this error as soon as I put it on the server. I know this error occurs when an object has not been instantiated prior to a reference, but why shouldn't it happen in the localhost? Here's what the the whole error msg looks like:
6
13422
by: Scott M. Lyon | last post by:
As I mentioned in my other post, I'm attempting to, using COM Interop so I can update existing VB6 code to (for several specific functions) return a Hashtable from a .NET library. I've had very little luck processing the Hashtable itself in VB6 (I can add a reference to the project so it knows what a Hashtable is, but I'm not having much luck looping through all objects in the Hashtable), so I decided to try a different idea.
8
14758
by: fniles | last post by:
I have a collection inside a class, sometimes when I add to the collection, I get the error "At least one object must implement IComparable". What does the error mean ? Thanks. Public Class SessionClass Private Quotes As Collection = New Collection Sub NewQuote(ByVal Message As String) Dim swError As StreamWriter
3
2883
by: Michel Couche | last post by:
Hello, I have an ASP.Net application that uses the Wizard control to build a newsletter. There are three steps in the wizard. The customer's specific design data are loaded from a database in step 1 of the wizard and saved into a session variable. In step 2, when the user clicks on a button, I load the design data from the session variable and these are saved for further handling in a HashTable. The HashTable is defined in the...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10267
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10040
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7463
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6717
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4012
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.