• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to render a changeable landscape?  RSS feed

 
B. Paxton
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just wondering how to render a 2D landscape (top-down) that can change. I have written code that I think should work, and NetBeans can't find an error, but each run, either World.worldInit() throws a NullPointerException (if there is not enough input to generate the landscape) or Main.paint() throws another NullPointerException (no idea why)
I think it might be something to do with variable tile[][] when it is being checked to find what to render, but I'm not sure.
The for loop is the same in both cases but with different things in the middle.

Source:
Main.class: http://paste.ubuntu.com/783857/
World.class: http://paste.ubuntu.com/783859/

Please help!
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please post the line which throws the Exception, which you can identify from the stack trace given with the Exception. Also print out all the objects used in that line, whether they are null or not.
 
B. Paxton
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Error text: http://paste.ubuntu.com/783872/

Line 45 gave the error:
if(World.tile[rc1][rc2] == 1) { // dirt

the first time it checks World.tile[][] (which should be initialised to a value) is when it gives an error.
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And so where is the array-of-arrays (Hey Campbell!) initialized to a specific size? I don't know why it isn't failing before the line you give us; it should fail on worldInit as soon as you try to put something into one element of the array because the array's storage has not yet been allocated. Perhaps there are one or more other classes that access this public static?

rc
 
B. Paxton
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
List of all classes in the NetBeans project and source for each: http://paste.ubuntu.com/784033/
This list contains all classes except World.class (http://paste.ubuntu.com/783859/) and Main.class (http://paste.ubuntu.com/783857/)
I didn't think I had to allocate storage for the variable, I have never needed to do this previously. How do I do it?

EDIT: Googled it, found http://java.sun.com/docs/books/jls/second_edition/html/arrays.doc.html, image now renders correctly.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!