[4371] in java-interest

home help back first fref pref prev next nref lref last post

NullPointerException from drawLine() inside a thread in paint() ??

daemon@ATHENA.MIT.EDU (Eric Ho)
Thu Dec 21 01:37:54 1995

Date: Thu, 21 Dec 95 00:04:15 EST
From: eric@internet.sbi.com (Eric Ho)
To: java-interest-digest@java.sun.com

Greetings Everyone,

I'm not sure if this is a bug or not in the appletviewer (JDK b1).

I've a canvas and in its overriden paint() method, I tried to do some simple
drawings in a separate thread.  Calling Graphics' setColor() is fine but
calling drawLine() greeted me with a NullPointerException.

Am I missing something ??

Any pointers/info much appreciated.

Here is the sample code :-

------------8<------- CUT HERE -----------8<-----------

import java.applet.*;
import java.lang.*;

public class Simple extends java.applet.Applet {
  public void init() {
    setLayout(new BorderLayout());
    add("Center", new MyCanvas());
  }
  public static void main(String args[]) {
    Frame f = new Frame("Simple");
    Simple t = new Simple();

    t.init();
    t.start();
    f.add("Center", t);
    f.resize(400, 400);
    f.show();
  }
}

class MyCanvas extends java.awt.Canvas {
  public void paint(Graphics g) {
    CreativeBrush cb = new CreativeBrush(g);
    new Thread(cb).start();
    System.out.println("MyCanvas.paint(): Done.");  System.out.flush();
  }
}

class CreativeBrush implements Runnable {
  Graphics gcontext;
  public CreativeBrush(Graphics g) { 
    this.gcontext = g; 
  }
  public void run() {
    System.out.println("run(): starts.");  System.out.flush();
    CrazyDrawings();
    System.out.println("run(): done.");  System.out.flush();
  }
  void CrazyDrawings() {
    gcontext.setColor(Color.orange);
    System.out.println("CrazyDrawings(): starts.");  System.out.flush();
    gcontext.drawLine(10, 10, 90, 90);  // <<=== *** PROBLEM  HERE *****
    System.out.println("CrazyDrawings(): Done.");  System.out.flush();
  }
}
------------8<------- CUT HERE -----------8<-----------

-Eric Ho-  eric@sbi.com

-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post