0
    import ddf.minim.*;
AudioPlayer player;
Minim minim;

Table t1= new Table(300, 300);
float power=0;
float dx=0;
float dy=0;

void setup()
{
  size(1000, 600);
  frameRate(20);
  minim= new Minim(this);
  player=minim.loadFile("ballsound.mp3");
}

void draw()
{
  strokeWeight(1);
  stroke(0, 0, 0); 
  strokeWeight(10);
  stroke(255, 0, 0);
  fill(26, 218, 35);
  rect(0, 0, 1000, 600);
  noStroke();
  fill(0);
  ellipse(0, 0, 80, 80);
  ellipse(1000, 0, 80, 80);
  ellipse(0, 600, 80, 80);
  ellipse(1000, 600, 80, 80);
  strokeWeight(1);
  stroke(0, 0, 0);
  fill(255);
  ellipse(t1.cue_ball.center.x, t1.cue_ball.center.y, 20, 20);

  t1.cue_ball.center.x+=dx;
  t1.cue_ball.center.y+=dy;

  dx=friction(dx);
  dy=friction(dy);


  stroke(40); 
  strokeWeight(4);
  line ( mouseX , mouseY , mouseX + cos (atan2 ( mouseY -t1.cue_ball.center.y , mouseX - t1.cue_ball.center.x) )*300 , mouseY +  sin( atan2 ( mouseY - t1.cue_ball.center.y , mouseX -t1.cue_ball.center.x ))*300);

  if (mousePressed)
  {
    power+=4;

  }
   if (t1.cue_ball.center.x+20 > 1000 || t1.cue_ball.center.x-20 < 0 )
  {

    dx*=-1;
    player.rewind();
    player.play();
  }
  if (t1.cue_ball.center.y+20 > 600 || t1.cue_ball.center.y-20 < 0)
  {
    dy*=-1;
    player.rewind();
    player.play();
  }

  if( (t1.cue_ball.center.x < 40  && (t1.cue_ball.center.y < 40 ||t1.cue_ball.center.y > 560 ) )  ||  ( t1.cue_ball.center.x > 980 && ( t1.cue_ball.center.y > 580 || t1.cue_ball.center.y < 40)))
  {
    dx=0;
    dy=0;
    fill(255);
    textSize(25);
    text("GAME OVER" ,500,300);
    player.pause();
  }

 if (t1.cue_ball.center.x +20 == 1000)
 {
   t1.cue_ball.center.x=979;
 }
 if (t1.cue_ball.center.x -20 == 0)
 {

   t1.cue_ball.center.x =21;
  }
 if(t1.cue_ball.center.y -20 == 0)
 {
   t1.cue_ball.center.y  =21;
 }




  }



void mouseReleased()
{
  dx=t1.cue_ball.center.x-mouseX;
  dy=t1.cue_ball.center.y-mouseY;
  float n= sqrt(pow(dx,2)+pow(dy,2));

  dx*=power/n;
  dy*=power/n;
}

float friction (float c)
{
  c*=0.9;
  return c;
}

class Ball 
{
  float rad;
  Point center;
  Point contact_point;
  color col;

  Ball ( float a, float b)
  {
    center = new Point (a+=dx, b+=dy);
  }
} 

class Table
{
  Ball [] b_arr;    
  Stick st;
  Ball cue_ball;

  Table ( float a, float b )
  {

    cue_ball= new Ball( a, b);
  }
}

class Point
{
  float x;
  float y;

  Point(float a, float b)
  {
    x=a;
    y=b;
  }
}

class Stick
{
  Point start_p;
  Point end_p;
  color col;
  int length;
}

Since the stick was added it has stopped being able to bounce off the walls and I can't understand why. It was working before it was added, and then it just stopped and when it gets close it gives NullPointerException. I don't see how to stick would change anything though.

Megannn
  • 13
  • 3
  • where did you initialize `Ball [] b_arr; Stick st;`? – SMA Dec 04 '16 at 07:44
  • Did you have a look at the stacktrace? It would give you an indicator where in the code you get the nullpointerException. – Einar Dec 04 '16 at 07:44
  • provide stacktrace. – HaroldSer Dec 04 '16 at 09:33
  • I'm very new to processing. Never knew there was a stacktrace. How would I provide that? – Megannn Dec 04 '16 at 19:45
  • Got it to work, just forgot to put in the sound file and that was causing it oops – Megannn Dec 04 '16 at 20:08
  • @Maroun Please note that this is a [tag:processing] question, and [Processing != Java](http://meta.stackoverflow.com/questions/321127/processing-java). Specifically, this NPE was generated from Processing itself, and Processing does not offer a traditional stack trace in this case. The actual cause is a bit removed: in this case, it was a missing sound file. I'm voting to reopen this question as its solution is not one of the answers to the canonical question, and I'm hoping that you reopen it. – Kevin Workman Dec 21 '16 at 04:07

1 Answers1

0

Like you said in your comment: this error was caused by a missing sound file.

Processing often throws NullPointerExceptions if you don't include necessary files or don't do the correct set up. Unlike Java where you get a stack trace, it can be pretty hard to debug these types of errors since they come from inside Processing.

One thing to do is add println() statements in your code to help narrow down exactly what's happening.

Also, in the future please try to post an MCVE instead of your entire sketch. That often means starting over with a blank sketch and only adding enough code to reproduce the error. This avoids all of the confusion you had in the comments this time around.

Community
  • 1
  • 1
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107