获取图像时用尽netbeans时出现空指针异常

时间:2013-08-30 20:58:59

标签: java netbeans

在“animate”方法中获取空指针异常。不确定为什么当青蛙在我的游戏中上升时它起作用。但是当按下向右或向下时,它会出错。所有图像文件都在“图像”包中。它在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;
}
}

2 个答案:

答案 0 :(得分:0)

确保所有图像都出现在正确的目录/文件夹中。使用程序所期望的完整路径引入调试输出。像

这样的东西
System.err.println( new File("yourFileName").getAbsolutePath() );

答案 1 :(得分: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";

您必须显示资源目录的屏幕截图,以便我相信所有这些文件确实存在。