如何初始化数组以避免空指针异常?

时间:2016-12-05 22:38:18

标签: java arrays nullpointerexception

当我尝试在方法参数中调用布尔数组时,我的程序中出现了nullPointerException。布尔数组创建为常量,然后在单独的void方法中初始化。有人可以向我解释为什么它找不到布尔数组(或找不到数组值)?

public static void moveTarget(Graphics g) {
  if (!targetMovement)
     return;
  drawTarget(g, BACKGROUND_COLOR);
  drawShield(g, BACKGROUND_COLOR);

这一行就是程序失败的具体部分。 (空指针异常)

  int f = findTargetMissilePosition(targetMissileActive);

我在这里指定常量,但不要初始化。

     // Target Missile values
     public static Color TARGET_MISSILE_COLOR = TARGET_COLOR;
     public static int MAX_MISSILES = 10;
     public static double TARGET_MISSILE_SPEED = MISSILE_SPEED;
     public static double TARGET_SHOOT_PROBABILITY = .1;
     public static double[] targetMissilePositionX;
     public static double[] targetMissilePositionY;
     public static double[] targetMissileDeltaX;
     public static double[] targetMissileDeltaY;
     public static boolean[] targetMissileActive;

     // main method does initialization and calls startGrame
     public static void main(String[] args) {
        DrawingPanel panel = new DrawingPanel(PANEL_WIDTH, PANEL_HEIGHT);
        Graphics g = panel.getGraphics( );
        initialize();
        startGame(panel, g);

}       //上面调用了initialize(),这是数组初始化的地方

    // start the main game loop which runs forever
    public static void startGame(DrawingPanel panel, Graphics g) {
        while(true) {
           panel.sleep(SLEEP_TIME);
           handleKeys(panel,g);
           moveTarget(g);
           drawAll(g);
           moveMissile(g);
           shootTargetMissile(g);
           for (int i=0;i<10;i++) {
             int f = findTargetMissilePosition(targetMissileActive);
             moveTargetMissile(g, f);
     }
           shieldHitTimer--;
           targetHitTimer--;
           shooterHitTimer--;
  }

}       //上面是第一次显示代码行并且似乎没有问题。

  // reset all parameters to start over
  public static void reset(Graphics g) {
        g.setColor(BACKGROUND_COLOR);
        g.fillRect(0,0,PANEL_WIDTH,PANEL_HEIGHT);
        initialize();

}

  //Here the arrays are initialized
     // initialize parameters for the start of the program
     public static void initialize() {
  shooterPositionX = SHOOTER_INITIAL_POSITION_X;
  gunAngle = 0;
  targetPositionX = PANEL_WIDTH/2;
  missileActive = false;
  hitCount = 0;
  shooterHitCount = 0;
  hitDisplayString = "Hits: ";
  targetDeltaX = 0;
  targetHitTimer = 0;
  shieldHitTimer = 0;
  shooterHitTimer = 0;
  boolean [] targetMissileActive = new boolean [9]; // might change??
  int [] targetMissilePositionX = new int [9];
  int [] targetMissilePositionY = new int [9];
  int [] targetMissileDeltaX = new int [9];
  int [] targetMissileDeltaY = new int [9];
  for (int i=0; i<9; i++) {
    targetMissilePositionX[i] = 0;
    targetMissilePositionY[i] = 0;
    targetMissileDeltaX[i] = 0;
    targetMissileDeltaY[i] = 0;
    targetMissileActive[i] = false;
}

}

     // draw everything in its current position
     public static void drawAll(Graphics g) {
  g.setColor(Color.BLACK);
  g.drawString("Project 3 by Benjamin Koch",10,15);
  g.drawString(hitDisplayString,10,30);

  int f = findTargetMissilePosition(targetMissileActive);
  drawTargetMissile(g, TARGET_MISSILE_COLOR, f);
  drawShooter(g,SHOOTER_COLOR);
  if (targetHitTimer > 0)
     drawTarget(g, SHIELD_HIT_COLOR);
  else
     drawTarget(g,TARGET_COLOR);
  Color shieldColor = BACKGROUND_COLOR; // default: do not draw
  if (shieldActive) {
     if (shieldHitTimer > 0)
        shieldColor  = SHIELD_HIT_COLOR;
     else
        shieldColor = SHIELD_COLOR;
  }
  drawShield(g, shieldColor);

}

     public static int findTargetMissilePosition (boolean[] data) {
  for (int i=0;i<MAX_MISSILES;i++) {
    if (data[i]==false) {
      return i;
    } 
  }
  return -1;

}

1 个答案:

答案 0 :(得分:0)

initialize()功能中,您可以创建局部变量但不会初始化全局变量。

 targetMissileActive = new boolean [9];
  targetMissilePositionX = new int [9];
  targetMissilePositionY = new int [9];
  targetMissileDeltaX = new int [9];
  targetMissileDeltaY = new int [9];
  for (int i=0; i<9; i++) {
    targetMissilePositionX[i] = 0;
    targetMissilePositionY[i] = 0;
    targetMissileDeltaX[i] = 0;
    targetMissileDeltaY[i] = 0;
    targetMissileActive[i] = false;
}

这会将变量引用到全局变量。