hello guys i have this drawing application where i compute pixel on a Jpanel with different colors , as i did not want to use an applet, i created a JFrame as the container of my Jpanel, My Jpanel implements Runnable as well , when i tried to called the public void run()method of the Jpanel by calling start inside the construtor of JFrame , it says nullpointer exception , could anyone tell me why and where is the best place to call my runnable?
There was a young man of Cadiz <br />Who inferred that life is what it is , <br />for he early learnt ,<br /> If it were what it weren't ,<br />it could not be that which it is
Why? It is asking for a graphics context of a component that has not been realized, ie, the setVisible method has not yet been called on the JFrame so the JComponent is not on the screen/not visible and therefore the JComponent does not have a graphics context. The call to getGraphics returns null. So arrange to start the thread after the gui is realized. Here are some minimal changes that allow your app to run:
The paintComponent method in the JComponent is called as the JComponent is asked to render itself in the gui. So you can be assured that a graphic context, Graphics g, exists on this first trip through paintComponent. Now it is safe to get the size of the JComponent and to initialize the Image im. After this you can safely start your thread.
if you think brussel sprouts are yummy, you should try any other food. And this tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java