Link to home
Start Free TrialLog in
Avatar of brecht
brecht

asked on

NullPointerException in update(Graphics g)

Why am I always getting NullPointerExceptions in this update method:


public synchronized void update(Graphics g) {

    Color plotColor;

    left = 10;
    right = monitor.location().x-5;
    floor = bottomPanel.location().y-50;
    length = right - left;
    inc = length/11.00;

    if(offscreenG == null) {
        offscreenImg = createImage(size().width, size().height);
        offscreenG = offscreenImg.getGraphics();
    }

    if(trajG == null) {
        trajImg = createImage(size().width, size().height);
        trajG = trajImg.getGraphics();
        drawOldTrajectories(trajG);
    }

    offscreenG.drawImage(trajImg,0,0,this);

    if(running) drawCurrentTrajectory(offscreenG);
   
    drawFloor(offscreenG);
    drawCannon(offscreenG);
    g.drawImage(offscreenImg,0,0,this);
}

  All of the drawXxx methods are declared synchronized.  I get the exception within these draw methods, usually the last time my run() method calls it (the update).  
Here is my run() method:

public void run() {

    Double tempDouble;
    tempDouble = new Double(angleField.getText().trim());
    theta = (tempDouble.doubleValue())*Math.PI/180;
    double c1 = Math.tan(theta);
    int vel = this.vel.getValue();
    double c2 = g/(2*vel*vel*Math.cos(theta)*Math.cos(theta));
    plotX = plotY = 0;
    points.removeAllElements();

    double R = c1/c2;


    long t = System.currentTimeMillis();
    for(double i = 0; i<=R/2; i+=(5/inc) ){
        plotX = (int)(inc*i) + left;
        plotY = floor - (int)(inc*(c1*i - c2*i*i));
        points.addElement(new Point(plotX, plotY));
        repaint();
        try {
            t+=sleepTime;
            Thread.sleep(Math.max(0, t - System.currentTimeMillis()));
            }
        catch (InterruptedException e) {}
    }

    peak = (vel*vel*Math.sin(theta)*Math.sin(theta))/(2*g);
    heightField.setText(""+peak);
    repaint();

    for(double i = R/2; i<=R; i+=(5/inc) ){
        plotX = (int)(inc*i) + left;
        plotY = floor - (int)(inc*(c1*i - c2*i*i));
        points.addElement(new Point(plotX, plotY));
        repaint();
        try {
            t+=sleepTime;
            Thread.sleep(Math.max(0, t - System.currentTimeMillis()));
            }
        catch (InterruptedException e) {}
    }



    plotX = left + (int)(inc*R);
    plotY = floor - (int)(inc*(c1*R - c2*R*R));
    monitor.property[trial][2].setText(""+R);
    repaint();
   
    allPoints.setElementAt((Vector)points.clone(),trial);
    running = false;
    offscreenG = null;
    trajG = null;
    repaint();

}
ASKER CERTIFIED SOLUTION
Avatar of garik
garik

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of brecht
brecht

ASKER

This basically worked.  I can't sycnhronize on "running", because its not an Object, so I just synchronized on "this".  That seems to work, but do you foresee any problem in doing that?
The only problem might be reduced multi-threading efficiency, since you lock the whole applet during update, but if the number of simultaneously running threads is not too big, that shouldn't bother. If some applets "freeze" on update, then you migth consider reducing the scope of the object for synchronization.
Avatar of brecht

ASKER

The thing is, I just don't know much about using synchronization.  I've just seen examples in books where they declare a method synchronized, or synchronize on some variable, but I don't really have a good feel for it.  What else could I use synchronize on besides "running" or "this" ?
You can synchronize on any variable of the reference type. F.ex., you could use offscreenImg or trajG for synchronization - any object that identifies overlapping area of interest for the concurrent threads. But before you dive into multithreading and synchronization, I'd suggest to work more on your existing code, clean it up. Don't be afraid to use more methods - it'd not only improve the readability of your code, but could also better define sections dealing with particular objects and ease significantly synchronization in the future. Try to really think through the concept of object oriented programming - it helped me, anyway :)