• 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 ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

jPanel ,nullpointer exception

Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please could you post some code so that we can see why the null pointer is occuring.

luc ndabaneze
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi rachel
sorry here is the code

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class canvas1 extends JFrame {
man mandel;
public canvas1() {

getContentPane().setLayout(new BorderLayout() );
JPanel Bottom = new JPanel();
JButton StartButton = new JButton("START");
mandel= new man();
getContentPane().add(mandel,BorderLayout.CENTER );
Thread runner = new Thread(mandel);

getContentPane().add(Bottom,BorderLayout.SOUTH );}
public Insets getInsets() {

return new Insets(2,2,2,2);

class man extends JComponent implements Runnable {

Graphics offscreen;
Image im;
double xscale,yscale;
boolean running;

double xmax = 2;
double ymax = 2;
double xmin = -1.2;
double ymin = -0.2;

public void paintComponent(Graphics g) {

if (im == null) {
g.fillRect(0,0,getWidth(),getHeight()) ; }
g.drawImage(im,0,0,null) ;}


public void run() {

Dimension d = getSize();
int maxX = d.width - 1;
int maxY = d.height - 1;
im = createImage(maxX,maxY);
offscreen = im.getGraphics();
double dx,dy;

xscale = (xmax-xmin)/d.width;
yscale = (ymax-ymin)/ d.height;

for ( int i= 0 ; i<maxX ; i++ ) {
dx= (xmin+ xscale) ;
for ( int j = 0;j<maxY;j++) {
dy = (ymin + yscale) ;
try {
Thread.currentThread().sleep(1000) ;
catch (InterruptedException e) { }
dy= dy + yscale;

dx = dx + xscale;



int calculate(double a, double bi){

double c,ci,d,z,zi;
int Max = 250;
a= 0;
bi= 0;
int count = 0;

while ( count < Max && d<4) {
z= a*a- bi*bi + xmin ;
zi = 2*(a*bi) +ymin ;
d = (a*bi);

count++ ; }

return count; }


public static void main(String[] args) {
try {
catch (Exception e) {}

canvas1 application = new canvas1();



Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This line is the cause of the error:

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.
    Bookmark Topic Watch Topic
  • New Topic