views:

42

answers:

2

OK so I have this applet I am making and I want it to generator a world according to a number...

Here it is:

 public int[][] loadBoard(int map) {
    if (map == 1) { int[][] board = { {
 2,2,24,24,24,24,24,3,3,0,0,0,1 },

 { 2,2,24,23,23,23,24,1,3,0,0,0,1 },

 { 1,1,24,23,23,23,24,1,3,3,3,3,1 },

 { 1,1,24,24,23,24,24,1,1,1,1,3,1 },

 { 1,1,1,1,7,1,1,1,1,1,1,3,1 },

 { 5,1,1,1,7,7,7,7,7,1,1,1,1 },

 { 6,3,3,1,3,3,3,1,7,7,7,3,1 },

 { 6,3,3,1,3,1,1,1,1,1,7,1,1 },

 { 3,3,1,1,1,1,1,1,1,1,7,1,1 } };

 }else{

 int[][] board = {

 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },


 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,24,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,24,1,1,1,1 },

 { 1,1,7,1,1,24,24,24,24,1,1,1,1 },

 { 1,1,7,1,1,24,1,24,1,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },

 { 1,3,3,1,1,24,1,1,1,1,1,1,1 },

 }; } return board; }

and to call it I use:

board = loadBoard(1);

I put that in the init() method. Then that way I can call maps upon the number inside loadBoard(). However, when I start my game I get nullpointer exception and I KNOW for a FACT that its something to do with the code I just showed you above. It's probably some rookie mistake I am doing.. maybe you can help?

A: 

I'm not sure that your null pointer is related to this code. However, it is really weird the fact that you declare board inside the scope of the if/else statement and the return this variable outside the scope.

YuppieNetworking
yeah, i kinda noticed what I was doing was wrong halfway but then I said "screw it, i'll go ask stackoverflow"
Dan
+2  A: 

It is. You create "board" variable again. Even though the name is the same the variable you return is not the one you created. Here is the fixed code:

public int[][] loadBoard(int map) {
    if (map == 1) { 
 return new int[][] { 

 {2,2,24,24,24,24,24,3,3,0,0,0,1 },

 { 2,2,24,23,23,23,24,1,3,0,0,0,1 },

 { 1,1,24,23,23,23,24,1,3,3,3,3,1 },

 { 1,1,24,24,23,24,24,1,1,1,1,3,1 },

 { 1,1,1,1,7,1,1,1,1,1,1,3,1 },

 { 5,1,1,1,7,7,7,7,7,1,1,1,1 },

 { 6,3,3,1,3,3,3,1,7,7,7,3,1 },

 { 6,3,3,1,3,1,1,1,1,1,7,1,1 },

 { 3,3,1,1,1,1,1,1,1,1,7,1,1 } };

 }else{

 return new int[][] {

 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },


 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,24,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,24,1,1,1,1 },

 { 1,1,7,1,1,24,24,24,24,1,1,1,1 },

 { 1,1,7,1,1,24,1,24,1,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },

 { 1,1,1,1,1,24,1,1,1,1,1,1,1 },

 { 1,3,3,1,1,24,1,1,1,1,1,1,1 },

 }; } 

}

It can be simp'ified even more if I would now how many "numbers" you're planing to have :)

Another suggestion would be to NOT create arrays on the fly, but have them as constants. Then return appropriate array from the method. Your code may look like (for more then 2 choices):

 private static final int[][] BOARD1 = <array here>;
 private static final int[][] BOARD2 = <array here>;
 private static final int[][] BOARD3 = <array here>;
 private static final int[][] BOARD4 = <array here>;


 public function int[][] loadBoard( int choice ) {
   switch( choice ) {
      case 1: return BOARD1;
      case 2: return BOARD2;
      case 3: return BOARD3;
      case 4: return BOARD4;
      default: throw new RuntimeException( "Unknown board choice" );
   }
 }
eugener
let's say 4. what then? and thank you :P
Dan
ps - i get unreachable statement on your code for the line "return board" :S
Dan
@Dan - well delete it.
Stephen C
Yes... delete it :) Left by mistake :)If you have more then 2 "numbers" you can use switch statement.
eugener
See updated answer for more then 2 choices + boards as constants
eugener