Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2005

Tip: Looking for answers? Try searching our database.

Getting this Error

Thread view: 
Gary - 08 Mar 2005 16:13 GMT
Exception in thread "main" java.lang.NullPointerException
       at Bookstore.openInputStream(Bookstore.java:80)
       at Bookstore.main(Bookstore.java:55)

Any Ideas?
John Harlow - 08 Mar 2005 20:42 GMT
> 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!
Dan Nuttle - 11 Mar 2005 02:21 GMT
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?


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.