Loading...
Logo
Processing Forum

NPE (New to Processing)

in Programming Questions  •  11 months ago  
Hello, I'm trying to write a template program for buttons and menus, but I run into a NullPointerException when I try and construct the button class, in line 17.

I am new to Processing, and I have never used Classes before.

Here is my code:
Copy code
  1. int menu = 1;
  2. Button[] mainmenu;
  3. //Button[] optionsmenu;
  4. //Button[] levels;
  5. //Button[] game;

  6. void drawMenu(Button[] m){
  7.   for (int i = 0; i < m.length; i++){
  8.     m[i].draw();
  9.   }
  10. }

  11. void setup(){
  12.   size(800, 640);
  13.   //pos x, pos y, size x, size y, to menu id, from menu id, image name 
  14.   mainmenu[0] = new Button(width/2, height/3, 160, 40, 3, 1, "PlayButton.jpg");
  15.   mainmenu[1] = new Button(width/2, height/2, 160, 40, 2, 1, "OptionsButton.jpg");
  16.   mainmenu[2] = new Button(width/2, 2*height/3, 160, 40, 999, 1, "ExitButton.jpg");
  17. }

  18. void draw(){
  19.   background(0);
  20.   if (menu == 1){
  21.   drawMenu(mainmenu); 
  22.   }
  23. }

  24. class Button {
  25.   int px;
  26.   int py;
  27.   int sx;
  28.   int sy;
  29.   int ToMenu;
  30.   int InMenu;
  31.   String sprite;
  32.   PImage img;
  33.   //Constructor!
  34.   Button(int tpx, int tpy, int tsx, int tsy, int tToMenu, int tInMenu, String sprite){
  35.     img = loadImage(sprite);
  36.     px = tpx;
  37.     py = tpy;
  38.     sx = tsx;
  39.     sy = tsy;
  40.     ToMenu = tToMenu;
  41.     InMenu = tInMenu;
  42.   }
  43.   void draw(){
  44.     image(img, px-(sx/2), py-(sy/2));
  45.   }
  46.   void mouseover(){
  47.     if (mouseX > px-(sx/2) && mouseX < px+(sx/2)){
  48.       if (mouseY > py-(sy/2) && mouseY < py+(sy/2)){
  49.         menu = ToMenu;
  50.       } 
  51.     } 
  52.   }
  53.   //End Class
  54. }

  55. void mousePressed(){
  56.   if (menu==1){
  57.     for (int i = 0; i < 3; i++){
  58.       mainmenu[i].mouseover();
  59.     }
  60.   }
  61. }

Here is the error stack:

Copy code
  1. Exception in thread "Animation Thread" java.lang.NullPointerException
  2. at Buttons.setup(Buttons.java:35)
  3. at processing.core.PApplet.handleDraw(PApplet.java:2103)
  4. at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:190)
  5. at processing.core.PApplet.run(PApplet.java:2006)
  6. at java.lang.Thread.run(Thread.java:662)
Thank you!

Replies(4)

Hey Kelson!

In Java, when you create an array, you also have to make a pre-initialization to create room for the elements to be stored in it, before you can even start putting anything there.

That's why the following fails outright -> mainmenu[0] = new Button(width/2, height/3, 160, 40, 3, 1, "PlayButton.jpg");

You haven't created room for them yet!

Try out these following lines at the beginning of your code:

private static final byte buttonNum = 3;
private static final Button[] mainmenu = new Button[buttonNum];

The lines above make space to 3 elements of type Button to mainmenu.

If you need more space, increase the value of buttonNum

Hope it helps. Keep going! 
That works, thanks!

What do the 'private', 'static', 'final' and 'byte' each do..? How does that initialize the array?


Oops! Sorry. It's unrelated! You can remove them!
I always use them outta habit! 
This is a short explanation what they are for:
  • final     -> hints to compiler that the value stored won't change anymore.
  • static    -> hints this is a unique global copy, it's not subjected on being instantiated.
  • private -> hints its contents can't be accessed from outside the "sketch".
  • byte     -> it's a type which uses 1 byte of memory. By its turn, int uses 4 bytes of memory!
Ah, ok! :)