2013-08-30 40 views
0

在「animate」方法中獲取空指針異常。當青蛙在我的比賽中上升時,不確定爲什麼它會起作用。但是,如果按向右或向下,就會出錯。所有圖像文件都在「圖像」包中。它在netbeans中工作正常。任何幫助深表感謝。所有路徑都已經過徹底檢查。我將顯示所有代碼,因爲找不到原因。當獲取圖像時用完netbeans時出現空指針異常

package frogger; 
import java.awt.event.KeyEvent; 
import java.io.BufferedInputStream; 
import java.io.IOException; 
import javax.sound.sampled.AudioSystem; 
import javax.sound.sampled.Clip; 
import javax.sound.sampled.LineUnavailableException; 
import javax.sound.sampled.UnsupportedAudioFileException; 
import javax.swing.ImageIcon; 

public class Frog extends Sprite implements Commons { 

String frog = "/images/upFrogStill.png"; 
private int dx,dy; 
private long timeLastChg; 
private boolean leftReleased = true; 
private boolean rightReleased = true; 
private boolean upReleased = true; 
private boolean downReleased = true; 
private String audioFile = "/audio/hop.wav"; 
private Clip clip; 
private boolean frogReset; 

public Frog() { 
    ImageIcon ii = new ImageIcon(getClass().getResource(frog)); 
    image = ii.getImage(); 
    width = image.getWidth(null); 
    height = image.getHeight(null); 
    resetState(); 
} 
public void move() { 
    x += dx; 
    y += dy; 
    animate(); 
} 
public void animate() { 
     ImageIcon ii = new ImageIcon(this.getClass().getResource(frog)); 
     image = ii.getImage(); 
} 
public void muted() { 
    if (Board.soundOn) { 
     getSound(); 
     playSound(); 
    } 
    if (!Board.soundOn) { 
     if (clip != null) { 
      stopSound(); 
     } 
    } 
} 
private void getSound() { 
    BufferedInputStream inaudio = new BufferedInputStream(getClass().getResourceAsStream(audioFile)); 
    clip = null; 
    try { 
     clip = AudioSystem.getClip(); 
     clip.open(AudioSystem.getAudioInputStream(inaudio)); 
    } catch (LineUnavailableException | UnsupportedAudioFileException | IOException exc) { 
     exc.printStackTrace(System.out); 
    } 
} 
public void playSound() { 
    clip.start(); 
} 
public void stopSound() { 
    clip.stop(); 
} 
public void keyPressed(KeyEvent e) { 
    int key = e.getKeyCode(); 
    if (frog != null) { 
     if (System.currentTimeMillis() - timeLastChg > 250) { 
      if (key == KeyEvent.VK_LEFT) { 
       frog = "/images/leftFrogJump.png"; 
       frogReset = false; 
       getSound(); 
       playSound(); 
       muted(); 
       if (rightReleased && upReleased && downReleased) { 
        x -= 15; 
        if (x < -5) { 
         x += 15; 
        } 
       } 
       leftReleased = false; 
       animate(); 
      } 
      if (key == KeyEvent.VK_UP) { 
       frog = "/images/upFrogJump.png"; 
       frogReset = false; 
       getSound(); 
       playSound(); 
       muted(); 
       if (leftReleased && rightReleased && downReleased) { 
        y -= 15; 
       } 
       upReleased = false; 
       animate(); 
      } 
      if (key == KeyEvent.VK_DOWN) { 
       frog = "/images/downFrogJump.png"; 
       frogReset = false; 
       getSound(); 
       playSound(); 
       muted(); 
       if (leftReleased && rightReleased && upReleased) { 
        y += 15; 
        if (y > 410) { 
         y -= 15; 
        } 
       } 
       downReleased = false; 
       animate(); 
      } 
      if (key == KeyEvent.VK_RIGHT) { 
       frog = "/images/rightFrogJump.png"; 
       frogReset = false; 
       getSound(); 
       playSound(); 
       muted(); 
       if (leftReleased && upReleased && downReleased) { 
        x += 15; 
        if (x > 390) { 
         x -= 15; 
        } 
       } 
       rightReleased = false; 
       animate(); 
      } 
      timeLastChg = System.currentTimeMillis(); 
     } 
    } 
} 
public void keyReleased(KeyEvent e) { 
    int key = e.getKeyCode(); 
    if (frog != null) { 
     if (key == KeyEvent.VK_LEFT) { 
      frog = "/images/leftFrogStill.png"; 
      if (!leftReleased && !frogReset) { 
       x -= 15; 
       if (x < 0) { 
        x += 15; 
       } 
      } 
      leftReleased = true; 
      animate(); 
     } 
     if (key == KeyEvent.VK_RIGHT && !frogReset) { 
      frog = "/images/rightFrogStill.png"; 
      if (!rightReleased) { 
       x += 15; 
      } 
      if (x > 390) { 
       x -= 15; 
      } 
      rightReleased = true; 
      animate(); 
     } 
     //Up released 
     if (key == KeyEvent.VK_UP && !frogReset) { 
      frog = "/images/upFrogStill.png"; 
      if (!upReleased) { 
       y -= 15; 
      } 
      upReleased = true; 
      Board.scoreInt += 10; 
      animate(); 
     } 
     if (key == KeyEvent.VK_DOWN && !frogReset) { 
      frog = "/images/downFrogStill.png"; 
      if (!downReleased) { 
       y += 15; 
       if (y < 410) { 
        Board.scoreInt -= 10; 
       } 
       if (y > 410) { 
        y -= 15; 
       } 
      } 
      downReleased = true; 
      animate(); 
     } 
    } 
} 
final void resetState() { 
    frogReset = true; 
    frog = "/images/upFrogStill.png"; 
    x = 185; 
    y = 397; 
} 
} 
+1

如果您從應用程序類路徑的根目錄開始查找,是否存在此路徑'/ images/upFrogStill.png'?或者'/ audio/hop.wav'? –

+0

是的。它在開始時加載。這是當我按左或右或向下,它說在animate方法中的空指針異常。 –

+3

您能否顯示完整的堆棧跟蹤 –

回答

0

確保所有圖像都存在於正確的目錄/文件夾中。使用程序預期的完整路徑引入調試輸出。像

System.err.println(new File("yourFileName").getAbsolutePath()); 
+2

這些文件是'jar'/classpath中的資源。你不會爲此使用「文件」。 –

+0

文件在jar中實際上工作正常。使用此並發現錯誤。檢查了很多次,仍然沒有注意到它。其中一個圖像有一個字母小寫,而不是上部。爲什麼在NetBeans中它不區分大小寫,但是在jar文件外部是區分大小寫的? –

0

我拒絕東西,相信所有下列路徑的存在爲正確的資源:

frog = "/images/leftFrogJump.png"; 
frog = "/images/upFrogJump.png"; 
frog = "/images/downFrogJump.png"; 
frog = "/images/rightFrogJump.png"; 
frog = "/images/leftFrogStill.png"; 
frog = "/images/rightFrogStill.png"; 
frog = "/images/upFrogStill.png"; 
frog = "/images/downFrogStill.png"; 

你必須表現出你的資源目錄的截圖,我相信,所有這些文件真的存在。

+0

這是一個小寫字符問題。 DERP。 –

+0

告訴你:)請將回答標爲已接受。 (你也應該在你的幾個老問題上做到這一點) –

相關問題