错误 eventqueue.java_线程“AWT-EventQueue-0”中的异常java.lang.NullPointerException [duplicate]...

这个问题在这里已有答案:

我知道这个异常被问到的频率,我已经阅读了很多关于这个问题的线索,但我仍然不知道为什么这个错误总是出现,当我删除'mf'变量时,它确实有效,但没有显示图片起来 .

代码 . 名称的游戏“AWT-EventQueue-0”java.lang.NullPointerException java.lang.NullPointerException(game.java:118) . 代码.MettigelsNightmare . (game.java:105) . (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 at java.awt.EventQueue.access $ 700(EventQueue.java:97)java.awt.EventQueue $ 3.run(EventQueue.java:709)java.awt.EventQueue $ 3.run(EventQueue.java:703)at java . java.security.ProtectionDomain上的security.AccessController.doPrivileged(本机方法)$ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76),位于java.awt.EventDispatchThread.pumpOneEventForFilters的java.awt.EventQueue.dispatchEvent(EventQueue.java:726) EventDispatchThread.java:201)java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventD) ispatchThread.java:105)java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)at 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);

}

}

}

参与评论 您还未登录,请先 登录 后发表或查看评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:数字20 设计师:CSDN官方博客 返回首页

打赏作者

Bella S

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值