Working on class work for project but I ran in to this problem, pleas help me! My code wont run!

So I haven't been using processing in a while now, but I need to use it for a project and I was trying to use processing with the porclipsing plugin but I get errors when running my sketch!

This is my Main:

package VectorsClass;

import processing.core.*;
public class VectorMain extends PApplet {

    public static void main(String[] args) {
        PApplet.main(VectorMain.class.getName());

    }
    public Ball ball = new Ball();
    public void settings(){
        size(200,200);
        smooth();
    }

    public void setup(){

    }
    public void draw(){
        background(0,0,0);
        ball.move();
        ball.bounce();
        ball.draw();
    }
}

This is a class I made

package VectorsClass;

import processing.core.*;

public class Ball extends PApplet {
    private PVector location;
    private PVector velocity;

    public Ball() {
        location = new PVector(width/2,height/2);
        velocity = new PVector(2.5f,-2);
    }

    public void move(){
        location.add(velocity);
    }

    public void bounce(){
        if((location.x > width) || (location.x < 0)) {velocity.x = velocity.x * -1;}
        if((location.y > height) || (location.y < 0)) {velocity.y = velocity.y * -1;}
    }

    public void draw(){
        stroke(127);
        strokeWeight(2);
        fill(127);
        ellipse(location.x, location.y, 48, 48);
    }
}

These are the errors: Exception in thread "Animation Thread" java.lang.NullPointerException at processing.core.PApplet.stroke(PApplet.java:13758) at VectorsClass.Ball.draw(Ball.java:24) at VectorsClass.VectorMain.draw(VectorMain.java:23) at processing.core.PApplet.handleDraw(PApplet.java:2402) at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1527) at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)

Thank you for any help! I made this little sketch to see if I set everything up properly but it dose not seem to work.

Answers

  • edited July 2016 Answer ✓

    I fixed it! thank you anyways!

    package VectorsClass;
    
    import processing.core.*;
    public class VectorMain extends PApplet {
    
        public static void main(String[] args) {
            //PApplet.main(VectorMain.class.getName());
            PApplet.main(new String[] {VectorMain.class.getName()});
    
        }
        Ball ball = new Ball(this);
        public void settings(){
            size(300,300);
            smooth();
        }
        /// Set up
        public void setup(){
    
        }
        public void draw(){
            background(0,0,0);
            ball.move();
            ball.bounce();
            ball.draw();
        }
    }
    

    ====

    package VectorsClass;
    
    import processing.core.*;
    
    public class Ball{
        VectorMain p;
        private PVector location;
        private PVector velocity;
    
        public Ball(VectorMain p) {
            this.p = p;
            location = new PVector(p.width/2,p.height/2);
            velocity = new PVector(2.5f,-2);
        }
    
        public void move(){
            location.add(velocity);
        }
    
        public void bounce(){
            if((location.x > p.width) || (location.x < 0)) {velocity.x = velocity.x * -1;}
            if((location.y > p.height) || (location.y < 0)) {velocity.y = velocity.y * -1;}
        }
    
        public void draw(){
            p.stroke(127);
            p.strokeWeight(2);
            p.fill(127);
            p.ellipse(location.x, location.y, 48, 48);
        }
    }
    
Sign In or Register to comment.