• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Null Pointer Exception Issue in Java Maze

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for being so friendly on this community! I think I'm almost done my maze. I have a file called MazeRunner with preprogrammed mazes another file called MazeUtils that allows the user to choose and play a maze, by moving an "o" through "x's" that represent walls. I'm experiencing one last issue, though. Upon entering a direction for the "o" to move, I get an error that reads: 'Exception in thread "main" java.lang.NullPointerException .' The error points to the part of code for each direction that says:

if(map[location[a]-1][location[b]]==1)
System.out.println("There's a wall here!");

For example, if I type in "u" to go up, the error will point to line 90 in MazeUtils.
Can anybody help me figure out what is wrong with the math here? I'm not sure what's causing the issue. Thank you very much for the help!
Here's the MazeRunner file:




And here's the MazeUtils file:

 
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you've got a class variable a and an array location both initialized to zero, so when you get here:

location[a] = location[0] = 0. Then you subtract one and your array is out of bounds. Not sure why you get a NPE, but that's probably part of your problem.
 
Saloon Keeper
Posts: 8588
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are not assigning any thing to 'map'. I assume that for

you really wanted
 
Sheriff
Posts: 8064
569
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liam Mass wrote:

Shouldn't be other way round? location = startPosition1
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic