Error in netbeans java.lang.NullPointerException

Hi, I'm trying to run my code in netbeans, but it does not work:

package uk.co.mooduino.processingtest;

import processing.core.PApplet;

public class Button {

    PApplet Button;
    float COR_X, COR_Y, H, W;
    boolean state, attivo, indisponibile;
    String str;
    int CIA;
    int LBLU;
    int AZZ;

    Button(int x, int y, int h, int w, String str, PApplet p) {
        this.AZZ = Button.color (202, 223, 247);
        this.LBLU = Button.color (25, 71, 129);
        this.CIA = Button.color (218, 228, 240);
        Button = p;
        COR_X = x;
        COR_Y = y;
        H = h;
        W = w;
        state = false;
        this.str = str;
        indisponibile = true;
    }

    void change_disponibilita() {
        if (indisponibile) {
            indisponibile = false;
        } else {
            indisponibile = true;
        }
    }

    void click() {
        if (indisponibile == false && Button.mouseX > COR_X && Button.mouseX < COR_X + H && Button.mouseY > COR_Y && Button.mouseY < COR_Y + W && Button.mouseButton == LEFT) {
            state = true;
            attivo = true;
            Button.mouseButton = 0;
        }
    }

    void disegna() {
        click();
        Button.fill(state ? CIA : LBLU);
        if (indisponibile == true) {
            Button.fill(23, 126, 250);
        }
        Button.stroke(118, 163, 216);
        Button.rect(COR_X, COR_Y, H, W, 20);
        Button.fill(attivo ? AZZ : 0);
        Button.text(str, COR_X, COR_Y);
    }
}




package uk.co.mooduino.processingtest;

import java.io.File;
import processing.core.*;

public class MyProcessingSketch extends PApplet {

Button sectionA[]=new Button [6];
Button sectionB[]=new Button [7];
Button sectionC[]=new Button [3];
int ImgX;
int ImgY;
PImage img;
PImage tmp;
PImage R;
PImage G;
PImage B;
PImage YUV;
PImage y;
PImage u;
PImage v;
char X ;
int Y;
boolean verifica;


public void settings() {
  size(800, 600);
}


public void setup(){

  //LAYOUT
  fill(128);
  rect(0,0,100,600,7);
  rect(700,0,100,600,7);
  rect(100,500,600,100,7);
  fill(0);
  rect(100,0,600,500,7);




  sectionA[0]= new Button(30,70,40,35,"RGB",this);
  sectionA[1]= new Button(30,2*70,40,35,"R",this);
  sectionA[2]= new Button(30,3*70,40,35,"G",this);
  sectionA[3]= new Button(30,4*70,40,35,"B",this);
  sectionA[4]= new Button(30,5*70,40,35,"SALE",this);
  sectionA[5]= new Button(30,6*70,40,35,"PEPE",this);


  sectionB[0]= new Button(730,70,40,35,"3",this);
  sectionB[1]= new Button(730,2*70,40,35,"5",this);
  sectionB[2]= new Button(730,3*70,40,35,"7",this);
  sectionB[3]= new Button(730,4*70,40,35,"YUV",this);
  sectionB[4]= new Button(730,5*70,40,35,"Y",this);
  sectionB[5]= new Button(730,6*70,40,35,"U",this);
  sectionB[6]= new Button(730,7*70,40,35,"V",this);


  sectionC[0]= new Button(150,550,40,40,"APRI",this);
  sectionC[1]= new Button(2*150,550,80,40,"RESET",this);
  sectionC[2]= new Button(3*150,550,100,40,"SALVA",this);


  sectionC[0].change_disponibilita();
}



public void draw(){   


    for (Button sectionA1 : sectionA) {
        sectionA1.disegna();
    }


    for (Button sectionB1 : sectionB) {
        sectionB1.disegna();
    }


    for (Button sectionC1 : sectionC) {
        sectionC1.disegna();
    }
}

Output: run: java.lang.NullPointerException at uk.co.mooduino.processingtest.Button.(Button.java:16) at uk.co.mooduino.processingtest.MyProcessingSketch.setup(MyProcessingSketch.java:48) at processing.core.PApplet.handleDraw(PApplet.java:2387) at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1540) at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)

Tagged:

Answers

  • public class Button {
      PApplet Button;
    

    Having a member the exact same name as your class is asking for trouble. There is a style guide for Java that is very useful and that says all members should start with a lowercase letter, classes with an uppercase one.

  • But your problem is that you are referencing Button in lines 16, 17, 18 before you set it on line 19. Move line 19 before 16.

Sign In or Register to comment.