NullPointerException Java Maze

时间:2013-12-14 20:48:16

标签: java nullpointerexception

基本上我一直在关注一个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值一样,但它为我提供了异常错误。

代码: 地图类

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);
            }
        }
    }
}
}

1 个答案:

答案 0 :(得分:3)

问题出在以下代码中:在您的readFile()方法中,您在阅读文件时出现问题:

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

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

您的解决方案如下:

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

我希望这会有所帮助。