Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Having trouble painting object...

Status
Not open for further replies.

MarcGerrish

Programmer
Mar 6, 2004
2
GB
Hopefully someone will know why I can't get this to work properly, as I've been trying to suss it outy myself for the last four hours and am not getting anywhere.

It's a simple program to display two balls and a 'passage' (three rectangles at this stage) on a 'playingfield'

I know that the commands causing the runtine errors are the "blueball.paint(g)" "redball.paint(g)" and "thepassage.paint(g)" Everything else works fine.

Any ideas anyone?

Code:
*snip*

class ball
{
  Rectangle location;
  Color ballcolor=Color.blue;

  ball(int x, int y, int wh)
  {
    location=new Rectangle(x,y,wh,wh);
  }

  public void paint(Graphics g)
  {
    g.setColor(ballcolor);
    g.fillOval(location.x,location.y,location.width,location.height);
  }
}

*snip 'class passage' - same as 'class ball'

class field extends Frame implements Runnable
{
  Thread runner;
  ball blueball,redball;
  passage thepassage;
  int screenx=600;
  int screeny=400;
  int divx=20;
  int passagey=100;
  Color bgcolor=new Color(255,128,0);
  Color divcolor=Color.black;

  field()
  {
    super("Demon Balls");
    setSize(screenx,screeny);
    setBackground(bgcolor);
    ball blueball=new ball(100,175,50);
    ball redball=new ball(250,175,50);
    passage thepassage=new passage(((screenx-divx)/2),((screeny-passagey)/2),divx,passagey);
    show();
    runner=new Thread(this);
    runner.start();
  }

  public void drawbg(Graphics g)
  {
    g.setColor(bgcolor);
    g.drawRect(0,0,screenx,screeny);
    g.setColor(divcolor);
    g.fillRect(((screenx-divx)/2),0,divx,screeny);
  }

  public void paint(Graphics g)
  {
    drawbg(g);
    blueball.paint(g);
    redball.paint(g);
    thepassage.paint(g);
  }

  public void run()
  {
    repaint();
    try{Thread.sleep(500);}
    catch(InterruptedException e){}
  }
}

public class demonballs
{
  public static void main(String args[])
  {
    field playingfield;
    playingfield=new field();
    playingfield.show();
  }
}
 
The error message mentions a null pointer exception. I know what this means; attempting to access an object that doesn't exist, but as far as I can see blueball, redball and thepassage have all been declared correctly :-s
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top