Results 1 to 5 of 5
- 04-09-2014, 02:14 PM #1
Senior Member
- Join Date
- Apr 2014
- Location
- United Kingdom
- Posts
- 193
- Rep Power
- 7
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) { } }
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; } }
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)
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 02:18 PM.
- 04-09-2014, 02:16 PM #2
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
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
- 04-09-2014, 02:20 PM #3
Senior Member
- Join Date
- Apr 2014
- Location
- United Kingdom
- Posts
- 193
- Rep Power
- 7
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
- 04-09-2014, 02:29 PM #4
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
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)
Java Code:for (int i = 0; i < layers.length;i++) { nLayers[i] = layers[i].getName(); }
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
- 04-09-2014, 02:33 PM #5
Senior Member
- Join Date
- Apr 2014
- Location
- United Kingdom
- Posts
- 193
- Rep Power
- 7
Similar Threads
-
Null Pointer Exception
By Army in forum New To JavaReplies: 19Last Post: 01-19-2013, 05:14 AM -
Null pointer exception
By izzy in forum New To JavaReplies: 5Last Post: 03-22-2010, 06:19 PM -
Null Pointer exception
By diegoyj in forum New To JavaReplies: 7Last Post: 01-29-2010, 05:17 PM -
Null pointer exception
By Stephenmak in forum New To JavaReplies: 5Last Post: 04-01-2009, 03:17 PM -
[SOLVED] Cant figure out null pointer exception
By todd2230 in forum New To JavaReplies: 6Last Post: 05-06-2008, 08:45 AM
Bookmarks