2013-08-30 39 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

告诉你:)请将回答标为已接受。 (你也应该在你的几个老问题上做到这一点) –

相关问题