Discord
Steam
/
General
/
Trying to make..
Login/Join
Event Log
Trying to make a bouncing ball program but this gives me error, i have done this program without usi
1 replies, posted
Search
In This Thread
import java.awt.Color; import acm.program.*; import acm.graphics.*; public class bouncing extends GraphicsProgram { private static final int DIA = 50; private static final double GRAVITY = 3; private static final int DELAY = 50; private static final double X_START = DIA/2; private static final double Y_START = 100; private static final double X_VEL = 5; private static final double BOUNCE_COLLISION = 0.9; private double xvel = X_VEL; private double yvel = 0.0; private GOval ball; public void run(){ setup(); waitForClick(); while(ball.getX() < getWidth()){ moveBall(); checkcollision(); pause(DELAY); } } private void setup(){ GOval ball =new GOval(X_START,Y_START,DIA,DIA); //ball.setColor(Color.blue); ball.setFilled(true); add(ball,X_START,Y_START); } private void moveBall(){ yvel+=GRAVITY; ball.move(xvel, yvel); } private void checkcollision(){ if(ball.getY() > getHeight() - DIA){ yvel= - yvel * BOUNCE_COLLISION; double diff = ball.getY()-(getHeight() - DIA); ball.move(0,-2*diff); } } } give me following error Exception in thread "Thread-3" java.lang.NullPointerException at bouncing.run(bouncing.java:32) at acm.program.Program.runHook(Program.java:1592) at acm.program.Program.startRun(Program.java:1581) at acm.program.AppletStarter.run(Program.java:1939) at java.lang.Thread.run(Unknown Source)
Put this in the programming section.
Sorry, you need to
Log In
to post a reply to this thread.