473,386 Members | 1,819 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,386 software developers and data experts.

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.Collections;
public class SamplesHashtable
{
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 4875
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**************@tk2msftngp13.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.Collections;
public class SamplesHashtable
{
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**************@tk2msftngp13.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.Collections;
public class SamplesHashtable
{
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
NullReferenceException since q[0] hasn't been initialized to an instance of
Q.

Ken
"A. Burch" <a1****@msn.com> wrote in message
news:uC**************@tk2msftngp13.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.Collections;
public class SamplesHashtable
{
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
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...
0
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...
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: 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...
3
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...
3
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...
6
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...
8
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...
3
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...

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.