Java.lang.NullPointerException how can I fix it?

edited December 2015 in Library Questions

I'm new to processing This is my code below pls tell me what's wrong with my code

            import shiffman.box2d.*;
            import org.jbox2d.collision.shapes.*;
            import org.jbox2d.common.*;
            import org.jbox2d.dynamics.*;

            short count, dim = 150;
            int num = 0;

            PFont counter;

            final static short POS = 25, FPS = 50, BOLD = 0;
            final static color  BG = #C04000, 
            FG = #82CFD0, BORDER = 0;



            PImage photo;


            // A reference to our box2d world
            Box2DProcessing box2d;

            // A list we'll use to track fixed objects
            ArrayList<Boundary> boundaries;
            // A list for all of our rectangles
            ArrayList<CustomShape> polygons;

            ArrayList<Ball> balls;

            int missed =0;

            void setup() {
              size(1000,600);
              smooth();
              counter = createFont("Arial", 72, true);

              photo = loadImage("2-01.png");

              // Initialize box2d physics and create the world
              box2d = new Box2DProcessing(this);
              box2d.createWorld();
              // We are setting a custom gravity
              box2d.setGravity(0, -10);

              // Create ArrayLists  
              balls = new ArrayList<Ball>();
              boundaries = new ArrayList<Boundary>();
              polygons = new ArrayList<CustomShape>();

              // Add a bunch of fixed boundaries
              // b1
              Boundary b1 = new Boundary(200f, 200f, 150f, 10f, radians(45));
              boundaries.add(b1);

              // b2
              Boundary b2 = new Boundary(500f, 200f, 150f, 10f, radians(-45));
              boundaries.add(b2);

              Boundary b3 = new Boundary(100f, 200f, 150f, 10f, radians(0));
              boundaries.add(b3);


              //right side
              Boundary rightSide = new Boundary(width-5,height/2,10,height,0);
              rightSide.setRotation(-PI/8);
              boundaries.add(rightSide);
              // left sides
              Boundary leftSide = new Boundary(5,height/2,10,height,0);
              leftSide.setRotation(PI/8);
              boundaries.add(leftSide);
            }

             {
              size(208, 184);
              photo = loadImage("2-01.png");
            }


            void draw() {
              background(255);
              // We must always step through time!
              box2d.step();

              image(photo,mouseX,0);
              // Display all the boundaries
              for (Boundary wall: boundaries) {
                //wall.setRotation(PI/6);
                wall.display();
              }

              // Display all the people
              {
                //for (Ball b: balls)
               for (int i=0; i<balls.size(); i++) 
               {
                 //important
                Ball b = balls.get(i); 
                b.display();
                Vec2 pos   = box2d.getBodyPixelCoord(b.body);
                if(b.done())
                { 
                  missed++;
                  balls.remove(i);
                  println("ball missed:"+missed);
                  createBall(); 
                }
               }
              }



              {
                //for (Ball b: balls)
               for (int i=0; i<balls.size(); i++) 
               {
                 //important
                Ball b = balls.get(i); 
                b.display();

                 image(photo,mouseX,0);

                if(b.done())
                { 
                  missed++;
                  balls.remove(i);
                  println("ball missed:"+missed);
                  createBall(); 
                }
               }
              }
Tagged:

Answers

  • Answer ✓

    which line is the null pointer exception on, or are we meant to guess?

    we can't run your code because we don't have the images.

    or the font.

    or any of the libraries.

    did you read the FAQ about NPEs? https://forum.processing.org/two/discussion/8071/why-do-i-get-a-nullpointerexception

  • edited December 2015

    I'm sry to make u guys confuse haha firs time to use this forum.... now I have add image into here, I'm trying to add the score counter when the image catch the ball will increase the score but not dropping off the window only increase the score...

    import shiffman.box2d.*;
    import org.jbox2d.collision.shapes.*;
    import org.jbox2d.common.*;
    import org.jbox2d.dynamics.*;
    
    short count, dim = 150;
    int num = 0;
    
    PFont counter;
    
    final static short POS = 25, FPS = 50, BOLD = 2;
    final static color // BG = #C04000, 
    FG = #10BA93, BORDER = -1;
    
    PImage photo;
    
    
    
    // A reference to our box2d world
    Box2DProcessing box2d;
    
    // A list we'll use to track fixed objects
    ArrayList<Boundary> boundaries;
    // A list for all of our rectangles
    ArrayList<CustomShape> polygons;
    
    ArrayList<Ball> balls;
    
    
    int missed =0;
    
    void setup() {
      size(1000,600);
      smooth();
      counter = createFont("Arial", 50, true);
    
      photo = loadImage("2-01.png");
    
      // Initialize box2d physics and create the world
      box2d = new Box2DProcessing(this);
      box2d.createWorld();
      // We are setting a custom gravity
      box2d.setGravity(0, -10);
    
      // Create ArrayLists  
      balls = new ArrayList<Ball>();
      boundaries = new ArrayList<Boundary>();
      polygons = new ArrayList<CustomShape>();
    
      // Add a bunch of fixed boundaries
      // b1
      Boundary b1 = new Boundary(100f, 100f, 150f, 10f, radians(35));
      boundaries.add(b1);
    
      // b2
      Boundary b2 = new Boundary(300f, 100f, 150f, 10f, radians(-35));
      boundaries.add(b2);
    
      //right side
      Boundary rightSide = new Boundary(width-5,height/2,10,height,0);
      rightSide.setRotation(-PI/11);
      boundaries.add(rightSide);
      // left sides
      Boundary leftSide = new Boundary(5,height/2,10,height,0);
      leftSide.setRotation(PI/11);
      boundaries.add(leftSide);
    }
    
    void draw() {
      background(255);
      // We must always step through time!
      box2d.step();
    
      // Display all the boundaries
      for (Boundary wall: boundaries) {
        //wall.setRotation(PI/6);
        wall.display();
      }
    
      // Display all the people
      {
    
        //for (Ball b: balls)
       for (int i=0; i<balls.size(); i++) 
       {
         //important
        Ball b = balls.get(i); 
        b.display();
        Vec2 pos   = box2d.getBodyPixelCoord(b.body);
        image(photo,mouseX,500);
        if(b.done())
        { 
          missed++;
          balls.remove(i);
          println("ball missed:"+missed);
          createBall(); 
        }
       }
      }
    
    
      for (CustomShape cs: polygons) {
        cs.display();
      }
    
    
    fill(FG);
      ellipse(80, 80, 80, 80);
      textFont(counter, 45);         // Specify font to be used
      fill(#5D5D5D);                // Specify font color 
      text(nf(missed, 1), 60, 95);            // Display Text and Location
    
    }
    
    
    
    
    void createBall()
    {
      Ball b = new Ball(random(0, width), 0);
      balls.add(b);
    }
    
    void mousePressed() {
    
      createBall();
    
    }
    

    2-01

  • Which line is the npe on?

  • edited December 2015

    ( box2d.step();)

          // Display all the boundaries
          for (Boundary wall: boundaries) {
            //wall.setRotation(PI/6);
            wall.display();
          }
    
          // Display all the people
          {
    
            //for (Ball b: balls)
           for (int i=0; i<balls.size(); i++) 
           {
             //important
            Ball b = balls.get(i); 
            b.display();
            Vec2 pos   = box2d.getBodyPixelCoord(b.body);
            image(photo,mouseX,500);
            if(b.done())
            { 
              missed++;
              balls.remove(i);
              println("ball missed:"+missed);
              createBall(); 
            }
           }
          }
    
    
          for (CustomShape cs: polygons) {
            cs.display();
          }
    
Sign In or Register to comment.