views:

541

answers:

6

I have found a source code and i added it to my frame just for testing which it uses Java2D. But it thows an exception. I don't understand why.

my class:

package ClientGUI;




import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.RenderingHints;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.PathIterator;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;

/**
 *
 * @author ICC
 */

public class SignInFrame extends javax.swing.JFrame implements Runnable {

private static int iw,  ih,  iw2,  ih2;
private static Image img;
private static final int FORWARD = 0;
private static final int BACK = 1;

// the points of the curve
private Point2D pts[];

// initializes direction of movement forward, or left-to-right
private int direction = FORWARD;
private int pNum;
private int x,  y;
private Thread thread;
private BufferedImage bimg;

/** Creates new form SignInFrame */
public SignInFrame() {
    initComponents();
    img = getToolkit().getImage(Image.class.getResource("Yahoo-Messanger.jpg"));
    try {
        MediaTracker tracker = new MediaTracker(this);
        tracker.addImage(img, 0);
        tracker.waitForID(0);
    } catch (Exception e) {
    }
    iw = img.getWidth(this);
    ih = img.getHeight(this);
    iw2 = iw / 2;
    ih2 = ih / 2;

}

public void reset(int w, int h) {
    pNum = 0;
    direction = FORWARD;

    // initializes the cubic curve
    CubicCurve2D cc = new CubicCurve2D.Float(
            w * .2f, h * .5f, w * .4f, 0, w * .6f, h, w * .8f, h * .5f);

    // creates an iterator to define the boundary of the flattened curve
    PathIterator pi = cc.getPathIterator(null, 0.1);
    Point2D tmp[] = new Point2D[200];
    int i = 0;

    // while pi is iterating the curve, adds points to tmp array
    while (!pi.isDone()) {
        float[] coords = new float[6];
        switch (pi.currentSegment(coords)) {
            case PathIterator.SEG_MOVETO:
            case PathIterator.SEG_LINETO:
                tmp[i] = new Point2D.Float(coords[0], coords[1]);
        }
        i++;
        pi.next();
    }
    pts = new Point2D[i];

    // copies points from tmp to pts
    System.arraycopy(tmp, 0, pts, 0, i);
}

public void step(int w, int h) {
    if (pts == null) {
        return;
    }
    x = (int) pts[pNum].getX();
    y = (int) pts[pNum].getY();
    if (direction == FORWARD) {
        if (++pNum == pts.length) {
            direction = BACK;
        }
    }
    if (direction == BACK) {
        if (--pNum == 0) {
            direction = FORWARD;
        }
    }
}

public void drawDemo(int w, int h, Graphics2D g2) {
    g2.drawImage(img,
            0, 0, x, y,
            0, 0, iw2, ih2,
            this);
    g2.drawImage(img,
            x, 0, w, y,
            iw2, 0, iw, ih2,
            this);
    g2.drawImage(img,
            0, y, x, h,
            0, ih2, iw2, ih,
            this);
    g2.drawImage(img,
            x, y, w, h,
            iw2, ih2, iw, ih,
            this);
}

public Graphics2D createGraphics2D(int w, int h) {
    Graphics2D g2 = null;
    if (bimg == null || bimg.getWidth() != w || bimg.getHeight() != h) {
        bimg = (BufferedImage) createImage(w, h);
        reset(w, h);
    }
    g2 = bimg.createGraphics();
    g2.setBackground(getBackground());
    g2.clearRect(0, 0, w, h);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_RENDERING,
            RenderingHints.VALUE_RENDER_QUALITY);
    return g2;
}

@Override
public void paint(Graphics g) {
    Dimension d = getSize();
    step(d.width, d.height);
    Graphics2D g2 = createGraphics2D(d.width, d.height);
    drawDemo(d.width, d.height, g2);
    g2.dispose();
    g.drawImage(bimg, 0, 0, this);
}

public void start() {
    thread = new Thread(this);
    thread.setPriority(Thread.MIN_PRIORITY);
    thread.start();
}

public synchronized void stop() {
    thread = null;
}

public static void main(String argv[]) {

    SignInFrame f = new SignInFrame();





    f.start();
}

public void run() {

    Thread me = Thread.currentThread();
    while (thread == me) {
        repaint();
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            break;
        }
    }
    thread = null;
}}

the exception:

  init:
  deps-jar:
  Compiling 1 source file to C:\Users\ICC\Documents\NetBeansProjects\YahooServer\build\classes
  compile-single:
  run-single:
  Uncaught error fetching image:
  java.lang.NullPointerException
          at sun.awt.image.URLImageSource.getConnection(URLImageSource.java:97)
          at sun.awt.image.URLImageSource.getDecoder(URLImageSource.java:107)
          at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:240)
          at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
          at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)
+1  A: 

Use your debugger and define a breakpoint so that your application stops when the NPE is thrown. Then you'll find the line of code where you have a null reference which causes trouble. (or set the breakpoint on the line of code that is printed on the stack trace)

It's nearly impossible to give more detailed help without seeing the part of the code that throws the exception.

Edit

Simple typo this time? Is the image file called Yahoo-Messanger.jpg or shouldn't it be Yahoo-Messenger.jpg instead? Could be that you can't find the image. Unfortunatly your stacktrace snippet doesn't include the line of code in your class where the trouble starts.

Andreas_D
+3  A: 

The offending line is here img = getToolkit().getImage(Image.class.getResource("Yahoo-Messanger.jpg")); Make sure that the file exists refer to this doc to see the order on how the resources are loaded Java Doc for getResource

Greg
"Messanger" in the line Greg highlighted is misspelled, that might be a possible cause for it failing to find the image.
laura
Thanks,I get it.
Johanna
+1  A: 

I find the best way to solve these problems is one step at a time. The exception states that there was an error fetching the image. In SignInFrame() you are trying to retrieve an image img = getToolkit().getImage(Image.class.getResource("Yahoo-Messanger.jpg"));

Make sure that you are pointing to the image correctly. It may also help to look at the javadoc for getResource.

Also, I think (and im no expert) that it is generally a good idea to put methods that may throw exception in a try-catch. This way you know exactly where the error happens when the exception is throw.

UmYeah
+2  A: 
java.lang.NullPointerException
    at sun.awt.image.URLImageSource.getConnection(URLImageSource.java:97)

I do a wild guess: the URL is null. You need to debug the variables which were used around the time that your own code appears for the first time in the stacktrace. I've already explained in one of your previous topics how to debug.

BalusC
I think a null url is very likely the cause. Look at the code for URLImageSource.getConnection(): http://www.docjar.com/html/api/sun/awt/image/URLImageSource.java.html I think there's a good chance that the getResource() call that UmYeah mentions is the issue.
JeffH
A: 

I think that when you call f.start() it will actually invoke the run() method and not your own start() method, since the class implements Runnable.

lorenzog
A: 

the question is already answered, but I would suggested two additional changes in this line:

    img = getToolkit().getImage(Image.class.getResource("Yahoo-Messanger.jpg"));

1 - check the returned URL, it will be null if the image was not found.

2 - I think that Image.class is misleading here. Use getClass() as there is no (apparent) reason to use the Classloader of the Image class.

    URL url = getClass().getResource("Yahoo-Messanger.jpg");
    if (url == null) {
        // some error handling here: throw an Exception, logging, ...
    }
    img = getToolkit().getImage(url);

throwing an Exception would be the best solution IMHO.

Carlos Heuberger