getting error for my game in Java code package maze; import javax.swing.JFrame; public class Maze { public static void main(String[] args) { new Maze(); } public Maze(){ JFrame f = new JFrame(); f.setTitle("Maze Game"); f.setSize(500, 400); f.setLocationRelativeTo(null); f.add(new Board()); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
this is what i got: error loading map Exception in thread "main" java.lang.NullPointerException at maze.Map.readFile(Map.java:55) at maze.Map.<init>(Map.java:23) at maze.Board.<init>(Board.java:14) at maze.Maze.<init>(Maze.java:17) at maze.Maze.main(Maze.java:10)
can you post your code here or another site to view it? I hate downloading all this code to my comp...
maze.java= package maze; import javax.swing.JFrame; public class Maze { public static void main(String[] args) { new Maze(); } public Maze(){ JFrame f = new JFrame(); f.setTitle("Maze Game"); f.setSize(500, 400); f.setLocationRelativeTo(null); f.add(new Board()); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
player.java = package maze; import java.awt.Image; import javax.swing.ImageIcon; public class Player { private int tileX, tileY; private Image player; public Player(){ ImageIcon img= new ImageIcon("//users//Singh//Documents//test//tutorail//smiley.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; } }
map.java= package maze; import java.awt.*; import java.io.*; import java.util.*; import javax.swing.ImageIcon; public class Map { private Scanner j; private String Map[]=new String[16]; private Image grass, finish, wall; public Map() { ImageIcon img= new ImageIcon("//users//Singh//Documents//test//tutorail//grass.png"); grass = img.getImage(); img= new ImageIcon("//users//Singh//Documents//test//tutorail//wall.png"); wall=img.getImage(); img= new ImageIcon ("//users//Singh//Documents//test//tutorail//grass.png"); finish=img.getImage(); openFile1(); readFile(); closeFile(); } public Image getGrass() { return grass; } public Image getWall() { return wall; } public Image getFinish(){ return finish; } public String getMap(int x, int y){ String index=Map[y].substring(x, x+1); return index; } public void openFile1(){ try { j=new Scanner(new File("//users//Singh//Documents//test//tutorail//Map.txt")); }catch(Exception e) { System.out.println("error loading map"); } } public void readFile() { while(j.hasNext()) { for(int i=0;i<14;i++) { Map[i]=j.next(); } } } public void closeFile() { j.close(); } }
Board.java= package maze; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Board extends JPanel implements ActionListener { private Timer timer; private Map j; private Player p; private String Message=" "; public Board(){ j= new Map(); p=new Player(); addKeyListener(new Al()); setFocusable(true); timer=new Timer(25, this); timer.start(); } public void actionPerformed(ActionEvent e) { if(j.getMap(p.getTileX(), p.getTileY()).equals("f")){ Message="Winner"; } repaint(); } public void paint(Graphics g) { super.paint(g); for(int y=0;y<16;y++){ for(int x=0;x<16;x++){ if(j.getMap(x, y).equals("f")){ g.drawImage(j.getFinish(),x*32,y*32,null); } if(j.getMap(x, y).equals("g")){ g.drawImage(j.getGrass(),x*32,y*32,null); } if(j.getMap(x, y).equals("w")){ g.drawImage(j.getWall(),x*32,y*32,null); } } } g.drawString(Message, 50, 50); g.drawImage(p.getPlayer(), p.getTileX()*32, p.getTileY()*32,null); } public class Al extends KeyAdapter{ public void ketPressed(KeyEvent e){ int keycode=e.getKeyCode(); if (keycode == KeyEvent.VK_W) { if (!j.getMap(p.getTileX(), p.getTileY() - 1).equals("w")) { p.move(0, -1); } } if (keycode == KeyEvent.VK_S) { if (!j.getMap(p.getTileX(), p.getTileY() + 1).equals("w")) { p.move(0, 1); } } if (keycode == KeyEvent.VK_A) { if (!j.getMap(p.getTileX() - 1, p.getTileY()).equals("w")) { p.move(-1, 0); } } if (keycode == KeyEvent.VK_D) { if (!j.getMap(p.getTileX() - 1, p.getTileY()).equals("w")) { p.move(1, 0); } } } public void KeyRelased(KeyEvent e){ } public void keyTyped(KeyEvent e){ } } }
it says error loading map i think since im using a .txt file for the map it wont work so what could i use
figured out how to add .txt file on Macs
in your code you should have put main method at last. function Maze() it has to be super . JFrame has to be declared private and out of function maze() but inside the class " public class Maze " .
j=new Scanner(new File("//users//Singh//Documents//test//tutorail//Map.txt")); I think you need C: Also tutorial isn't spelled correctly if that might cause an issue. I don't know why you're reading in a text file....?
Join our real-time social learning platform and learn together with your friends!