线程“AWT-EventQueue-0”中的异常java.lang.NullPointerException错误

时间:2015-10-23 09:04:32

标签: java swing nullpointerexception jframe

我是Java的新手,我试图创建一个双人战斗游戏,其中玩家的动作由键盘控制。我没有发现任何语法错误或结构,但它会引发错误。

我的代码如下:

我的测试员班:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Tester extends JPanel implements KeyListener, Runnable,
MouseListener {

/**
 * 
 */
private static final long serialVersionUID = 1L;

/* Player class controls the actions of 1st player
 * Player2 class controls the actions of 2nd player
 * Health class controls increment or decrement in the health of the players */

Player p1;    
Player p1;
Player2 p2;
Health health;
Image img, end;
long startingTime, cumTime;
Image bg;
private boolean running;

public Tester() {
    running = true;

    p1 = new Player(500);
    p2 = new Player2();
    health = new Health(p1, p2);

}

public void loadImage() {
    try {
        img = ImageIO.read(new File(
                "D:/work/Mortal_combart/Falling@gettingUp/f05.gif"));
        end = ImageIO.read(new File("D:/work/Sprites/Dragon/end.png"));
        bg = ImageIO.read(new File("D:/work/Mortal_combart/Arena /pit.png"));
        p1.loadImage();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

@Override
protected void paintComponent(Graphics g) {
    // TODO Auto-generated method stub
    super.paintComponent(g);
    g.drawImage(bg, 0, 0, null);
    if (p1.exsists)
        p1.draw(g);
    else {

        g.drawImage(img, Math.round(p1.getX()), Math.round(p1.getY()), null);
        g.drawImage(end, 100, 300, null);
        g.setColor(Color.WHITE);
        g.drawRect(280, 470, 100, 40);
        g.drawString("TRY Again ?", 300, 500);
    }

    if (p2.exsists)
        p2.draw(g);
    else {
        g.drawImage(end, 100, 300, null);
        g.setColor(Color.WHITE);
        g.drawRect(280, 470, 100, 40);
        g.drawString("TRY Again ?", 300, 500);
    }
}

public static void main(String args[]) {
    Tester obj = new Tester();
    JFrame window = new JFrame();
    window.add(obj);
    window.setSize(800, 600);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);

    Thread th = new Thread(obj);
    th.start();
}

public void run() {
    // TODO Auto-generated method stub
    loadImage();
    movieLoop();
}

public void movieLoop() {
    long startingTime = System.currentTimeMillis();
    cumTime = startingTime;
    while (running) {
        long timePassed = System.currentTimeMillis() - cumTime;
        cumTime += timePassed;
        p1.update(timePassed, getWidth(), getHeight());
        p2.update(timePassed, p1);
        repaint();

    }
}
@Override
public void keyPressed(KeyEvent arg0) {
    // TODO Auto-generated method stub
    int keyCode = arg0.getKeyCode();
    Game game = p1.getGame();
    if (keyCode == KeyEvent.VK_ESCAPE) {
        running = false;
    } else if (keyCode == KeyEvent.VK_1 && p1.exsists) {
        game.setCurrent(keyCode);

    } else if (keyCode == KeyEvent.VK_2 && p1.exsists) {
        game.setCurrent(keyCode);
    } else if (keyCode == KeyEvent.VK_3 && p1.exsists) {
        game.setCurrent(keyCode);
    } else {
        arg0.consume();
    }
}

@Override
public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub
    if (p1.exsists)
        p1.getGame().setCurrent(KeyEvent.VK_0);
}

@Override
public void keyTyped(KeyEvent arg0) {
    // TODO Auto-generated method stub
    arg0.consume();
}

@Override
public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub
    if (e.getX() > 280 && e.getX() < 380 && e.getY() > 470
            && e.getY() < 510) {
        p1.restart();
        p2.restart();
        startingTime = System.currentTimeMillis();
        cumTime = startingTime;
    }
}

@Override
public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

}

我的玩家类:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;

public class Player {
/*
 * Game class sets the the sprite to be drawn on screen based on key pressed
 * Sprites class controls the sprites of the game
 * Animation class handles adding or removing of one scene from the game
 */

private Game game;
private Sprites s;
private Animation animate;
private List<Sprites> sList;
public long health, defHealth;
public boolean exsists;
Image img, end, logo2;

public Player(long health) {
    this.health = health;
    this.defHealth = health;
    exsists = true;
}

public void loadImage() {
    try {

        logo2 = ImageIO.read(new File("D:/work/Sprites/Dragon/logo.png"));

        sList = new ArrayList<Sprites>();

        animate = new Animation(10, 1);
        animate.addScene(ImageIO.read(new File(
                "D:/work/Mortal_combart/punching1/01.gif")), 250);
        animate.addScene(ImageIO.read(new File(
                "D:/work/Mortal_combart/punching1/02.gif")), 250);
        animate.addScene(ImageIO.read(new File(
                "D:/work/Mortal_combart/punching1/03.gif")), 250);
        animate.addScene(ImageIO.read(new File(
                "D:/work/Mortal_combart/punching1/04.gif")), 250);
        animate.addScene(ImageIO.read(new File(
                "D:/work/Mortal_combart/punching1/05.gif")), 250);
        animate.addScene(ImageIO.read(new File(
                "D:/work/Mortal_combart/punching1/06.gif")), 250);
        s = new Sprites(animate, "sprites 2");
        s.setVelocityX(0);
        s.setVelocityY(0);
        sList.add(s);

        animate = new Animation(0, 3);
        animate.addScene(ImageIO.read(new File(
                "D:/work/Mortal_combart/FightingStance/a01.gif")), 100);
        animate.addScene(ImageIO.read(new File(
                "D:/work/Mortal_combart/FightingStance/a02.gif")), 100);
        animate.addScene(ImageIO.read(new File(
                "D:/work/Mortal_combart/FightingStance/a03.gif")), 100);
        animate.addScene(ImageIO.read(new File(
                "D:/work/Mortal_combart/FightingStance/a04.gif")), 100);
        animate.addScene(ImageIO.read(new File(
                "D:/work/Mortal_combart/FightingStance/a05.gif")), 100);
        animate.addScene(ImageIO.read(new File(
                "D:/work/Mortal_combart/FightingStance/a06.gif")), 100);

        s = new Sprites(animate, "sprites 1");
        s.setVelocityX(0);
        s.setVelocityY(0);
        sList.add(s);

        animate = new Animation(100, 80);
        animate.addScene(ImageIO.read(new File(
                "D:/work/Mortal_combart/Finishers/01.gif")), 250);
        animate.addScene(ImageIO.read(new File(
                "D:/work/Mortal_combart/Finishers/02.gif")), 250);
        animate.addScene(ImageIO.read(new File(
                "D:/work/Mortal_combart/Finishers/03.gif")), 250);
        animate.addScene(ImageIO.read(new File(
                "D:/work/Mortal_combart/Finishers/04.gif")), 250);
        animate.addScene(ImageIO.read(new File(
                "D:/work/Mortal_combart/Finishers/05.gif")), 250);
        animate.addScene(ImageIO.read(new File(
                "D:/work/Mortal_combart/Finishers/06.gif")), 250);
        animate.addScene(ImageIO.read(new File(
                "D:/work/Mortal_combart/Finishers/07.gif")), 250);
        animate.addScene(ImageIO.read(new File(
                "D:/work/Mortal_combart/Finishers/08.gif")), 250);
        s = new Sprites(animate, "sprites 2");
        s.setVelocityX(0);
        s.setVelocityY(0);
        sList.add(s);

        animate = new Animation(50, 50);
        animate.addScene(ImageIO.read(new File(
                "D:/work/Mortal_combart/jumping3/f01.gif")), 250);
        animate.addScene(ImageIO.read(new File(
                "D:/work/Mortal_combart/jumping3/f02.gif")), 250);
        animate.addScene(ImageIO.read(new File(
                "D:/work/Mortal_combart/jumping3/f03.gif")), 250);
        animate.addScene(ImageIO.read(new File(
                "D:/work/Mortal_combart/jumping3/f04.gif")), 250);
        animate.addScene(ImageIO.read(new File(
                "D:/work/Mortal_combart/jumping3/f05.gif")), 250);
        animate.addScene(ImageIO.read(new File(
                "D:/work/Mortal_combart/jumping3/f06.gif")), 250);
        animate.addScene(ImageIO.read(new File(
                "D:/work/Mortal_combart/jumping3/f07.gif")), 250);
        animate.addScene(ImageIO.read(new File(
                "D:/work/Mortal_combart/jumping3/f08.gif")), 250);
        s = new Sprites(animate, "sprites 2");
        s.setVelocityX(0.25f);
        s.setVelocityY(0);
        sList.add(s);
        game = new Game(sList);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void draw(Graphics g) {
    g.drawImage(logo2, 0, 0, null);
    g.setColor(Color.WHITE);
    g.drawString(health + " ", 100, 150);
    if (exsists)
        g.drawImage(game.getImage(), Math.round(game.getX()),
                Math.round(game.getY()), null);
}

public void update(long timePassed, int width, int height) {
    if (exsists)
        game.update(timePassed, width, height);
}

public Game getGame() {
    return game;
}

public float getX() {
    return game.getX();
}

public void setX(float x) {
    game.setX(x);
}

public float getY() {
    return game.getY();
}

public void setY(float y) {
    game.setX(y);
}

public long curAnimationDefence() {
    return game.curAnimationDefence();
}

public long curAnimationPower() {
    return game.curAnimationPower();
}

public void decHealth(long dec) {
    health -= Math.abs(dec);

}

public void restart() {
    exsists = true;
    health = defHealth;
    game.start();
    setX(0);
}

}

抛出错误代码:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at gameon_check.Player.draw(Player.java:127)
at gameon_check.Tester.paintComponent(Tester.java:61)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1200(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

调试程序一段时间之后,我发现如果我将播放器类的draw方法中的if语句更改为: if (exsists && game!=null)它没有抛出任何异常。由于我已经在我的播放器类的加载图像函数中设置了游戏的值,我无法弄清楚为什么会抛出这样的异常。

我的游戏课程:

package gameon_check;


import java.awt.Image;
import java.awt.event.KeyEvent;
import java.util.List;

public class Game {
List<Sprites> aList;
Sprites cAnimate;
int prevCode;

public Game(List<Sprites> aList) {
    this.aList = aList;
    cAnimate = aList.get(1);
    prevCode = KeyEvent.VK_0;
}

public void update(long timePassed, int w, int h) {
    if (cAnimate.getX() < 0) {
        cAnimate.setX(w - cAnimate.getWidth());
    } else if (cAnimate.getX() + cAnimate.getWidth() >= w)
        cAnimate.setX(0);
    cAnimate.update(timePassed);
}

public void setCurrent(int keyCode) {
    float x = cAnimate.getX();
    if (keyCode == KeyEvent.VK_0) {
        cAnimate = aList.get(1);
        cAnimate.start();
        cAnimate.setX(x);
        prevCode = KeyEvent.VK_0;
    } else if (keyCode == KeyEvent.VK_1 && prevCode == KeyEvent.VK_0) {
        cAnimate = aList.get(0);
        cAnimate.start();
        cAnimate.setX(x);
        prevCode = KeyEvent.VK_1;
    } else if (keyCode == KeyEvent.VK_2 && prevCode == KeyEvent.VK_0) {
        cAnimate = aList.get(2);
        cAnimate.start();
        cAnimate.setX(x);
        prevCode = KeyEvent.VK_2;
    } else if (keyCode == KeyEvent.VK_3 && prevCode == KeyEvent.VK_0) {
        cAnimate = aList.get(3);
        cAnimate.start();
        cAnimate.setX(x);
        prevCode = KeyEvent.VK_3;
    }
}

public Image getImage() {
    return cAnimate.getImage();
}

public float getX() {
    return cAnimate.getX();
}

public float getY() {
    return cAnimate.getY();
}

public String info() {
    return cAnimate.info();
}
public void setX(float x)
{
    cAnimate.setX(x);
}
public void setY(float y)
{
    cAnimate.setY(y);
}

public long curAnimationDefence() {
    // TODO Auto-generated method stub
    return cAnimate.curAnimationDefence();
}

public long curAnimationPower() {
    // TODO Auto-generated method stub
    return cAnimate.curAnimationPower();
}

public void start()
{
    cAnimate = aList.get(1);
    prevCode = KeyEvent.VK_0;
    cAnimate.start();
}

}

0 个答案:

没有答案