• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Constructor Issue

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Marshal
Posts: 27540
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Devin:

Hint: Arrays are objects, too.

John.
 
Devin Henderson
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Sheriff
Posts: 22716
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic