Loading...
Logo
Processing Forum
differenze9's Profile
2 Posts
3 Responses
0 Followers

Activity Trend

Last 30 days
Show:
Private Message
    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".

    So far i have only written the code supposed to draw the map (tile based).

    My Main class:

    1. //Using Eclipse
    2. //Main class, clean version

    3. import processing.core.*;
    4. import java.awt.event.*;

    5. public class Game2 extends PApplet implements KeyListener{
    6.       //Instancing classes
    7.       Player you = new Player();
    8.       maps map = new maps();
    9.       voids allVoids = new voids();    //the class causing errors
    10.       int lvl=1;                         //just for the development
    11. public static void main(String[] args) {
    12.       PApplet.main(new String[] { "Game2" });
    13. }

    14. public void setup(){
    15.       size(1024,640);
    16. }

    17. public void draw(){
    18.       background(255);
    19.       allVoids.drawBox(); //calling the void from class "voids", 
    20.       //supposed to be changed to a void drawing the map(background)
    21. }
    22. // Keypress -- works as far as i know, simply edits posX&posY from the player class 
    23. // to move the caracter. Outblocked to cause no errors
    24. /*
    25. public void keyPressed(KeyEvent e){
    26. int keyCode = e.getKeyCode();
    27. if (keyCode == KeyEvent.VK_RIGHT){
    28. if(map.main[lvl][you.posY+1][you.posX+2]>999 || map.main[lvl][you.posY+1][you.posX+2]<900) you.posX++;
    29. }
    30. if (keyCode == KeyEvent.VK_LEFT){
    31. if(map.main[lvl][you.posY+1][you.posX]>999 || map.main[lvl][you.posY+1][you.posX]<900) you.posX--;
    32. }
    33. if (keyCode == KeyEvent.VK_UP){
    34. if(map.main[lvl][you.posY][you.posX+1]>999 || map.main[lvl][you.posY][you.posX+1]<900)you.posY--;
    35. }
    36. if (keyCode == KeyEvent.VK_DOWN){
    37. if(map.main[lvl][you.posY+2][you.posX+1]>999 || map.main[lvl][you.posY+2][you.posX+1]<900)you.posY++;
    38. }
    39. else{
    40. e.consume();
    41. }
    42. }*/
    43. }

    The maps class:

    1. public class maps {
    2. // simply contains an int array where every position represents a field,
    3. // local variables are there to make different numbers represented equally long (square-ish shape)
    4. // simply: int main[lvl][x][y]
    5. int bdr=1;  //outer border, hit when person leaves the field, to save space
    6. //
    7. // later on some of the bdr tiles are going to be changed to numbervalues like 030404
    8. // 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
    9. //
    10. int emp=901;  //empty field
    11. int wll=20; //wall for test
    12. public int main[][][]={
    13. //map 1
    14. // in Eclipse the letters have the same width, therefor are all tiles represented by 
    15. //three letters to make the viewing easier for me
    16. {
    17. {bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr},
    18. {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
    19. {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
    20. {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
    21. {bdr,emp,emp,emp,emp,wll,wll,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
    22. {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
    23. {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
    24. {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
    25. {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
    26. {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
    27. {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
    28. {bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr}
    29. },
    30. //map 2
    31. {
    32. {bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr},
    33. {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
    34. {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
    35. {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
    36. {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
    37. {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
    38. {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
    39. {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
    40. {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
    41. {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
    42. {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
    43. {bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr}
    44. },
    45. //map 3
    46. {
    47. {bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr},
    48. {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
    49. {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
    50. {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
    51. {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
    52. {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
    53. {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
    54. {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
    55. {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
    56. {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
    57. {bdr,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,emp,bdr},
    58. {bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr,bdr}
    59. }
    60. };
    61. public maps(){}  //constructor
    62. }

    And here the voids class causing problems:

    1. import processing.core.*; 
    2. // class causing errors (i guess)

    3. class voids extends PApplet{ 
    4. //purpose, contain voids executed in the draw void to 
    5. //clean up the main class
    6. public void drawBox(){   // void for testing
    7. rect(5,5,50,50);
    8. }
    9. void drawMap(int map[][][], int lvl){ 
    10. // Most necessary void: Two for-loops to get a number from the
    11. // maps.main array and convert it to a picture (represented by
    12. // a rectangle for testing purposes).
    13. int m[][][]=map;
    14. for(int a=0;a<=15;a++){
    15. for (int b=0;b<=9;b++){
    16. if (m[lvl][b][a]==901){
    17. //draw empty
    18. rect((a-1)*64,(b-1)*64,64,64);
    19. }
    20. if (m[lvl][b][a]==20){
    21. //draw a wall
    22. }
    23. }
    24. }
    25. }
    26. }

    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:


    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)

    Eclipse gives no more information than this so there are no spelling errors etc.
    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 :)

    Hello.

    I am working on a simple game project and have a problem with multiple classes (static/nonstatic voids).

    I want my main class to be very clean, which means only setup(), draw() and keyevent. Therefor I have a second class named voids having different voids containing rect() and image() functions. For example one for drawing a map.

    So inside the main class i get:

    public void setup(){
          size(1024,640);
          //etc
    }
    public void draw(){
          voids.drawMap(map.main, lvl);
          voids.drawPlayer(posX,posY);
    }

    and inside the voids class:

    public void drawMap(int map[][][], int lvl){
          //code drawing the map, containing:
          rect(a,b,c,d);
    }

    this way the main class complains " Cannot make a static reference to the non-static method drawMap(int[][][], int) from the type voids" and suggests to make drawMap() static.
    But if i make drawMap() static the rect() function complains " Cannot make a static reference to the non-static method rect(float, float, float, float) from the type PApplet"

    Do you have any suggestions on how to fix this, I know it would work to move the drawMap void to the main class, but i want it clean.

    Thanks for the help // Differenze :)