gpt4 book ai didi

java - NullPointerException Java 迷宫

转载 作者:行者123 更新时间:2023-12-01 23:21:16 28 4
gpt4 key购买 nike

基本上,我一直在按照 Java 教程制作一个基本的迷宫游戏,其中我生成一个随机迷宫,并将其保存到文件中,然后使用 Jpanel 将其打印出来,但是在编译时我不断收到此错误。

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at maze.Map.getMap(Map.java:43)
at maze.Board.paint(Board.java:40)

但是我不确定为什么,我相信这与我的 Y 整数有关,但如果我将其设置为 0,它会导致我的程序打印错误(只打印同一列而不是更改行)。

我需要 Map.java:43 中的 Y 值像我的 x 值一样增加,但是它为我提供了异常错误。

代码: map 类

package maze;

import java.awt.Image;
import java.io.File;
import java.util.*;
import javax.swing.ImageIcon;

public class Map {

private Scanner m;
private Image wall, ground;
//number of columns
private String Map[] = new String[20];

public Map() {


//sets ground to a texture
ImageIcon img = new ImageIcon("C:\\MazeGraphics\\ground.png");
ground = img.getImage();
//sets wall texture
img = new ImageIcon("C:\\MazeGraphics\\wall.png");
wall = img.getImage();

openFile();
readFile();
closeFile();
}

public Image getGround() {
// gives grass texture for screen
return ground;
}

public Image getWall() {
// gives wall texture for screen
return wall;
}

public String getMap(int x, int y) {
//compares string to find graphic to use

String index= Map[y].substring(x, x+1);
return index;
}

public void openFile() {
try {
m = new Scanner(new File("C:\\MazeGraphics\\Maze.txt"));

} catch (Exception e) {
System.out.println("Error Loading Map");
}
}

public void readFile() {
for(int i = 0; i < 20; i++)
{
while(m.hasNext()){
Map[i] = m.next();
}
}
}

public void closeFile() {
m.close();
}
}

板级:

package maze;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Board extends JPanel implements ActionListener {

private Timer timer;
private Map m;
private Player p;

public Board()
{
MazeGeneration maz=new MazeGeneration();
//runs Map class
m = new Map();
p = new Player();
//listens for key presses
addKeyListener(new Al());
//knows to add key listener to the frame
setFocusable(true);

timer = new Timer(25, this);
timer.start();
}

public void actionPerformed(ActionEvent e) {
repaint();
}

public void paint(Graphics g) {
super.paint(g);
//paints the board row by row
for (int y = 0; y < 20; y++)
{
for (int x = 0; x < 20; x++)
{
//places graphic depending on the letter.
if (m.getMap(x, y).equals("g"))
{
g.drawImage(m.getGround(), y * 32, x * 32, null);
}
if (m.getMap(x, y).equals("w"))
{
g.drawImage(m.getWall(), y * 32,x * 32, null);
}
}
}
g.drawImage(p.getPlayer(), p.gettileX() * 32, p.gettileY() * 32, null);
}

public class Al extends KeyAdapter {

//moving the player
public void keyPressed(KeyEvent e, char maz[][], int r, int c) {
int keycode = e.getKeyCode();
if (keycode == KeyEvent.VK_W) {
//if not a wall can move
if (!m.getMap(p.gettileX(), p.gettileY() - 1).equals("w")) {
p.move(0, -1);
}
}
if (keycode == KeyEvent.VK_S) {
if (!m.getMap(p.gettileX(), p.gettileY() + 1).equals("w")) {
p.move(0, 1);
}
}
if (keycode == KeyEvent.VK_A) {
if (!m.getMap(p.gettileX() - 1, p.gettileY()).equals("w")) {
p.move(-1, 0);
}
}
if (keycode == KeyEvent.VK_D) {
if (!m.getMap(p.gettileX() + 1, p.gettileY() ).equals("w")) {
p.move(1, 0);
}
}
}
}
}

最佳答案

问题出在这段代码中:在您的 readFile() 方法中,在读取文件期间出现问题:

for(int i = 0; i < 20; i++)
{
while(m.hasNext()){
Map[i] = m.next();
}
}

您仅将值分配给第 0 个索引。解释一下,对于第一个 for 循环,索引 i 为零。内部循环“迭代”扫描仪 m 直到没有下一个元素。因此,Map[0] 包含文件中的最后一个元素。

您的解决方案如下:

int i = 0;
while (m.hasNext()) {
Map[i++] = m.next();
}

我希望这会有所帮助。

关于java - NullPointerException Java 迷宫,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20588096/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com