×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    2

    NullPointerException

    Hi all,

    I'm trying to make the venerable splash screen code from Sun's Tech Tip Archive work.

    For emulators (in Sun Studio Mobility 6) that are WTK 1.04, it works fine. However, for emulators that are WTK 2.1 or any emulator so far from Nokia's Developer Suite, it throws a NullPointerException that seems to be related to the g.drawImage (...) call.

    Anyone see this before? I'll append the code and then the exception trace.

    =======

    class SplashScreen extends Canvas {
    private Display display;
    private Displayable next;
    private Timer timer = new Timer();
    private Image img;

    SplashScreen(Display display, Displayable next) {
    this.display = display;
    this.next = next;

    display.setCurrent(this);

    try {
    img = Image.createImage("/DivvyUp.png");
    }
    catch (IOException e) {
    throw new RuntimeException("Can't load image. " + e);
    }
    }

    protected void keyPressed( int keyCode ) {
    dismiss();
    }

    protected void paint(Graphics g) {
    g.setGrayScale(255);
    g.fillRect(0, 0, getWidth(), getHeight());
    g.drawImage(img, getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);

    }

    protected void pointerPressed( int x, int y ) {
    dismiss();
    }

    protected void showNotify() {
    timer.schedule(new CountDown(), 5000);
    }

    private void dismiss() {
    timer.cancel();
    display.setCurrent( next );
    }

    private class CountDown extends TimerTask {
    public void run() {
    dismiss();
    }
    }
    }

    =======

    Running with storage root DefaultColorPhone
    java.lang.NullPointerException:
    at SplashScreen.paint(+42)
    at javax.microedition.lcdui.Canvas.callPaint(+80)
    at javax.microedition.lcdui.Display.repaint(+78)
    at javax.microedition.lcdui.Display.registerNewCurrent(+238)
    at javax.microedition.lcdui.Display.access$400(+6)
    at javax.microedition.lcdui.Display$DisplayAccessor.foregroundNotify(+46)
    at javax.microedition.lcdui.Display$DisplayManagerImpl.notifyWantsForeground(+153)
    at javax.microedition.lcdui.Display$DisplayManagerImpl.access$100(+6)
    at javax.microedition.lcdui.Display.setCurrentImpl(+98)
    at javax.microedition.lcdui.Display.setCurrent(+29)
    at SplashScreen.<init>(+30)
    at DivvyUpMIDlet.initMIDlet(+23)
    at DivvyUpMIDlet.startApp(+11)
    at javax.microedition.midlet.MIDletProxy.startApp(+7)
    at com.sun.midp.midlet.Scheduler.schedule(+266)
    at com.sun.midp.main.Main.runLocalClass(+28)
    at com.sun.midp.main.Main.main(+116)
    Execution completed.
    382915 bytecodes executed
    14 thread switches
    494 classes in the system (including system classes)
    2639 dynamic objects allocated (96700 bytes)
    6 garbage collections (61624 bytes collected)

  2. #2
    Registered User
    Join Date
    Oct 2004
    Location
    Germany, Ulm.
    Posts
    2

    Well, try this

    Hello,

    Your source seems to be fine. Try checking your image file name again. It is case sensitive, and should be exactly as in the source, "DivvyUp" or whatever, so those D and U must be capital letters. the best way to do it, dont use capitals in the name.

    I had this problem, if the file name is wrong, it is the same NullPointerException. Well try that, if that doesnot help, we will see what else can be done.

    Regards,
    Serge.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    2

    It's a scope issue, then.

    Since the Image.createImage doesn't throw an exception, I didn't understand how the file name could be the problem. But Sergey's message (thanks Serge!) got me to thinking what the null pointer exception really meant.

    So, I pulled the try/catch block that contains createImage into the paint() method as a test.

    It worked. Hmmmm ...

    So I changed the "private Image img;" to "protected Image img;" and put the createImage back into the constructor where it belongs.

    It works. :-)

    Not really being a Java guy, I don't pretend to understand why a private member variable visible in the constructor wouldn't be visible to a protected method -- and that only in certain emulators -- but there you have it.

    Thanks again, Serge!

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395
    Hmmm... that doesn't make a lot of sense, as scope is not relevant here. I'd guess that there's some set of circumstances where the Canvas is becoming current (and getting painted) before the Image is loaded. I wouldn't put a call to Display.setCurrent() in the constructor. Make sure the object is properly constructed before making it the current displayable.

    Graham.

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    3

    this might help

    try setting image variable to null on initialization

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×