我想写一个小型的2D 2D游戏,但是当我启动它时总是会出现黑屏和错误:
线程中的异常" AWT-EventQueue-0"显示java.lang.NullPointerException 在code.game.spawn_a_point(game.java:106)at code.game。(game.java:95)at code.MettigelsNightmare。(MettigelsNightmare.java:14)at code.MettigelsNightmare $ 1.run(MettigelsNightmare.java:31)at at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)at 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) 在java.awt.EventQueue.dispatchEvent(EventQueue.java:726)at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) 在 java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) 在 java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) 在java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
我的代码:
package code;
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.lang.reflect.Array;
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 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();
spawn_a_point();
running = 1;
t = new Timer(timerspeed, this);
t.start();
}
private void spawn_a_point() {
for (int i = 0; i < mf.length; i++) {
int r = new Random().nextInt(2) + 1;
Image[] mf = new Image[i];
if (r == 1) {
mf[i] = frau;
} else {
mf[i] = mann;
}
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 < mf.length; 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);
}
}
}
答案 0 :(得分:0)
我只看到一个初始化'mf'成员的地方:
private void spawn_a_point() {
if (mf != null) {
for (int i = 0; i < mf.length; i++) {
int r = new Random().nextInt(2) + 1;
Image[] mf = new Image[i];
if (r == 1){
mf[i] = frau;
} else {
mf[i] = mann;
}
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;
}
}
}
你说:
if (mf != null)
然后,此功能永远不会执行。