By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,292 Members | 1,360 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,292 IT Pros & Developers. It's quick & easy.

Object reference exception error for using Hashtable, why?

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.