Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor Issue

 
Devin Henderson
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21129
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Devin:

Hint: Arrays are objects, too.

John.
 
Devin Henderson
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic