Results 1 to 5 of 5
  1. #1
    augustas656 is online now Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    118
    Rep Power
    0

    Exclamation Can't figure out why I'm getting Null Pointer Exception

    Game (Class):
    Java Code:
    package pack.script.game;
    
    import java.awt.BorderLayout;
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.image.BufferStrategy;
    import java.awt.image.BufferedImage;
    //import java.awt.image.DataBufferInt;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    
    public class Game extends Canvas implements Runnable, KeyListener {
    	private static final long serialVersionUID = 1L;
    	
    	public boolean iRUNNING = false;
    	
    	public static final String sNAME = "Java Game";
    	public static final String sVER = "v1.00";
    	public static final int sSIZE = 20;
    	public static final int sSCALE = 16;
    	public static final int sBORDERS = 5;
    	public static int iSCALE = 2;
    	public static int tSIZE = iSCALE * sSCALE;
    	
    	public static final String sDISPLAYNAME = sNAME + " " + sVER;
    	public static final int sWIDTH = sSIZE * sSCALE;
    	public static final int sHEIGHT = (int) ((double)sWIDTH / 12D * 9D);
    	public static int iWIDTH = sWIDTH * iSCALE;
    	public static int iHEIGHT = sHEIGHT * iSCALE;
    	public static Dimension sDIMENSION = new Dimension(iWIDTH,iHEIGHT);
    	public static Map[] Maps;
    	
    	public static int[] XGRID = new int[(sWIDTH / sSCALE)];
    	public static int[] YGRID = new int[(sHEIGHT / sSCALE)];
    	
    	public static int[] rXGRID = new int[XGRID.length];
    	public static int[] rYGRID = new int[YGRID.length];
    	
    	private BufferedImage iIMAGE = new BufferedImage (iWIDTH, iHEIGHT, BufferedImage.TYPE_INT_ARGB);
    	//private int[] pixels = ((DataBufferInt) iIMAGE.getRaster().getDataBuffer()).getData();
    	private JFrame sFRAME;
    	
    	public Game() {
    		setMinimumSize(new Dimension(iWIDTH,iHEIGHT));
    		setMaximumSize(new Dimension(iWIDTH,iHEIGHT));
    		setPreferredSize(new Dimension(iWIDTH,iHEIGHT));
    		
    		sFRAME = new JFrame(sDISPLAYNAME);
    		sFRAME.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		sFRAME.setLayout(new BorderLayout());
    		sFRAME.add(this, BorderLayout.CENTER);
    		sFRAME.setSize(iWIDTH, iHEIGHT);
    		sFRAME.pack();
    		
    		sFRAME.setResizable(false);
    		sFRAME.setLocationRelativeTo(null);
    		sFRAME.setVisible(true);
    	}
    	
    	public synchronized void start() {
    		iRUNNING = true;
    		for (int i = 0; i < rXGRID.length; i++) { rXGRID[i] = 5 + (i * (iSCALE * sSCALE)); }
    		for (int i = 0; i < rYGRID.length; i++) { rYGRID[i] = 5 + (i * (iSCALE * sSCALE)); }
    		Init();
    		new Thread(this).start();
    	}
    	
    	public int[][][] nPASSARR(Map mapname) {
    		int width = mapname.getWidth() / sSCALE;
    		int height = mapname.getHeight() / sSCALE;
    		int[][][] Array = new int[width][height][5];
    		return Array;
    	}
    	
    	public void Init() {
    		Map mAkhuForest = new Map("Akhu Forest", "res/maps/mAkhuForest", 2);
    		mAkhuForest.setPassage(nPASSARR(mAkhuForest));
    	}
    	
    	public void run() {
    		long cLASTTIME = System.nanoTime();
    		double sNSPERTICK = 1000000000D / 60D;
    		
    		int cFRAMES = 0;
    		int cTICKS = 0;
    		
    		long cLASTTIMER = System.currentTimeMillis();
    		double delta = 0;
    		
    		while (iRUNNING) {
    			long cNOWTIME = System.nanoTime();
    			delta += (cNOWTIME - cLASTTIME) / sNSPERTICK;
    			cLASTTIME = cNOWTIME;
    			boolean shouldRender = true;
    			
    			while (delta >= 1) { cTICKS++; tick(); delta--; shouldRender = true; }
    			
    			try { Thread.sleep(2); } catch (InterruptedException e)
    			{ e.printStackTrace(); }
    			
    			if (shouldRender) { cFRAMES++; }
    			if (System.currentTimeMillis() - cLASTTIMER >= 1000) {
    				cLASTTIMER += 1000;
    				System.out.println(cFRAMES + " speed, " + cTICKS + " ticks");
    				cFRAMES = 0; cTICKS = 0;
    			}
    		}
    	}
    	
    	public void tick() {
    		BufferStrategy sBUFFERSTRATEGY = getBufferStrategy();
    		if (sBUFFERSTRATEGY == null) {
    			createBufferStrategy(3);
    			return;
    		}
    		
    		Graphics sGRAPHICS = sBUFFERSTRATEGY.getDrawGraphics();
    		sGRAPHICS.drawImage(iIMAGE, 0, 0, getWidth(), getHeight(), null);
    		
    		sGRAPHICS.setColor(Color.BLACK);
    		gRECT(sGRAPHICS, 0, 0, getWidth(), getHeight(), true);
    		
    		sGRAPHICS.setColor(Color.BLACK);
    		gRECT(sGRAPHICS, 5, 5, getWidth() - 5, getHeight() - 5, true);
    		
    		//drawImage(sGRAPHICS, "res/ground/grass01.png", sX(1), sY(1), 16, 16);
    		//drawImage(sGRAPHICS, "res/ground/grass01.png", sX(20), sY(1), 16, 16);
    		//drawImage(sGRAPHICS, "res/ground/grass01.png", -10, -10);
    		
    		/*int[][][] abc = new int[(sWIDTH / sSCALE)][(sHEIGHT / sSCALE)][5];
    		Map test = new Map("testmap","res/maps/test", abc, 3);
    		System.out.print(test.getHeight());*/
    		
    		sGRAPHICS.setColor(Color.BLACK);
    		for (int i = 0; i < sBORDERS; i++) {
    		gRECT(sGRAPHICS, 0 + i, 0 + i, getWidth() - (i + 1), getHeight() - (i + 1), false);	}
    		
    		sGRAPHICS.dispose();
    		sBUFFERSTRATEGY.show();
    	}
    	
    	public int sX(int place) { return rXGRID[place - 1]; }
    	public int sY(int place) { return rYGRID[place - 1]; }
    	
    	public static BufferedImage Img(String path) {
    		BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
    		try { image = ImageIO.read(new File(path)); }
    		catch (IOException e) { e.printStackTrace(); }
    		return image;
    	}
    
    	public void drawImage (Graphics g, String path, int x, int y) {
    		BufferedImage img = Img(path);
    		int w = img.getWidth() * iSCALE;
    		int h = img.getHeight() * iSCALE;
    		g.drawImage(img, x, y, w, h, null);
    	}
    	
    	public void gRECT(Graphics g, int x1, int y1, int x2, int y2, boolean fill) {
    		int x[] = {x1,x2,x2,x1,x1};
    		int y[] = {y1,y1,y2,y2,y1};
    		if (fill == false) {
    			g.drawPolygon(x, y, 5);
    		} else {
    			g.fillPolygon(x, y, 5);
    		}
    	}
    	
    	public void gPIX(Graphics g, int x, int y) {
    		g.drawLine(x, y, x, y);
    	}
    	
    	public static void main(String[] args) {
    		new Game().start();
    	}
    
    	public void keyPressed(KeyEvent arg0) {
    		
    	}
    
    	public void keyReleased(KeyEvent arg0) {
    		
    	}
    
    	public void keyTyped(KeyEvent arg0) {
    		
    	}
    }
    Map (Class):
    Java Code:
    package pack.script.game;
    
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.util.Arrays;
    
    public class Map {
    	String mNAME;
    	String mPATH;
    	
    	int[][][] mPASSAGE;
    	int mCHARPRIORITY;
    	int scale = Game.iSCALE;
    	
    	File folder;
    	File[] layers;
    	String[] nLayers;
    	
    	String mEXAMPLELAYER;
    	BufferedImage mIMAGE;
    	
    	public Map(String mNAME, String mPATH, int mCHARPRIORITY) {
    		this.mNAME = mNAME;
    		this.mPATH = mPATH;
    		this.mPASSAGE = mPASSAGE;
    		this.mCHARPRIORITY = mCHARPRIORITY;
    		
    		this.folder = new File(getLayersPath());
    		this.layers = this.folder.listFiles();
    		
    		for (int i = 0; i < layers.length;i++) { nLayers[i] = layers[i].getName(); }
    		Arrays.sort(nLayers);
    		
    		mEXAMPLELAYER = getLayersPath() + "/" + layers[0].getName();
    		mIMAGE = Game.Img(mEXAMPLELAYER);
    	}
    	public String getName() { return mNAME; }
    	public String getPath() { return mPATH; }
    	public String getLayersPath() { return (mPATH + "/layers"); }
    	public int getWidth() { return mIMAGE.getWidth(); }
    	public int getHeight() { return mIMAGE.getHeight(); }
    	public void setPassage(int[][][] array) { this.mPASSAGE = array; }
    
    	public boolean getPassage(int x, int y, String area) {
    		int areaPos = 0;
    		if (area.equals("north")) { areaPos = 1; }
    		else if (area.equals("east")) { areaPos = 2; }
    		else if (area.equals("south")) { areaPos = 3; }
    		else if (area.equals("west")) { areaPos = 4; }
    		else { areaPos = 0; }
    		
    		try {
    			if (mPASSAGE[x - 1][y - 1][areaPos] == 1) {
    				return true;
    			} else {
    				return false;
    			}
    		} catch (ArrayIndexOutOfBoundsException e) {
    			return false;
    		}
    	}
    	
    	public int bConv(boolean boolvalue) {
    		int integer;
    		if (boolvalue == true) {
    			integer = 1;
    		} else {
    			integer = 0;
    		} return integer;
    	}
    	
    	public void fillPass(boolean value) {
    		Arrays.fill(mPASSAGE, bConv(value));
    	}
    	
    	public void fillTile(int x, int y, boolean value) {
    		Arrays.fill(mPASSAGE[x - 1][y - 1], bConv(value));
    	}
    	
    	public int[][][] getPassArray() { return mPASSAGE; }
    	public boolean[] getTilePassArray(int x, int y) {
    		boolean[] array = new boolean[5];
    		Arrays.fill(array, false);
    		for (int i = 0; i < mPASSAGE[x - 1][y - 1].length; i++) {
    			if (mPASSAGE[x - 1][y - 1][i] == 1) {
    				array[i] = true;
    			} else {
    				array[i] = false;
    			}
    		}
    		return array;
    	}
    	
    	public String[] getPreLayers() {
    		String[] preLayers = {};
    		for (int i = 0; i < mCHARPRIORITY; i++) {
    			preLayers[i] = nLayers[i];
    		}
    		return preLayers;
    		
    	}
    	public String[] getPostLayers() {
    		String[] postLayers = {};
    		for (int i = mCHARPRIORITY + 1; i < layers.length; i++) {
    			postLayers[i - mCHARPRIORITY - 1] = nLayers[i];
    		}
    		return postLayers;
    	}
    }
    Error Code:
    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    	at pack.script.game.Map.<init>(Map.java:31)
    	at pack.script.game.Game.Init(Game.java:83)
    	at pack.script.game.Game.start(Game.java:71)
    	at pack.script.game.Game.main(Game.java:181)
    I'd usually look at the first two error lines, because the last few are usually related to the thread that I started, since the error happened in the thread, the game start and initialization and everything gets mentioned aswell.

    Thanks
    Augustas

    My Files:

    Java Code:
    Game
    	src
    		pack
    			script
    				game
    					Game.java
    					Map.java
    	res
    		maps
    			mAkhuForest
    Last edited by augustas656; 04-09-2014 at 01:18 PM.

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,001
    Rep Power
    6

    Default Re: Can't figure out why I'm getting Null Pointer Exception

    Here is a question for you: what does listFiles() return when the folder contains NO files? I'll make it multiple choice.

    a) an empty array
    b) null
    c) your computer blows up
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    augustas656 is online now Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    118
    Rep Power
    0

    Default Re: Can't figure out why I'm getting Null Pointer Exception

    Ermm, there's only the Akhu Forest map folder, inside it there's nothing. Damn, is that the problem? xD

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,001
    Rep Power
    6

    Default Re: Can't figure out why I'm getting Null Pointer Exception

    Its simple reasoning you know.

    The exception points to Map line 31.

    Java Code:
    at pack.script.game.Map.<init>(Map.java:31)
    Map line 31 is this:

    Java Code:
    for (int i = 0; i < layers.length;i++) { nLayers[i] = layers[i].getName(); }
    So an NPE happens here. Then you have to wonder: what CAN be null in this line? At that point its simply going through the possibilities until you find the one that is null (System.out.println() is your friend). And then you have to answer the question: WHY is it null? That requires looking at where you last assign a value to the offending variable.

    You see, its a process. One you'll go through many times in your life. I hope soon you can repeat this process yourself.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    augustas656 is online now Senior Member
    Join Date
    Apr 2014
    Location
    United Kingdom
    Posts
    118
    Rep Power
    0

    Default Re: Can't figure out why I'm getting Null Pointer Exception

    Yeah I use system out print, this time I was testing slowly, creating my very first map, and then eventually I got this error, and just by removing the error lined with // the entire class just collapsed. xD, that's where I got stuck xD

Similar Threads

  1. Null Pointer Exception
    By Army in forum New To Java
    Replies: 19
    Last Post: 01-19-2013, 04:14 AM
  2. Null pointer exception
    By izzy in forum New To Java
    Replies: 5
    Last Post: 03-22-2010, 05:19 PM
  3. Null Pointer exception
    By diegoyj in forum New To Java
    Replies: 7
    Last Post: 01-29-2010, 04:17 PM
  4. Null pointer exception
    By Stephenmak in forum New To Java
    Replies: 5
    Last Post: 04-01-2009, 02:17 PM
  5. [SOLVED] Cant figure out null pointer exception
    By todd2230 in forum New To Java
    Replies: 6
    Last Post: 05-06-2008, 07:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •