| Reply | « Previous Thread | Next Thread » |
|
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) |
|
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. |
|
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! |
|
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. |
| grahamhughes |
| View Public Profile |
| Find all posts by grahamhughes |
|
try setting image variable to null on initialization
|
| Reply | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|