Null pointer exception multi-classes
in
Programming Questions
•
11 months ago
Hello, i've got a problem with my game.
I get a Null Pointer Exception.
I want the code to be clean and easy to read so the main class shall only contain the setup and draw void and a key handler. The voids used by the main class are supposed to be in another class called " voids".
I get a Null Pointer Exception.
I want the code to be clean and easy to read so the main class shall only contain the setup and draw void and a key handler. The voids used by the main class are supposed to be in another class called " voids".
So far i have only written the code supposed to draw the map (tile based).
My Main class:
My Main class:
- //Using Eclipse
- //Main class, clean version
- import processing.core.*;
- import java.awt.event.*;
- public class Game2 extends PApplet implements KeyListener{
- //Instancing classes
- Player you = new Player();
- maps map = new maps();
- voids allVoids = new voids(); //the class causing errors
- int lvl=1; //just for the development
- public static void main(String[] args) {
- PApplet.main(new String[] { "Game2" });
- }
- public void setup(){
- size(1024,640);
- }
- public void draw(){
- background(255);
- allVoids.drawBox(); //calling the void from class "voids",
- //supposed to be changed to a void drawing the map(background)
- }
- // Keypress -- works as far as i know, simply edits posX&posY from the player class
- // to move the caracter. Outblocked to cause no errors
- /*
- public void keyPressed(KeyEvent e){
- int keyCode = e.getKeyCode();
- if (keyCode == KeyEvent.VK_RIGHT){
- if(map.main[lvl][you.posY+1][you.posX+2]>999 || map.main[lvl][you.posY+1][you.posX+2]<900) you.posX++;
- }
- if (keyCode == KeyEvent.VK_LEFT){
- if(map.main[lvl][you.posY+1][you.posX]>999 || map.main[lvl][you.posY+1][you.posX]<900) you.posX--;
- }
- if (keyCode == KeyEvent.VK_UP){
- if(map.main[lvl][you.posY][you.posX+1]>999 || map.main[lvl][you.posY][you.posX+1]<900)you.posY--;
- }
- if (keyCode == KeyEvent.VK_DOWN){
- if(map.main[lvl][you.posY+2][you.posX+1]>999 || map.main[lvl][you.posY+2][you.posX+1]<900)you.posY++;
- }
- else{
- e.consume();
- }
- }*/
- }
The maps class:
- public class maps {
- // simply contains an int array where every position represents a field,
- // local variables are there to make different numbers represented equally long (square-ish shape)
- // simply: int main[lvl][x][y]
- int bdr=1; //outer border, hit when person leaves the field, to save space
- //
- // later on some of the bdr tiles are going to be changed to numbervalues like 030404
- // so if the player walks on this tile the map is going to change to "lvl" 03 and the player will stand on
// tile 04,04 - //
- int emp=901; //empty field
- int wll=20; //wall for test
- public int main[][][]={
- //map 1
- // in Eclipse the letters have the same width, therefor are all tiles represented by
- //three letters to make the viewing easier for me
- {
- {bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr},
- {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
- {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
- {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
- {bdr,emp,emp,emp,emp,wll,wll,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
- {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
- {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
- {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
- {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
- {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
- {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
- {bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr}
- },
- //map 2
- {
- {bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr},
- {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
- {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
- {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
- {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
- {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
- {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
- {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
- {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
- {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
- {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
- {bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr}
- },
- //map 3
- {
- {bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr},
- {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
- {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
- {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
- {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
- {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
- {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
- {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
- {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
- {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
- {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
- {bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr}
- }
- };
- public maps(){} //constructor
- }
And here the voids class causing problems:
- import processing.core.*;
- // class causing errors (i guess)
- class voids extends PApplet{
- //purpose, contain voids executed in the draw void to
- //clean up the main class
- public void drawBox(){ // void for testing
- rect(5,5,50,50);
- }
- void drawMap(int map[][][], int lvl){
- // Most necessary void: Two for-loops to get a number from the
- // maps.main array and convert it to a picture (represented by
- // a rectangle for testing purposes).
- int m[][][]=map;
- for(int a=0;a<=15;a++){
- for (int b=0;b<=9;b++){
- if (m[lvl][b][a]==901){
- //draw empty
- rect((a-1)*64,(b-1)*64,64,64);
- }
- if (m[lvl][b][a]==20){
- //draw a wall
- }
- }
- }
- }
- }
So the draw void is supposed to execute voids like allVoids.drawMap(), allVoids.drawPlayer(), allVoids.drawInventory().
But the rect() void in "voids" causes an Null Pointer Exception:
I could move the voids into the main class witch makes the program run, but then the main class gets so full and i want it clean ^^
Help and Comments appreciated, sorry for gramatical errors
//Differenze :)
But the rect() void in "voids" causes an Null Pointer Exception:
Eclipse gives no more information than this so there are no spelling errors etc.Exception in thread "Animation Thread" java.lang.NullPointerException
at processing.core.PApplet.rect(PApplet.java:11045)
at voids.drawBox(voids.java:5)
at Game2.draw(Game2.java:19)
at processing.core.PApplet.handleDraw(PApplet.java:2128)
at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:190)
at processing.core.PApplet.run(PApplet.java:2006)
at java.lang.Thread.run(Thread.java:680)
I could move the voids into the main class witch makes the program run, but then the main class gets so full and i want it clean ^^
Help and Comments appreciated, sorry for gramatical errors
//Differenze :)
1