> Exception in thread "main" java.lang.NullPointerException
> at Bookstore.openInputStream(Bookstore.java:80)
> at Bookstore.main(Bookstore.java:55)
>
> Any Ideas?
You are accessing an object in line 80 of Bookstore.java which hasn't been
initialized.
sgt_sock - 09 Mar 2005 03:05 GMT
Is the variable in line 80 a local variable? If so, local variables
don't initialize themselves so you have to initialize it manually. Hope
this helps!
In response to the other answers, technically it's not a matter of an object
being "initialized." For instance, consider two examples:
MyClass a;
MyClass b = null;
a.doSomething();
b.doSomething();
b has been initialized, though it's just been set to null. The compiler
will never complain that b might not be initialized. a has never been
initialized. So if you write code where a might be accessed at any point,
the code will not compile. (I think you can use a switch to force
compilation, but that's not wise.)
OK, so what is a NullPointerException? Ah, the good old days of being a
Java beginner. I remember when I too did not understand what this meant,
and I asked a reputed Java "expert" what was going on. He looked kind of
nervous, and pulled down a book on Java and told me to look it up. Ha!
Some expert.
Anyway, when an object is null, it means it hasn't yet been instantiated.
(Which is a different thing from being initialized.) Think of it this way.
A variable can be thought of as a mask. If you say, "Hello there, how are
you?" the mask will just sit their silently. Now, if you instantiate the
variable, it's like a person wearing the mask. Now the mask (the person,
actually) can respond to your questions. So:
StringBuffer a = null;
a.append("a");
Will result in a NullPointerException, because a is just the "mask." On the
other hand:
StringBuffer a = new StringBuffer; //Put a person behind the mask
a.append("123ABC");
will not result in a NullPointerException.
> Exception in thread "main" java.lang.NullPointerException
> at Bookstore.openInputStream(Bookstore.java:80)
> at Bookstore.main(Bookstore.java:55)
>
> Any Ideas?