Null pointer exception....very annoying.
Right well i'm trying to use my method checking whether my Stack is empty or not, everything compiles ok but i get a null pointer exception when i call the method in my game(my program is a game).
publicboolean isEmpty()
{
if(rooms.empty() ==true){
returntrue;
}
else
{
returnfalse;
i get the null pointer on "if(rooms.empty() == true) {
Any help please? I'm a novice. I can post more code if necessary.
Thanks.
[892 byte] By [
Capsuda] at [2007-11-26 20:31:19]

rooms must be null, trace throught the code prior to that method call to find out where rooms is initialized.
The "rooms" variable is null when you execute that line.
public boolean isEmpty() {
return (rooms == null) || (rooms.empty());
}
~
> if(rooms.empty() == true) {
>return true;
>}
>else
>{
> return false;
Other than the check for null case, it's a good thing the boolean type doesn't contain more than 2 values, otherwise if it did I guess this guy would have written something like this:
if (rooms.empty() == boolVal1)
return boolVal1;
else if (rooms.empty() == boolVal2)
return boolVal2;
else if (rooms.empty() == boolVal3)
return boolVal3;
else if (rooms.empty() == boolVal4)
return boolVal4;
// else ad nauseum until values are covered
instead of simply:
return rooms.empty();
Some people just like to do things the hard way I guess.
In my instance variables at the top i've:private Stack <Room> rooms;That correct?
return (rooms == null) || (rooms.empty());That line of code may be fine, but it may also be hiding an error.Is rooms allowed to to null?
> In my instance variables at the top i've:>> private Stack <Room> rooms;> > That correct?1/2 the story. Where do you write something like:rooms = new Stack < Room > ();
> In my instance variables at the top i've:>> ivate Stack <Room> rooms;> > That correct?Depends. Did you ever create a Stack via the new keyword? I guess you did not, so rooms is null.
> That line of code may be fine, but it may also be
> hiding an error.
> Is rooms allowed to to null?
It is in my case. ;o)
In all seriousness, that's a design decision, and can easily be mitigated with a guard clause.if (rooms == null) throw new IllegalStateException("Rooms is null, but WHY?!?!?");
return rooms.empty();
Of course, one can arrange for rooms to not be null in other ways that don't involve a guard clause as well, but those can be a good exercise for the reader. Hope this helps clarify. Good point, Doctor. :o)
~
Correct i haven't created a Stack via the new keyword.Where should that be? In the same class as the previous instance varialbe i posted or what?
> Correct i haven't created a Stack via the new
> keyword.
>
> Where should that be? In the same class as the
> previous instance varialbe i posted or what?
In the constructor for the class where that variable exists is probably the best place.
> Correct i haven't created a Stack via the new
> keyword.
>
> Where should that be? In the same class as the
> previous instance varialbe i posted or what?
Since you're being coy with your source code one can't say for sure,
but dollars to doughnuts, this should work:
private Stack <Room> rooms = new Stack < Room > ();
Ok thanks guys, i put in " rooms = new Stack < Room > (); " into my constructor in that class and it fixed that problem.
In another class i'm running this method,
**
* This method will allow you to go back through rooms in the game
*/
private Room backRoom()
{
if(player.isEmpty() == true){
System.out.println("You didn't go into any previous rooms");
}else{
Room aRoom = player.popRoom();
Room currentRoom = player.getCurrentRoom();
player.enterRoom(aRoom);
System.out.println(player.getCurrentRoom().getLongDescription());
}return null;
This method allows me to go back to previous rooms in my game by calling this method. its not working fully yet because even if i call the back method after i've been in a few rooms it prints out 'You didn't go into any previous rooms', even though i clearly have. I think it might be something to do with this
/**
* Removes the object at the top of this stack and returns that object as the value of this function
*/
public Room popRoom()
{
Room aRoom = rooms.pop();
return aRoom;
}
Any ideas you guys?
backRoom always return null. Is that a problem?
Why does it always return null?
> This method allows me to go back to previous rooms in
> my game by calling this method. its not working fully
> yet because even if i call the back method after i've
> been in a few rooms it prints out 'You didn't go into
> any previous rooms', even though i clearly have. I
> think it might be something to do with this
No, it has to do with player.isEmpty() returning true. That's why backRoom prints "You didn't go into any previous rooms". The call to player.popRoom() never gets called. Look into why player.isEmpty() returns true when you call that method.
> Why does it always return null?
Because you have
return null;
at the end of the method. No matter what happens in the if/else, you get to that statement.
I think you need to go back and study the basics a little more. Here's a good place to start: http://java.sun.com/docs/books/tutorial/
By the way, I would write it
if(player.isEmpty()){
...
}
and
public Room popRoom() {
return rooms.pop();
}
But it sounds to me like you are never *pushing* a room onto the stack.
Check that rooms get pushed somewhere and that you execute that code first.
Well yeah i thought it had something to do with it returning true aswell, so i changed it to 'false' and the error pointed at this
public Room popRoom()
{
Room aRoom = rooms.pop();
return aRoom;
So what could i do?
What does "and the error pointed at this" mean? What was the exact error? EmptyStackException?
when i changed if(player.isEmpty() == true){ toif(player.isEmpty() == false){yeah the error is EmptyStackException.I'd love to know what that means and how i can fix it?
and it points at thisRoom aRoom = rooms.pop();which is in another class
> when i changed
> if(player.isEmpty() == true){
> to
> if(player.isEmpty() == false){
>
> yeah the error is EmptyStackException.
>
> I'd love to know what that means and how i can fix it?
Check out the API for Stack:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Stack.html#pop()
It tells you that the pop() method throws an EmptyStackException if the stack is empty. Like I said before, you should go back to a basic tutorial and learn this stuff. We can't debug your entire program this way, and it will be much more effective for you to learn through a tutorial than this forum.
> when i changed
> if(player.isEmpty() == true){
> to
> if(player.isEmpty() == false){
>
> yeah the error is EmptyStackException.
>
> I'd love to know what that means and how i can fix it?
When you reverse the logic of the test: from
if (player.isEmpty())
to
if (!player.isEmpty())
It shouldn't surprise you that you are trying to pop an empty stack.
If you are at the point that you are just guessing and changing your
code here and there in an effort to make it work, I think it's time to
step away from the keyboard, take a break, then come back and
think about the design behind the code. Maybe meet with your
instructor and get some input. Making random changes in code
rarely fixes it. it's like trying to flip a coin 100 times and have it
come up heads every time.
ok well thanks anyway. but when you were saying earlier that at the end of my method i have return null;what else could i put? like i need to have a return statement there.
> ok well thanks anyway. but when you were saying
> earlier that at the end of my method i have return
> null;
>
> what else could i put? like i need to have a return
> statement there.
You don't have to have a return statement there, it's up to you what you return. If your design requires you return some Room object, then return the appropriate one. If it doesn't, then declare the method to return void. It's up to you how you design the program.
> i wish i was like u guys
I've taught Java programming and had more that one student
tell me that learning object-oriented programming had "rewired"
their brains, that they thought differently because of it.
I don't know if I'd go so far as to claim Java has changed the way I
think (that makes it sound like LSD), but learning to program
forces one to think logically, in a way few people do in everyday
life. I think there's a "hump" you have to get over, like dreaming
in a foreign language you are learning, before coding becomes
natural. You just have to stick with it.