• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Null Pointer Exception when using Graphics libraries

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,
I have athread that updates a graphic on the screen unfortunatly this constantly throws a null pointer exception from the graphics libraries everytime the thread calls the graphics to update. Does anyone know a way around this?
thanks,
James
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without a stack trace and perhaps a snippet of relevant code we can't even start to help you. But on the topic of threads, beware that most Swing methods are not threadsafe.
- Peter
 
James Johnson
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Peter den Haan:
Without a stack trace and perhaps a snippet of relevant code we can't even start to help you. But on the topic of threads, beware that most Swing methods are not threadsafe.
- Peter



Okay heres a sample of my code:
The panel to draw on is passed from another method

class ButtonPanel extends JPanel
{
ScreenUpdate su = new ScreenUpdate(this);
public ButtonPanel ()
{
}
su.start();
}
public void DefineButton(String name, final int lightid,final int lightxa, final int lightya,final int lightxb, final int lightyb)
{
try
{
su.draw(lightid,0,0,0,0);
} ^^^^^^^^^----This throws an exception
}
}

class ScreenUpdate extends Thread
{
private JPanel box;
public ScreenUpdate(JPanel c)
{
box = c;
}
public boolean draw(int lightid,int lightxa,int lightya, int lightxb, int lightyb)
{
Graphics g = box.getGraphics();
g.fillOval(btnx,btny,15,15);
g.setColor(Color.black);
g.drawOval(btnx,btny,15,15);
return lightstate[lightid];
}
public void run(){
while(true){
try
{
draw(SPlight,0,0,0,0);
^^^^^^^^^----This throws an exception everytime its called
querysp();
}
catch (Exception e){}
try
{
Thread.sleep(1000);}
catch( InterruptedException e) {
System.err.println("Caught exception at Run" + e.toString());
}
}
}
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi James,
getGraphics() is pretty likely to return null if you're still in your ButtonPanel's constructor - from API javadocs getGraphics() returns

"A graphics context for this component, or null if it has none."

. not sure why this would continue to return null once component visible though (what's that lightstate array? looks suspicious...).
looks like some questions for the Swing forum as well, since custom painting of components is usually done throughoverriding paintComponent(). check out http://java.sun.com/docs/books/tutorial/uiswing/painting/overview.html
further than that, a few questions: is that su.start() meant to be in your ButtonPanel constructor? when, and on what thread of execution, is DefineButton() called? why did you decide to use a thread in the first place? what advantages is the thread meant to be providing you with?
oh, go on, post an e.printStackTrace()...
cheers, dan.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic