Java android中的调用TargetException和空指针异常

Java android中的调用TargetException和空指针异常,java,android,nullpointerexception,cocos2d-android,invocationtargetexception,Java,Android,Nullpointerexception,Cocos2d Android,Invocationtargetexception,我在项目中有这部分代码,其中在这一行中获取“空指针异常”(ship.getPosition().x-(ship.getContentSize().width),,但我在类中将ship值声明为受保护的CCSprite ship;,但仍然获取“空指针异常”错误。 这是完整的代码 public void update(float dt) { for (CCSprite target : _targets) {

我在项目中有这部分代码,其中在这一行中获取“空指针异常”(ship.getPosition().x-(ship.getContentSize().width),,但我在类中将ship值声明为
受保护的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);
                    }
              }   
这是构造器中船精灵的编码

CCSprite ship = CCSprite.sprite("ship150.png");
     ship.setPosition(CGPoint.ccp(25,100));
     ship.setAnchorPoint(CGPoint.ccp(0,0));
     ship.setTag(4);
        addChild(ship);
对数输出

 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

您需要将受保护的属性设置为一个值,否则它将为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);
}

你在哪里设置“受保护的CCSprite ship;”的值?是的并不能真正回答我的问题…你在哪里设置“ship”的值?我将发布一个答案来解释我的意思…我没有在addTarget()中放置sprite(ship)的代码,只是在构造函数中声明了ship的值这是我犯的错误,还添加了_ships.remove(sprite);这段代码在“public void spriteMoveFinished(Object sender)”中完成了,所以这使它工作了,无论如何,谢谢:)@Larry McKenzie