• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Null Error in Program

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



I'm running into an error on line 26 when I try to run it. It gives this error: Exception in thread "main" java.lang.NullPointerException: Cannot invoke "World.Room.getExit(char)" because "currentRoom" is null at GameRoom/World.Game.main(Game.java:26)
 
Saloon Keeper
Posts: 10929
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This call prior to line 26 is the last place 'currrentRoom' has been set so that means the call to getExit('s') is returning null, you'll have to figure out why.
 
Master Rancher
Posts: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it's usually a good idea to look at the error message:

because "currentRoom" is null at GameRoom/World.Game.main(Game.java:26)


So, why is currentRoom null?  Is there somewhere in your program where currentRoom is set to something other than null?  Where does that data come from?

Or, you could read what Carey already posted. Oh well, too late...
 
Rancher
Posts: 5035
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is printed by the println statement on line 24?
 
Anai Anderson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:This call prior to line 26 is the last place 'currrentRoom' has been set so that means the call to getExit('s') is returning null, you'll have to figure out why.



I tried taking it off or changing it, but I'm still getting the same error...
 
Mike Simmons
Master Rancher
Posts: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have the code to theRoom class?  Is that something you can change?  Or is it something you were given?  Can you at least look at it, and show it to us?

Alternately, looking at your main() method, it looks like you're basically wandering around a maze.  How do you know that it's actually possible to move in that direction?  Is it possible that the method returns null when it's not possible to move in that direction?  If that happens, you should probably not update currentRoom to be what getExit() returns, because if it's null, you no longer have a valid currentRoom reference.  
 
Anai Anderson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:Do you have the code to theRoom class?  Is that something you can change?  Or is it something you were given?  Can you at least look at it, and show it to us?

Alternately, looking at your main() method, it looks like you're basically wandering around a maze.  How do you know that it's actually possible to move in that direction?  Is it possible that the method returns null when it's not possible to move in that direction?  If that happens, you should probably not update currentRoom to be what getExit() returns, because if it's null, you no longer have a valid currentRoom reference.  



I actually was able to change it to something different now:

 
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note: I added code tags to the previous post to make it readable.
 
Carey Brown
Saloon Keeper
Posts: 10929
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You still have a problem here that getExit() can return null and you are not handling that.
 
Tim Holloway
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pedantic note here: While it is syntactically valid to start a package name component with an upper-case letter, it is a standard convention that package name components should start with lower-case. Similar conventions apply to class and member/method naming capitalizations.

Also, it's common, though not required to make your package names be world-unique by making the high-level elements of it to reflect the organization you're working for. Now, it's perfectly OK not to make a package name be something like "edu.ucf.cop0999.jsmith.project1", but if you want to share your code with the world, it's helpful.

And of course, while Windows likes to create directories with names that start uppercase, the rest of the world, including Java, generally does not, and Java is case-sensitive, so if you do rename your package from "World" to "world", make sure you package directory name is also changed!

On a less pedantic level, it's worth noting that in practical application setting up a dungeon like this is often easier to do declaratively. If, rather than coding an explicit statement for each room and exit, you had a more general algorithm, you could even load up the architecture from a config file such as a Java properties file or a structured data file such as YAML or XML and be able to completely change the world without altering the program code.
 
Blueberry pie is best when it is firm and you can hold in your hand. Smell it. And smell this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic