线程中的异常" AWT-EventQueue-0"显示java.lang.NullPointerException

时间:2016-01-30 19:06:59

标签: java nullpointerexception

我知道这个异常被问到的频率,我已经阅读了很多关于这个问题的主题,但是当我删除“错误”时,我仍然不知道为什么会出现这个错误。变量,它确实有效,但没有图片出现。

  

线程中的异常" AWT-EventQueue-0"显示java.lang.NullPointerException       在code.game.spawn_a_point(game.java:118)       在code.game。(game.java:105)       at code.MettigelsNightmare。(MettigelsNightmare.java:14)       at code.MettigelsNightmare $ 1.run(MettigelsNightmare.java:31)       at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)       at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)       at java.awt.EventQueue.access $ 500(EventQueue.java:97)       at java.awt.EventQueue $ 3.run(EventQueue.java:709)       at java.awt.EventQueue $ 3.run(EventQueue.java:703)       at java.security.AccessController.doPrivileged(Native Method)       at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)       at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)       at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)       at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)       at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)       at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)       at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)       在java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

这就是守则:     包裹代码;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;

public class game extends JPanel implements ActionListener {
    private static final long serialVersionUID = 1L;
    private static int width = 1920;
    private static int height = 1000;

    private Image kopfR;
    private Image kopfL;
    private Image kopfO;
    private Image kopfU;

    private Image mann;
    private Image frau;
    private Image mf[];

    private Image shootIMG_L;
    private Image shootIMG_R;
    private Image shootIMG_O;
    private Image shootIMG_U;

    private int tail_amount = 3;
    private static int snake_width = 50;
    private static int x;
    private static int y;

    private static int mY[] = new int[width * height / (snake_width * snake_width)];
    private static int mX[] = new int[width * height / (snake_width * snake_width)];

    private int timerspeed = 250;
    private int points = 0;

    private static int shootX;
    private static int shootY;

    public static int shootD = -50;

    /*
     * 0 = left 1 = right 2 = up 3 = down
     */

    public static int direction;

    public static int running;

    public static Timer t;

    public game() {
        addKeyListener(new KeyBinds());
        setPreferredSize(new Dimension(width, height));
        setFocusable(true);
        setBackground(Color.DARK_GRAY);
        setVisible(true);

        ImageIcon icon_mann = new ImageIcon("assets/mann.png");
        ImageIcon icon_frau = new ImageIcon("assets/frau.png");

        ImageIcon icon_headL = new ImageIcon("assets/spielerlinks.png");
        ImageIcon icon_headR = new ImageIcon("assets/spielerrechts.png");
        ImageIcon icon_headO = new ImageIcon("assets/spieleroben.png");
        ImageIcon icon_headU = new ImageIcon("assets/spielerunten.png");

        ImageIcon icon_shootL = new ImageIcon("assets/plinks.png");
        ImageIcon icon_shootR = new ImageIcon("assets/prechts.png");
        ImageIcon icon_shootO = new ImageIcon("assets/poben.png");
        ImageIcon icon_shootU = new ImageIcon("assets/punten.png");

        mann = icon_mann.getImage();
        frau = icon_frau.getImage();

        kopfL = icon_headL.getImage();
        kopfR = icon_headR.getImage();
        kopfO = icon_headO.getImage();
        kopfU = icon_headU.getImage();

        shootIMG_L = icon_shootL.getImage();
        shootIMG_R = icon_shootR.getImage();
        shootIMG_O = icon_shootO.getImage();
        shootIMG_U = icon_shootU.getImage();

        for (int i = 0; i < tail_amount; i++) {
            x = 500 - i * snake_width;
            y = 400;
        }

        running = 1;

        t = new Timer(timerspeed, this);
        t.start();

        spawn_a_point();

    }

    private void spawn_a_point() {
        if (mann != null) {
            for (int i = 1; i < 30; i++) {

                int r = new Random().nextInt(2) + 1;

                if (r == 1) {
                    mf[i] = mann;
                } else {
                    mf[i] = frau;
                }

                int random_x = (int) (Math.random() * 39);
                mX[i] = random_x * snake_width;
                int random_y = (int) (Math.random() * 19);
                mY[i] = random_y * snake_width;
            }
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (running == 1) {
            check_shoot();
            check_death();
            get_a_point();
            move();
            move_shoot();
        }
        repaint();
    }

    private void move_shoot() {
        switch (shootD) {

        case 0:
            shootX -= snake_width;
            break;
        case 1:
            shootX += snake_width;
            break;
        case 2:
            shootY -= snake_width;
            break;
        case 3:
            shootY += snake_width;
            break;
        default:
            break;
        }
    }

    static void shoot() {
        shootX = x;
        shootY = y;
        switch (shootD) {

        case 0:
            shootX -= snake_width;
            break;
        case 1:
            shootX += snake_width;
            break;
        case 2:
            shootY -= snake_width;
            break;
        case 3:
            shootY += snake_width;
            break;
        default:
            break;
        }
    }

    public void check_shoot() {
        for (int i = 0; i < 30; i++) {

            if (shootX == mX[i] && shootY == mY[i]) {
                timerspeed -= 30;
                points++;
            }
        }

    }

    private void move() {
        /*
         * 0 = left 1 = right 2 = up 3 = down
         */
        switch (direction) {

        case 0:
            x -= snake_width;
            break;
        case 1:
            x += snake_width;
            break;
        case 2:
            y -= snake_width;
            break;
        case 3:
            y += snake_width;
            break;
        default:
            break;
        }
    }

    private void check_death() {

        if (y >= height || x >= width || y < 0 || x < 0) {
            running = 0;
        }
        if (running == 0 || running == 2) {
            t.stop();
        }
    }

    private void get_a_point() {
        for (int i = 0; i < 30; i++) {
            if (x == mX[i] && y == mY[i]) {
                timerspeed -= 30;
                points++;
                mX[i] = 5000;
            }
        }

    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (running == 1) {

            if (shootD == 0) {
                g.drawImage(shootIMG_L, shootX, shootY, this);
            } else if (shootD == 1) {
                g.drawImage(shootIMG_R, shootX, shootY, this);
            } else if (shootD == 2) {
                g.drawImage(shootIMG_O, shootX, shootY, this);
            } else if (shootD == 3) {
                g.drawImage(shootIMG_U, shootX, shootY, this);
            }

            for (int i = 0; i < 30; i++) {
                g.drawImage(mf[i], mX[i], mY[i], this);
            }

            Font fa = new Font("Calibri", Font.BOLD, 50);

            g.setColor(Color.GREEN);
            g.setFont(fa);

            g.drawString("" + points, 960, 50);

            /*
             * 0 = left; 1 = right; 2 = up; 3 = down;
             */
            if (direction == 0) {
                g.drawImage(kopfL, x, y, this);
            } else if (direction == 1) {
                g.drawImage(kopfR, x, y, this);
            } else if (direction == 2) {
                g.drawImage(kopfO, x, y, this);
            } else if (direction == 3) {
                g.drawImage(kopfU, x, y, this);
            }

            Toolkit.getDefaultToolkit().sync();
        } else if (running == 0) {
            Font f = new Font("Calibri", Font.BOLD, 40);

            g.setColor(Color.RED);
            g.setFont(f);

            g.drawString("Du hast verloren!", 450, height / 2);
        } else {
            Font f = new Font("Calibri", Font.BOLD, 20);

            g.setColor(Color.RED);
            g.setFont(f);

            g.drawString("Mettigels Nightmare ist pausiert!", 460, height / 2);
            g.drawString("Mit ENTER kannst du weiterspielen", 433, height / 2 + 20);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的数组mfprivate Image mf[];)已声明但从未初始化... 然后在你正在做的spawn_a_point方法中:

 if (r == 1) {
     mf[i] = mann;
 } else {
   mf[i] = frau;
 }

但是mf有一个空引用,因此 Nullpointer异常