|
Re: I am getting a nullpointer exception
|
Posted: Aug 14, 2003 1:03 PM
|
|
Hi!
Java works with references. When you create a variable, normally you create a reference to an object, such as an Integer. So you could say the variable points to the object. You might have done something like this:
HashMap h; h.put("name", "henk");
or
HashMap h = null; h.put("name", "henk");
Every object that is declared in java refers to null, you could say points to nothing. So when you execute above code you use a reference to null trying to access the objects' put method. This causes the java interpreter to throw a NullPointerException. Now for the solution: Declaring means only: saying to the computer that your variable exists, not what it is. The fix for the above solution is not only declaring but also initializing your variable:
HashMap h = new HashMap(); h.put("name", "Henk");
If this does not help, send the code snippet that causes the Exception. regards
Henk
> Hi so I am new to this forum and still a student in > computer science ... > So my question is what exactly is a nullpointer exception > and when did i get one > I wanted to create a hash table where i could put some > stuff but unfortunately when i execute my code i ma > getting this error. > I appreciate any help > Gery
|
|