title: Drawing board game pieces in an array
body: <p>I cannot get the array to display rooks or bishops (r &amp; b). I need them displayed on the row in front of the King (k), G, s, n, l.
  The case statements should run through and draw all the pieces on the board, however it will only run through 18 times, then crash, but I'm sure I declared the array to have 40 pieces (In shogi each player starts with 20 pieces).
  The case statements also ignore case r and b at the moment.</p>
  
  <pre><code>    private void setUpNewPieces(){
      for (int i = 0; i &lt; 2; i++){
          //Sets back row.
          pieces[20 * i] = new Piece ('K', i, 4, (8 * i));
          pieces[(20 * i) + 1] = new Piece('G', i, 3, (8 * i));
          pieces[(20 * i) + 2] = new Piece('s', i, 2, (8 * i));
          pieces[(20 * i) + 3] = new Piece('n', i, 1, (8 * i));
          pieces[(20 * i) + 4] = new Piece('l', i, 0, (8 * i));
          pieces[(20 * i) + 5] = new Piece('G', i, 5, (8 * i));
          pieces[(20 * i) + 6] = new Piece('s', i, 6, (8 * i));
          pieces[(20 * i) + 7] = new Piece('n', i, 7, (8 * i));
          pieces[(20 * i) + 8] = new Piece('l', i, 8, (8 * i));
  
          for (int i3 = 0; i3 &lt; 2; i3++){
              pieces[(20 * i) + 9] = new Piece('b', i, i3, (7 * i));
              pieces[(20 * i) + 10] = new Piece('r', i, i3, (7 * 1));
          }
          //Set pawns.
          for (int i2 = 0; i2 &lt; 9; i2++){
              pieces[(18 * i) + 9 + i2] = new Piece('p', i, i2,(6 * i));
          }
          Log.d("setUpNewPieces", "pawnDone");
      }
      Log.d("setUpNewPieces", "nonPawnDone");
  }//END private void newPieces()
  
  
  
  private void updateBoard(){
      Location field;
      int x, y;
  
      String theArray;
      for(int jkl=0;jkl &lt; pieces.length;jkl++){
          theArray = jkl + ": " + String.valueOf(pieces[jkl].type) + "-" + String.valueOf(pieces[jkl].direction);
          Log.d("TheArray",theArray);
      }
  
      //Draw pieces.
      for (int i = 0; i &lt; 40; i++){
          field = pieces [i].getLocation();
          Log.d("DrawPieces - i",String.valueOf(i));
          Log.d("DrawPieces - type",String.valueOf(pieces[i].type));
          Log.d("DrawPieces - direction",String.valueOf(pieces[i].direction));
          x = field.getX();
          y = field.getY();
  
          if(!field.isEmpty()){
              switch (pieces[i].direction){
              case 0:
                  Log.d("pieces[i].d...","case0Start");
                  switch (pieces[i].type){
                  case 'K':
                      gameBoardView[x][y].setImageResource(R.drawable.king);
                      break;
                  case 'G':
                      gameBoardView[x][y].setImageResource(R.drawable.gold);
                      break;
                  case 's':
                      gameBoardView[x][y].setImageResource(R.drawable.silver);
                      break;
                  case 'S':
                      gameBoardView[x][y].setImageResource(R.drawable.silver_p);
                      break;
                  case 'n':
                      gameBoardView[x][y].setImageResource(R.drawable.knight);
                      break;
                  case 'N':
                      gameBoardView[x][y].setImageResource(R.drawable.knight_p);
                      break;
                  case 'l':
                      gameBoardView[x][y].setImageResource(R.drawable.lance);
                      break;
                  case 'L':
                      gameBoardView[x][y].setImageResource(R.drawable.lance_p);
                      break;
                  case 'b':
                      gameBoardView[x][y].setImageResource(R.drawable.bishop);
                      break;
                  case 'B':
                      gameBoardView[x][y].setImageResource(R.drawable.bishop_p);
                      break;
                  case 'r':
                      gameBoardView[x][y].setImageResource(R.drawable.rook);
                      break;
                  case 'R':
                      gameBoardView[x][y].setImageResource(R.drawable.rook_p);
                      break;
                  case 'p':
                      gameBoardView[x][y].setImageResource(R.drawable.pawn);
                      break;
                  case 'P':
                      gameBoardView[x][y].setImageResource(R.drawable.pawn_p);
                      break;
                  }//END switch (pieces[i].type) WITHIN case 0
                  Log.d("pieces[i].d...","case0Finish");
                  break;
              case 1:
                  Log.d("pieces[i].d...","case1Start");
                  switch (pieces[i].type){
                  case 'K':
                      gameBoardView[x][y].setImageResource(R.drawable.king_rot);
                      break;
                  case 'G':
                      gameBoardView[x][y].setImageResource(R.drawable.gold_rot);
                      break;
                  case 's':
                      gameBoardView[x][y].setImageResource(R.drawable.silver_rot);
                      break;
                  case 'S':
                      gameBoardView[x][y].setImageResource(R.drawable.silver_p_rot);
                      break;
                  case 'n':
                      gameBoardView[x][y].setImageResource(R.drawable.knight_rot);
                      break;
                  case 'N':
                      gameBoardView[x][y].setImageResource(R.drawable.knight_p_rot);
                      break;
                  case 'l':
                      gameBoardView[x][y].setImageResource(R.drawable.lance_rot);
                      break;
                  case 'L':
                      gameBoardView[x][y].setImageResource(R.drawable.lance_p_rot);
                      break;
                  case 'b':
                      gameBoardView[x][y].setImageResource(R.drawable.bishop_rot);
                      break;
                  case 'B':
                      gameBoardView[x][y].setImageResource(R.drawable.bishop_p_rot);
                      break;
                  case 'r':
                      gameBoardView[x][y].setImageResource(R.drawable.rook_rot);
                      break;
                  case 'R':
                      gameBoardView[x][y].setImageResource(R.drawable.rook_p_rot);
                      break;
                  case 'p':
                      gameBoardView[x][y].setImageResource(R.drawable.pawn_rot);
                      break;
                  case 'P':
                      gameBoardView[x][y].setImageResource(R.drawable.pawn_p_rot);
                      break;
                  }//END switch (pieces[i].type) WITHIN case 1
                  Log.d("pieces[i].d...","case1Finish");
                  break;
  
              }//END switch (pieces[i].direction)
              Log.d("updateBoardDrawPieces", "drawDone");
          }//END if(!field.isEmpty())
      }//END for (int i = 0; i &lt; 40; i++)
  
  }//END updateBoard{
  </code></pre>
  
  <p>Here is the full error log:</p>
  
  <pre><code>03-01 14:59:55.262: DEBUG/AndroidRuntime(579): &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; AndroidRuntime START &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
  03-01 14:59:55.272: DEBUG/AndroidRuntime(579): CheckJNI is ON
  03-01 14:59:55.522: DEBUG/AndroidRuntime(579): --- registering native functions ---
  03-01 14:59:56.742: DEBUG/dalvikvm(184): GC_EXPLICIT freed 121 objects / 6392 bytes in 153ms
  03-01 14:59:56.852: DEBUG/PackageParser(68): Scanning package: /data/app/vmdl73743.tmp
  03-01 14:59:57.242: DEBUG/dalvikvm(68): GC_FOR_MALLOC freed 14819 objects / 820272 bytes in 171ms
  03-01 14:59:58.022: INFO/PackageManager(68): Removing non-system package:com.coursework.shogi
  03-01 14:59:58.022: INFO/ActivityManager(68): Force stopping package com.coursework.shogi uid=10036
  03-01 14:59:58.252: DEBUG/PackageManager(68): Scanning package com.coursework.shogi
  03-01 14:59:58.252: INFO/PackageManager(68): Package com.coursework.shogi codePath changed from /data/app/com.coursework.shogi-2.apk to /data/app/com.coursework.shogi-1.apk; Retaining data and using new
  03-01 14:59:58.272: INFO/PackageManager(68): /data/app/com.coursework.shogi-1.apk changed; unpacking
  03-01 14:59:58.322: DEBUG/installd(35): DexInv: --- BEGIN '/data/app/com.coursework.shogi-1.apk' ---
  03-01 14:59:58.692: DEBUG/dalvikvm(586): DexOpt: load 64ms, verify 143ms, opt 4ms
  03-01 14:59:58.705: DEBUG/installd(35): DexInv: --- END '/data/app/com.coursework.shogi-1.apk' (success) ---
  03-01 14:59:58.712: WARN/PackageManager(68): Code path for pkg : com.coursework.shogi changing from /data/app/com.coursework.shogi-2.apk to /data/app/com.coursework.shogi-1.apk
  03-01 14:59:58.712: WARN/PackageManager(68): Resource path for pkg : com.coursework.shogi changing from /data/app/com.coursework.shogi-2.apk to /data/app/com.coursework.shogi-1.apk
  03-01 14:59:58.712: DEBUG/PackageManager(68):   Activities: com.coursework.shogi.Shogi com.coursework.shogi.Instructions com.coursework.shogi.Game com.coursework.shogi.Prefs
  03-01 14:59:58.732: INFO/ActivityManager(68): Force stopping package com.coursework.shogi uid=10036
  03-01 14:59:58.932: INFO/installd(35): move /data/dalvik-cache/data@app@com.coursework.shogi-1.apk@classes.dex -&gt; /data/dalvik-cache/data@app@com.coursework.shogi-1.apk@classes.dex
  03-01 14:59:58.932: DEBUG/PackageManager(68): New package installed in /data/app/com.coursework.shogi-1.apk
  03-01 14:59:59.192: INFO/ActivityManager(68): Force stopping package com.coursework.shogi uid=10036
  03-01 14:59:59.332: DEBUG/dalvikvm(127): GC_EXPLICIT freed 796 objects / 43384 bytes in 117ms
  03-01 14:59:59.742: WARN/RecognitionManagerService(68): no available voice recognition services found
  03-01 15:00:00.182: DEBUG/dalvikvm(68): GC_EXPLICIT freed 12265 objects / 682336 bytes in 190ms
  03-01 15:00:00.262: DEBUG/dalvikvm(165): GC_EXPLICIT freed 1580 objects / 80600 bytes in 895ms
  03-01 15:00:00.342: INFO/installd(35): unlink /data/dalvik-cache/data@app@com.coursework.shogi-2.apk@classes.dex
  03-01 15:00:00.372: DEBUG/AndroidRuntime(579): Shutting down VM
  03-01 15:00:00.382: DEBUG/dalvikvm(579): Debugger has detached; object registry had 1 entries
  03-01 15:00:00.412: INFO/AndroidRuntime(579): NOTE: attach of thread 'Binder Thread #3' failed
  03-01 15:00:01.212: DEBUG/AndroidRuntime(592): &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; AndroidRuntime START &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;
  03-01 15:00:01.212: DEBUG/AndroidRuntime(592): CheckJNI is ON
  03-01 15:00:01.582: DEBUG/AndroidRuntime(592): --- registering native functions ---
  03-01 15:00:02.673: INFO/ActivityManager(68): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.coursework.shogi/.Shogi }
  03-01 15:00:02.942: DEBUG/AndroidRuntime(592): Shutting down VM
  03-01 15:00:02.973: DEBUG/dalvikvm(592): Debugger has detached; object registry had 1 entries
  03-01 15:00:03.052: INFO/AndroidRuntime(592): NOTE: attach of thread 'Binder Thread #3' failed
  03-01 15:00:03.104: INFO/ActivityManager(68): Start proc com.coursework.shogi for activity com.coursework.shogi/.Shogi: pid=599 uid=10036 gids={}
  03-01 15:00:04.702: INFO/ActivityManager(68): Displayed activity com.coursework.shogi/.Shogi: 1768 ms (total 2033641 ms)
  03-01 15:00:07.112: INFO/ActivityManager(68): Starting activity: Intent { cmp=com.coursework.shogi/.Game }
  03-01 15:00:07.622: DEBUG/setContentView(599): Called in protected void
  03-01 15:00:07.652: DEBUG/setUpObjects(599): setUpGameBoardViewCalled
  03-01 15:00:07.682: DEBUG/setUpObjects(599): initialseGridViews
  
  It calls the class 40 times, as I wanted...
  
  03-01 15:00:07.692: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.692: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.692: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.702: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.722: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.722: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.722: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.722: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.734: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.734: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.741: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.741: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.752: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.752: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.752: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.752: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.762: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.762: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.772: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.772: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.772: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.772: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.772: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.772: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.772: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.772: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.772: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.772: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.772: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.772: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.772: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.772: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.772: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.772: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.772: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.772: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.772: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.792: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.792: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.792: DEBUG/publicClassPiece(599): Started
  03-01 15:00:07.792: DEBUG/setUpObjects(599): lastMoveInitialised
  03-01 15:00:07.792: DEBUG/setUpObjects(599): Called in protected void
  03-01 15:00:07.792: DEBUG/setUpNewPieces(599): pawnDone
  03-01 15:00:07.792: DEBUG/setUpNewPieces(599): pawnDone
  03-01 15:00:07.792: DEBUG/setUpNewPieces(599): nonPawnDone
  03-01 15:00:07.792: DEBUG/setUpNewPieces(599): Called in protected void
  
  Here TheArray is only called 18 times, I was told this is because I only have 18 spaces in the array, and it then crashes because there is no more room. It calls the back row of pieces and the pawns, ignoring the rook and bishop; and never getting to the second player
  (Sorry if I repeated myself, but i thought this may help).
  
  03-01 15:00:07.792: DEBUG/TheArray(599): 0: K-0
  03-01 15:00:07.792: DEBUG/TheArray(599): 1: G-0
  03-01 15:00:07.792: DEBUG/TheArray(599): 2: s-0
  03-01 15:00:07.792: DEBUG/TheArray(599): 3: n-0
  03-01 15:00:07.792: DEBUG/TheArray(599): 4: l-0
  03-01 15:00:07.792: DEBUG/TheArray(599): 5: G-0
  03-01 15:00:07.792: DEBUG/TheArray(599): 6: s-0
  03-01 15:00:07.792: DEBUG/TheArray(599): 7: n-0
  03-01 15:00:07.792: DEBUG/TheArray(599): 8: l-0
  03-01 15:00:07.792: DEBUG/TheArray(599): 9: p-0
  03-01 15:00:07.792: DEBUG/TheArray(599): 10: p-0
  03-01 15:00:07.792: DEBUG/TheArray(599): 11: p-0
  03-01 15:00:07.792: DEBUG/TheArray(599): 12: p-0
  03-01 15:00:07.792: DEBUG/TheArray(599): 13: p-0
  03-01 15:00:07.792: DEBUG/TheArray(599): 14: p-0
  03-01 15:00:07.802: DEBUG/TheArray(599): 15: p-0
  03-01 15:00:07.802: DEBUG/TheArray(599): 16: p-0
  03-01 15:00:07.802: DEBUG/TheArray(599): 17: p-0
  03-01 15:00:07.802: DEBUG/AndroidRuntime(599): Shutting down VM
  03-01 15:00:07.802: WARN/dalvikvm(599): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
  03-01 15:00:07.852: ERROR/AndroidRuntime(599): FATAL EXCEPTION: main
  03-01 15:00:07.852: ERROR/AndroidRuntime(599): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.coursework.shogi/com.coursework.shogi.Game}: java.lang.NullPointerException
  03-01 15:00:07.852: ERROR/AndroidRuntime(599):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
  03-01 15:00:07.852: ERROR/AndroidRuntime(599):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
  03-01 15:00:07.852: ERROR/AndroidRuntime(599):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
  03-01 15:00:07.852: ERROR/AndroidRuntime(599):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
  03-01 15:00:07.852: ERROR/AndroidRuntime(599):     at android.os.Handler.dispatchMessage(Handler.java:99)
  03-01 15:00:07.852: ERROR/AndroidRuntime(599):     at android.os.Looper.loop(Looper.java:123)
  03-01 15:00:07.852: ERROR/AndroidRuntime(599):     at android.app.ActivityThread.main(ActivityThread.java:4627)
  03-01 15:00:07.852: ERROR/AndroidRuntime(599):     at java.lang.reflect.Method.invokeNative(Native Method)
  03-01 15:00:07.852: ERROR/AndroidRuntime(599):     at java.lang.reflect.Method.invoke(Method.java:521)
  03-01 15:00:07.852: ERROR/AndroidRuntime(599):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
  03-01 15:00:07.852: ERROR/AndroidRuntime(599):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
  03-01 15:00:07.852: ERROR/AndroidRuntime(599):     at dalvik.system.NativeStart.main(Native Method)
  03-01 15:00:07.852: ERROR/AndroidRuntime(599): Caused by: java.lang.NullPointerException
  03-01 15:00:07.852: ERROR/AndroidRuntime(599):     at com.coursework.shogi.Game$Piece.access$0(Game.java:72)
  03-01 15:00:07.852: ERROR/AndroidRuntime(599):     at com.coursework.shogi.Game.updateBoard(Game.java:434)
  03-01 15:00:07.852: ERROR/AndroidRuntime(599):     at com.coursework.shogi.Game.onCreate(Game.java:30)
  03-01 15:00:07.852: ERROR/AndroidRuntime(599):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
  03-01 15:00:07.852: ERROR/AndroidRuntime(599):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
  03-01 15:00:07.852: ERROR/AndroidRuntime(599):     ... 11 more
  03-01 15:00:07.904: WARN/ActivityManager(68):   Force finishing activity com.coursework.shogi/.Game
  03-01 15:00:07.912: WARN/ActivityManager(68):   Force finishing activity com.coursework.shogi/.Shogi
  03-01 15:00:08.413: WARN/ActivityManager(68): Activity pause timeout for HistoryRecord{45009650 com.coursework.shogi/.Game}
  03-01 15:00:14.871: DEBUG/dalvikvm(184): GC_EXPLICIT freed 163 objects / 11848 bytes in 158ms
  03-01 15:00:19.523: WARN/ActivityManager(68): Activity destroy timeout for HistoryRecord{44ff8a00 com.coursework.shogi/.Shogi}
  03-01 15:00:19.523: WARN/ActivityManager(68): Activity destroy timeout for HistoryRecord{45009650 com.coursework.shogi/.Game}
  03-01 15:00:19.912: DEBUG/dalvikvm(262): GC_EXPLICIT freed 47 objects / 2264 bytes in 154ms
  </code></pre>
  
  <p>Here is the class for the pieces:</p>
  
  <pre><code>public class Piece {
      /**Type List:
       * K = King
       * G = Gold General
       * s = Silver General
       * S = Promoted Silver
       * n = Knight
       * N = Promoted Knight
       * l = Lance
       * L = Promoted Lance
       * b = Bishop
       * B = Dragon Horse (Promoted Bishop)
       * r = Rook
       * R = Dragon (Promoted Rook)
       * p = Pawn
       * P = Token (Promoted Pawn)
       */
      private char type;//see above for the variables used for the types of piece..
      private int direction;//0 = black/gote, 1 = white/sente. 
      private Location field = new Location();
      private boolean moved;
      private boolean onBoard;//if false, the piece will be in a token reservoir.
          //private bool onBoard not implemented
  
      public Piece(char setType, int setDirection, int setX, int setY){
          this.type = setType;
          this.direction = setDirection;
          this.field.setX(setX);
          this.field.setY(setY);
          this.moved = false;
          this.onBoard = true;
      }
  
      public Piece(){
          this.type = 'K';
          this.direction = 0;
          this.field.empty();
          this.moved = false;
          this.onBoard = true;
      }
  
      public void copyFrom(Piece p){
          type = p.getType();
          direction = p.getDirection();
          field.setX(p.getLocation().getX());
          field.setY(p.getLocation().getY());
      }
  
      public Location getLocation(){
          return field;
      }
  
      public void setLocation(int x, int y, boolean move){
          field.setX(x);
          field.setY(y);
          if (move){
              moved = true;
          }
      }
  
      public int getX(){
          return field.getX();
      }
  
      public int getY(){
          return field.getY();
      }
  
      public void setX(int x){
          field.setX(x);
      }
  
      public void setY(int y){
          field.setY(y);
      }
  
      public boolean compareLocation(Location l){
          return field.equals(l);
      }
  
      public boolean compareLocation(int x, int y){
          return ((x == field.getX()) &amp;&amp; (y == field.getY()));
      }
  
      public int getDirection(){
          return direction;
      }
  
      public void setDirection(int i){
          direction = i;
      }
  
      public char getType(){
          return type;
      }
  
      public void setType(int i){
          type = (char) i;
      }
  
      public boolean hasMoved(){
          return moved;
      }
  
      public void setMoved(byte b){
          moved = (b == 1);
      }
  
  }//END public class Piece
  </code></pre>