2012-10-13 84 views
0

我试图做一个基本的game.I时,有一个名为Ball.java,Scratch.java和Panel.javaNullPointerException异常试图获得信息

在面板类,我试图从Ball.java获取信息类,但我得到NullPointerException.Where是我的代码中的错误?

获取exceiption在这段代码中Panel.java

public Rect getBoundsBall(){ 
    return new Rect (top.getX(), top.getY(),top.getX()+ball.getWidth() ,top.getY()+ball.getHeight()); 
} 

错误日志

10-13 12:57:28.746: E/AndroidRuntime(367): FATAL EXCEPTION: Thread-10 
10-13 12:57:28.746: E/AndroidRuntime(367): java.lang.NullPointerException 
10-13 12:57:28.746: E/AndroidRuntime(367): at com.emredavarci.denemeler.Panel.getBoundsBall(Panel.java:44) 
10-13 12:57:28.746: E/AndroidRuntime(367): at com.emredavarci.denemeler.Panel.update(Panel.java:70) 
10-13 12:57:28.746: E/AndroidRuntime(367): at com.emredavarci.denemeler.TutorialThread.run(TutorialThread.java:30) 

Panel.java

public class Panel extends SurfaceView implements SurfaceHolder.Callback { 

    private TutorialThread _thread; 

    Scratch raket=new Scratch(); 
    Ball top=new Ball(); 
    Bitmap ball; 
    Bitmap _scratch; 

    public Panel(Context context) { 
     super(context); 
     getHolder().addCallback(this); 
     _thread = new TutorialThread(getHolder(), this);/
     setFocusable(true); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { // to 
     switch (event.getAction()){ 
      case MotionEvent.ACTION_MOVE: // 
      raket.setX((int)(event.getX())); 
      break; 
     } 

     return true; 

    } 

    public Rect getBoundsBall(){ 
     return new Rect (top.getX(), top.getY(),top.getX()+ball.getWidth() ,top.getY()+ball.getHeight()); 
    } 

    public Rect getBoundsScratch(){ 
     return new Rect (raket.getX(), raket.getY(),raket.getX()+_scratch.getWidth() ,raket.getY()+_scratch.getHeight()); 
    } 

    public void update(){/


     if((top.getX()<480) && (top.getX()>0)){ 
      top.setX(top.getX()+top.getxdirection()); 
      } 
     if((top.getX()==480) || (top.getX()==0)){ 
      top.setxdirection(-1); 
      top.setX(top.getX()+top.getxdirection()); 
     } 
     if((top.getY()<780) && (top.getY()>0)){ 
      top.setY(top.getY()+top.getydirection()); 
      } 
     if((top.getY()==780) || (top.getY()==0)){ 
      top.setydirection(-1); 
      top.setY(top.getY()+top.getydirection()); 
     } 


     Rect BallBounds = getBoundsBall(); 
     Rect ScratchBounds = getBoundsScratch(); 

     if(BallBounds.intersect(ScratchBounds) ){    
      top.setydirection(-1); 
      //top.setY(top.getY()); !!!! 
     } 

    } 





    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 
     // TODO Auto-generated method stub 
    } 

    public void surfaceCreated(SurfaceHolder holder) { 
     _thread.setRunning(true); 
     _thread.start(); 
    } 

    public void surfaceDestroyed(SurfaceHolder holder) { 
     // simply copied from sample application LunarLander: 
     // we have to tell thread to shut down & wait for it to finish, or else 
     // it might touch the Surface after we return and explode 
     boolean retry = true; 
     _thread.setRunning(false); 
     while (retry) { 
      try { 
       _thread.join(); 
       retry = false; 
      } catch (InterruptedException e) { 
       // we will try it again and again... 
      } 
     } 
    } 

    @Override 
    public void onDraw(Canvas canvas) { 
    _scratch = BitmapFactory.decodeResource(getResources(), R.drawable.red2); 
    ball = BitmapFactory.decodeResource(getResources(), R.drawable.ball); 
    canvas.drawColor(Color.BLACK); 
    canvas.drawBitmap(_scratch, raket.getX() - (_scratch.getWidth()/2), raket.getY() - (_scratch.getHeight()/2), null); 
    canvas.drawBitmap(ball, top.getX() - (ball.getWidth()/2), top.getY() - (ball.getHeight()/2), null); 
    } 

}

Ball.java

public class Ball { 

    private int x=100;   // the X coordinate 
    private int y=100;   // the Y coordinate 
    int xdirection=10; 
    int ydirection=10; 


    public Ball() { 
     // TODO Auto-generated constructor stub 
    } 

    public int getX(){ 
     return x; 
    } 

    public int getY(){ 
     return y; 
    } 

    public void setX(int a){ 
     x=a; 
    } 

    public void setY(int b){ 
     y=b; 
    } 

    public int getxdirection(){ 
     return xdirection; 
    } 

    public int getydirection(){ 
     return ydirection; 
    } 

    public void setxdirection(int a){ 
     xdirection=xdirection*a; 
    } 

    public void setydirection(int b){ 
     ydirection=ydirection*b; 
    } 

    //while ile ball u hareket ettir 
    //ball un koordinatlarını sakla 


} 

Scratch.java

public class Scratch { 

    private int x = 250; 
    private int y = 600; 

    public Scratch() { 
     // TODO Auto-generated constructor stub 
    } 

    public void setX(int a){ 
     x=a; 
    } 

    public void setY(int b){ 
     y=b; 
    } 

    public int getX(){ 
     return x; 
    } 

    public int getY(){ 
     return y; 
    } 

} 
+0

也请张贴异常的输出。 –

+0

共享堆栈跟踪 – Anshu

+0

当您发布堆栈跟踪时,请记住突出显示发生异常的留置权。 – Simon

回答

1

你失去了一些东西,如:

Ball top = new Ball(); 

即变量top没有初始化,但在update方法使用。

另外,初始化ball对象outsiteonDraw。谁知道在onDraw之前是否会拨打update

+0

我做了你所说的改变,但它仍然给予同样的错误。我已经添加了错误日志也 – Zapdos

+0

是的,谢谢你,我肯定在onDraw()之前调用update()。它的工作! – Zapdos

1

加入@ Rudolph的回答,你确定Bitmap ball已初始化?可以肯定,尝试登录ball为:

public Rect getBoundsBall(){ 
    log.v("ball",""+ball); 
    return new Rect (top.getX(), top.getY(),top.getX()+ball.getWidth() ,top.getY()+ball.getHeight()); 
} 

无论balltop是罪魁祸首。找出哪一个是未初始化的,并相应地解决它。

+1

感谢您的有用信息! – Zapdos

相关问题