0

I know I am doing something wrong but I can't seem to fix it.

class Draw {

    int x,y;
    int xp,yp;
    PImage image;

    Draw(int dragx, int dragy, int movex, int movey, PImage a) {
        x = dragx;
        y = dragy;
        xp = movex;
        yp = movey;
        image = a;
    }

    void display() {
        smooth();
        background(255);
        fill(255);
        rect (0,180,40,40);
        fill(0);
        rect (0,240,40,40);
        image = get();
        stroke(0);
        fill(255);
    }

    void drawing() {
        background(image);
        float sizex = xp - x;
        float sizey = yp - y; 
        if (mousePressed && mouseButton == LEFT) {
            rect(x, y, sizex, sizey);
        }
    }

    void press() {
        x = mouseX;
        y = mouseY;
    }

    void release() {
        xp = mouseX;
        yp = mouseY;
        noLoop();
        image = get();
        loop();
    }

    void drag() {
        xp = mouseX;
        yp = mouseY;
    }
}

Draw rect;

void setup() {
    size (900,600);
    //rect.display();
}

void draw() {
    background(255);
    //rect.drawing();
}
void mousePressed() {
    //rect.press();
}
void mouseReleased() {
    //rect.release();
}
void mouseDragged() {
    //rect.drag();
}

The areas in comments or "//" are the errors that I get, giving me 'NullPointerException' error. I want to somehow understand how I can put 'void display()' under 'void setup()' under Draw rect; without any errors.

2
  • 3
    You need to create a Draw object for rect to refer to. Also, I don't think you've shown all of your code here - there seem to be gaps. Commented Apr 20, 2014 at 6:31
  • 1
    Also remember that in OOP classes names should't be verbs like Draw. Commented Apr 21, 2014 at 21:50

1 Answer 1

2

You have not initialized rect anywhere. You should initialize it:

void setup() {
    rect = new Draw(0, 0, 0, 0, new PImage());
    size (900,600);
    //rect.display();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that helped very much. But now it's taken me to a new error... Where "background(image)" under "void drawing()" is saying "background image must be same size as your application". I'm just going to try and figure that out now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.