在android中调用TargetException和null指针异常

时间:2013-06-10 05:44:22

标签: java android nullpointerexception cocos2d-android invocationtargetexception

我在项目中有这部分代码,其中获取"空指针异常"在这一行CGRect shipRect = CGRect.make(ship.getPosition().x - (ship.getContentSize().width),但我在类中声明了船舶值为protected CCSprite ship;,但仍然得到了#34;空指针异常"错误。
   这里是完整的代码

public void update(float dt)
{


            for (CCSprite target : _targets)
              {
                    CGRect targetRect = CGRect.make(target.getPosition().x - (target.getContentSize().width),
                                                                              target.getPosition().y -
                                                                            (target.getContentSize().height),
                                                            target.getContentSize().width,target.getContentSize().height);

                    System.out.println("shipstodelete : " + ship);

                    CGRect shipRect = CGRect.make(ship.getPosition().x - (ship.getContentSize().width),
                            ship.getPosition().y - (ship.getContentSize().height),
                            ship.getContentSize().width,ship.getContentSize().height);



                 if (CGRect.intersects(targetRect, shipRect))
                    {

                    this.removeChild(ship, true);
                    //removeChild(ship, true);
                    }
              }   

这是构造函数

中的船精灵的编码
CCSprite ship = CCSprite.sprite("ship150.png");
     ship.setPosition(CGPoint.ccp(25,100));
     ship.setAnchorPoint(CGPoint.ccp(0,0));
     ship.setTag(4);
        addChild(ship);

logcat输出

 06-10 04:53:13.463: W/System.err(1138): java.lang.reflect.InvocationTargetException
 06-10 04:53:13.473: W/System.err(1138):    at    java.lang.reflect.Method.invokeNative(Native Method)
 06-10 04:53:13.473: W/System.err(1138):    at  java.lang.reflect.Method.invoke(Method.java:511)
 06-10 04:53:13.473: W/System.err(1138):    at org.cocos2d.actions.CCTimer.update(CCTimer.java:82)
  06-10 04:53:13.473: W/System.err(1138):   at org.cocos2d.actions.CCScheduler.tick(CCScheduler.java:253)
 06-10 04:53:13.473: W/System.err(1138):    at org.cocos2d.nodes.CCDirector.drawCCScene(CCDirector.java:679)
 06-10 04:53:13.473: W/System.err(1138):    at org.cocos2d.nodes.CCDirector.onDrawFrame(CCDirector.java:649)
 06-10 04:53:13.483: W/System.err(1138):    at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1516)
 06-10 04:53:13.483: W/System.err(1138):    at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
 06-10 04:53:13.483: W/System.err(1138): Caused by: java.lang.NullPointerException
 06-10 04:53:13.483: W/System.err(1138):    at com.trialcocos.GameL.update(GameL.java:183)
 06-10 04:53:13.483: W/System.err(1138):    ... 8 more

1 个答案:

答案 0 :(得分:0)

您需要将protected属性设置为值,否则为null。

protected CCSprite ship;

public void update(float dt){
    for (CCSprite target : _targets){
        CGRect targetRect = CGRect.make(target.getPosition().x - (target.getContentSize().width), target.getPosition().y - (target.getContentSize().height), target.getContentSize().width,target.getContentSize().height);
        System.out.println("shipstodelete : " + ship);
        CGRect shipRect = CGRect.make(ship.getPosition().x - (ship.getContentSize().width), ship.getPosition().y - (ship.getContentSize().height), ship.getContentSize().width,ship.getContentSize().height);
        if (CGRect.intersects(targetRect, shipRect)){
            this.removeChild(ship, true);
            //removeChild(ship, true);
        }
    }
}
...

public void someOtherMethodYouDontMention(){
    //Notice I set the previously declared ship value not a new ship value
    ship = CCSprite.sprite("ship150.png");
    ship.setPosition(CGPoint.ccp(25,100));
    ship.setAnchorPoint(CGPoint.ccp(0,0));
    ship.setTag(4);
    addChild(ship);
}