title: How to add a score counter in android game?
body: <p>Hi I want to implement an 'image score counter' in my android game but it does not work well. However it compiles well but soon when ball reaches screen edge app returns error:</p>
  
  <pre><code>04-08 10:07:31.639: E/AndroidRuntime(8599): FATAL EXCEPTION: Thread-1176
  04-08
  10:07:31.639: E/AndroidRuntime(8599): java.lang.NullPointerException
  04-08 10:07:31.639: E/AndroidRuntime(8599):at com.Pong.GameState.update(GameState.java:154)
  04-08 10:07:31.639: E/AndroidRuntime(8599):at com.Pong.GameThread.run(GameThread.java:41)
  </code></pre>
  
  <p>I got 3 files that menage the game:
  GameState.java GameView.java GameThread.java:</p>
  
  <p>The errors are in:</p>
  
  <p><strong>GameState.java:</strong></p>
  
  <pre><code>TopCounter.setImageResource(myImageList[i+1]);
  </code></pre>
  
  <p><strong>GameThread.java:</strong></p>
  
  <pre><code>_state.update();
  </code></pre>
  
  <p>This is how GameThread.java looks like:</p>
  
  <pre><code>package com.Pong;
  
  import....
  
      class GameThread extends Thread {
          private SurfaceHolder surfaceHolder;
          private GameView gameView;
          private Paint _paint;
          private GameState _state;
          private boolean run = false;
          public GameThread(SurfaceHolder surfaceHolder, GameView gameView, Context context, Handler handler) {
              this.surfaceHolder = surfaceHolder;
              this.gameView = gameView;
              _paint = new Paint();
              _state = new GameState();
          }
  
          public void setRunning(boolean run) {
              this.run = run;
          }
  
          public SurfaceHolder getSurfaceHolder() {
              return surfaceHolder;
          }
  
          @Override
          public void run() {
              Canvas c;
              while (run) {
                  c = null;
  
                  try {
                      c = surfaceHolder.lockCanvas(null);
  
                      synchronized (surfaceHolder) {
                           gameView.onDraw(c);
                          _state.update();  //here is the error
                          _state.draw(c, _paint);
                      }
                  } finally {
                      if (c != null) {
                          surfaceHolder.unlockCanvasAndPost(c);
                      }
                  }
              }
          }
  
  
  public GameState getGameState()
  {
  return _state;
  }
  }
  </code></pre>
  
  <p>This is how GameState.java looks like (I cut unnecessary lines):</p>
  
  <pre><code>package com.Pong;
  
  Import ...
  
  public class GameState extends Activity implements View.OnTouchListener {
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.gamestate);
          TopCounter = (ImageView) findViewById(R.id.counterT);
          BottomCounter = (ImageView) findViewById(R.id.counterB);
  
  }
      int _screenWidth = 270;
      int _screenHeight = 395;   
      int _ballSize = 10;
      int _ballX = 100;
      int _ballY = 100;
      int _ballVelocityX = 3;
      int _ballVelocityY = 3;
      int _batLength = 75;
      int _batHeight = 10;
      int _topBatX = (_screenWidth / 2+25) - (_batLength / 2);
      int _topBatY = 54;
      int _bottomBatX = (_screenWidth / 2+25) - (_batLength / 2); 
      int _bottomBatY = 392;
      int i=0;
      // under is matrix that contains images to my counter.
      int[] myImageList = new int[]{
              R.drawable.counter00,
              R.drawable.counter01,
              R.drawable.counter02,
              R.drawable.counter03,
              R.drawable.counter04,
              R.drawable.counter05,
              R.drawable.counter06,
              R.drawable.counter07,
              R.drawable.counter08,
              R.drawable.counter09
              };
      ImageView TopCounter;
      ImageView BottomCounter; 
  
      public GameState()
      {}
  
      public void update() {
  
          _ballX += _ballVelocityX;
          _ballY += _ballVelocityY;
  
  
      if( _ballY &gt; _screenHeight){
              _ballX = _screenWidth/2;
              _ballY = _screenHeight/2;
              TopCounter.setImageResource(myImageList[i+1]); //here is the error
              _toppoint=_toppoint+1;
              _topBatX =_screenWidth/2-10;
              _bottomBatX =_screenWidth/2-10;
              }       
  
          if( _ballY &lt; 50){
              _ballX = _screenWidth/2;
              _ballY = _screenHeight/2;
              BottomCounter.setImageResource(myImageList[i+1]);
              _bottompoint=_bottompoint+1;
              _topBatX =_screenWidth/2-10;
              _bottomBatX =_screenWidth/2-10;
              } 
          if (_toppoint==9){
  
          } 
          else if (_bottompoint==9) {
  
          }
  
          if(_ballX &gt; _screenWidth || _ballX &lt;= 40){
              _ballVelocityX *= -1;
              }
          if(_ballX &gt; _topBatX &amp;&amp; _ballX &lt; _topBatX+_batLength &amp;&amp; _ballY &lt; _topBatY){
              _ballVelocityY *= -1;
          }
          if(_ballX &gt; _bottomBatX &amp;&amp; _ballX &lt; _bottomBatX+_batLength &amp;&amp; _ballY &gt; _bottomBatY){
              _ballVelocityY *= -1;
          }
          if (_ballX == 40+_batLength) {
              _topBatX=40;
              } else if (_ballX &gt; 40+_batLength &amp;&amp; _ballX &lt;=  _screenWidth){
                  _topBatX=_ballX-_batLength+6;
                  } 
          if (_bottomBatX &lt; 46) {
              _bottomBatX=46;
          } else if (_bottomBatX+_batLength &gt; _screenWidth){
              _bottomBatX=_screenWidth-_batLength+6;
          }
          }
      public void draw(Canvas c, Paint paint) {
      paint.setARGB(255, 234, 222, 199);
      c.drawRect(new Rect(_ballX,_ballY,_ballX + _ballSize,_ballY + _ballSize), paint);
      c.drawRect(new Rect(_topBatX, _topBatY, _topBatX + _batLength, _topBatY + _batHeight), paint); //top bat
      c.drawRect(new Rect(_bottomBatX, _bottomBatY, _bottomBatX + _batLength, _bottomBatY + _batHeight), paint); //bottom bat
      }
  
  
  }
  </code></pre>
  
  <p>In my XML file:</p>
  
  <pre><code> &lt;ImageView
          android:id="@+id/counterT"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/counter00"
         android:layout_gravity="left|center_vertical"
           /&gt;
          &lt;ImageView
          android:id="@+id/counterB"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="@drawable/counter00" /&gt;
  </code></pre>
  
  <p>Can anybody help ?</p>