This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Constructor Issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructor Issue" Watch "Constructor Issue" New topic
Author

Constructor Issue

Devin Henderson
Greenhorn

Joined: Dec 01, 2009
Posts: 16
Hey all.

I am writing a program for a class and I am having a weird issue with a constructor.

In my test program, I am trying to do the following:



The Room class is as follows:



Now the problem is that the first Room constructor in the test program is bringing back a NullPointerException while the second constructor is working just fine and returning the "Null Room Name" exception message. When I try to step through the test program, I get a "ClassNotFoundException" on the first Room constructor.

Any idea what the heck is going on?

Thanks!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Yes. However you have put code in your catch-blocks which hides most of the exception's useful information. Replace that code by this:

and then you will get the full stack trace of the exception which was thrown. In particular you will see the line number from where the exception was thrown. That should help you zero in on the problem.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Devin:

Have you created a class for your RoomException? Also, you should be throwing NullPointerExceptions, or chain the NPE to a RoomException.

John.
Devin Henderson
Greenhorn

Joined: Dec 01, 2009
Posts: 16
I figured out what is throwing the NPE:



exitRooms is an instance variable that is set at the top of the class:



I know that I am missing a step in initializing it. Just cannot figure out the problem.

I am trying to set each of the exitRooms (limit the creation to the number of MAX_EXITS which is 4) to null.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Devin:

Hint: Arrays are objects, too.

John.
Devin Henderson
Greenhorn

Joined: Dec 01, 2009
Posts: 16
I am an idiot...

exitRooms = new Room[MAX_EXITS];
for(int i=0; i < MAX_EXITS; i++)
exitRooms[i] = null;

Forgot to initialize exitRooms...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

You don't need to explicitly set all array elements to null. Array elements default to 0 for primitive numeric arrays, '\0' for char arrays, false for boolean arrays and null for object arrays. After calling "exitRooms = new Room[MAX_EXITS];" all elements are already null.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
 
subject: Constructor Issue