Exception in thread "AWT-EventQueue-0" and Exception in thread "main"
Maze.java
Code Java:
import javax.swing.*;
public class Maze
{
public static void main(String[] args)
{
JFrame mainframe = new JFrame();
mainframe.add(new Board());
mainframe.setTitle("Noah's Game");
mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainframe.setSize(512,512);
mainframe.setLocation(null);
mainframe.setVisible(true);
}
}
Board.java
Code Java:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;
import java.*;
public class Board extends JPanel implements ActionListener {
private Map m;
private Timer timer;
private Player p;
private String Message = "Winner!";
private boolean Win = false;
private Font font = new Font("Serif", Font.BOLD,48);
public Board() {
timer = new Timer(25, this);
timer.start();
m = new Map();
p = new Player();
addKeyListener(new Al());
setFocusable(true);
}
public void actionPerformed(ActionEvent e) {
repaint();
if(m.getMap(p.getTileX(), p.getTileY()).equals("f"))
{
Message = "Winner!";
Win = true;
}
}
public void paint(Graphics g) {
super.paint(g);
if(!Win){
for (int y = 0; y < 16; y++) {
for (int x = 0; x < 16; x++) {
if (m.getMap(x, y).equals("f")) {
g.drawImage(m.getFinish(), x * 32, y * 32, null);
}
if (m.getMap(x, y).equals("g")) {
g.drawImage(m.getDirt(), x * 32, y * 32, null);
}
if (m.getMap(x, y).equals("w")) {
g.drawImage(m.getWall(), x * 32, y * 32, null);
}
}
}
g.drawImage(p.getPlayer(), p.getTileX() * 32, p.getTileY() * 32, null);
}
if(Win){
g.setColor(Color.MAGENTA);
g.setFont(font);
g.drawString(Message, 150, 300);
}
}
public class Al extends KeyAdapter {
public void keyPresed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_W) {
if (!m.getMap(p.getTileX(), p.getTileY() - 1).equals("w"))
;
p.move(0, -1);
}
if (e.getKeyCode() == KeyEvent.VK_A) {
if (!m.getMap(p.getTileX(), p.getTileY() + 1).equals("w"))
;
p.move(0, 1);
}
if (e.getKeyCode() == KeyEvent.VK_S) {
if (!m.getMap(p.getTileX() - 1, p.getTileY()).equals("w"))
;
p.move(-1, 0);
}
if (e.getKeyCode() == KeyEvent.VK_D) {
if (!m.getMap(p.getTileX() + 1, p.getTileY()).equals("w"))
;
p.move(1, 0);
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
}
}
Map.java
Code Java:
private String Map[] = new String[16];
private Image dirt ,
wall,
finish;
public void map()
{
ImageIcon img = new ImageIcon("C://Noah's World//dirt.png");
dirt = img.getImage();
img = new ImageIcon("C://Noah's World//wall.png");
wall = img.getImage();
img = new ImageIcon("C://Noah's World//finish.png");
finish = img.getImage();
openFile();
readFile();
closeFile();
}
public Image getFinish()
{
return finish;
}
public Image getDirt()
{
return dirt;
}
public Image getWall()
{
return wall;
}
public String getMap(int x, int y)
{
String index = Map[y].substring(x,x + 1);
return index;
}
public void openFile()
{
try{
m = new Scanner(new File("C://Noah's World//Map.txt"));
}catch(Exception e)
{
System.out.println("error loading map");
}
}
public void readFile()
{
while(m.hasNext())
{
for(int i = 0; i < 14; i++)
{
Map[i] = m.next();
}
}
}
public void closeFile()
{
m.close();
}
}
Player.java
Code Java:
import java.awt.Image;
import javax.swing.ImageIcon;
public class Player {
private int tileX, tileY;
private Image player;
public Player() {
ImageIcon img = new ImageIcon("C://Noah's World//Player.png");
player = img.getImage();
tileX = 1;
tileY = 1;
}
public Image getPlayer() {
return player;
}
public int getTileX() {
return tileX;
}
public int getTileY() {
return tileY;
}
public void move(int dx, int dy) {
tileX += dx;
tileY += dy;
}
}
and here is the error report
Re: Exception in thread "AWT-EventQueue-0" and Exception in thread "main"
Quote:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Component.setLocation(Component.java:2112 )
at java.awt.Window.setLocation(Window.java:934)
at Maze.main(Maze.java:13)
Look at line 13 in Maze for a null value. The java program expects a non-null value there.
Read the API doc to see if you are using the method correctly or if you should use another method.
Re: Exception in thread "AWT-EventQueue-0" and Exception in thread "main"
mainframe.setLocation(null);
Why? Maybe you mean setLocationRelativeTo(null), which will centre your window. That is your first problem anyhow.
Your "Map" array is of length 16, 0 - 15, how do you ensure that when you request a substring from the array that the index is valid?
As for how you name your variables, I suggest reading over Java naming conventions:
Code Conventions for the Java Programming Language: 9. Naming Conventions