-1

My program compiles perfectly, but whenever I try to run it throws a NullPointerException. I tried searching this up, and found that the error relates to some value being null while the program is trying to use it, but I re-checked everything and came up blank.

    import hsa.Console;
    import java.awt.*;
    import java.util.* ;
    public class PlatformAndBall2
    {
        static Console g ;
       public static void BallLoop() throws InterruptedException
    {
    do {

      int height = g.getHeight(); 
      int width = g.getWidth(); 
      int xpos,ypos ; xpos= 0; ypos =0 ; 
      int radius ;  radius = 20 ; 
      int addx, addy ; addx = 3; addy = 3; 

      xpos += addx ; 
      ypos += addy  ; 
      g.setColor(Color.red) ;
      g.clear();
      g.fillOval(xpos,ypos,radius,radius) ;

      Thread.sleep(15); // sets frames

      if ( xpos < 0 )
        addx += 3 ; 

      if ( xpos > width - radius)
         addx += -3 ;    

      if ( ypos < 0 )
        addy += 3 ;    

      if ( ypos > height -radius ) 
        addy += -3 ;
    }while (true);
       }
       public static void main(String[] args)throws InterruptedException {

        Console g = new Console() ;
        BallLoop();
       }}
Pointy
  • 405,095
  • 59
  • 585
  • 614
muko06
  • 1
  • 1

1 Answers1

0

The static field static Console g never gets initialized. In your main you're initializing a local Console g = new Console();. Initialize g = new Console();

public static void main(String[] args)throws InterruptedException {
    g = new Console();
    BallLoop();
}
StaticBeagle
  • 5,070
  • 2
  • 23
  • 34