You Are Here:

Community: Developer Discussion Boards

#1 Old NullPointerException - 2004-10-28, 21:51

Join Date: Oct 2004
Posts: 2
sloban
Offline
Registered User
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)
Reply With Quote

#2 Old Well, try this - 2004-10-29, 07:52

Join Date: Oct 2004
Posts: 2
Location: Germany, Ulm.
s_ch
Offline
Registered User
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.
Reply With Quote

#3 Old It's a scope issue, then. - 2004-10-30, 02:25

Join Date: Oct 2004
Posts: 2
sloban
Offline
Registered User
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!
Reply With Quote

#4 Old 2004-10-30, 15:33

Join Date: Jun 2003
Posts: 4,325
Location: Cheshire, UK
grahamhughes's Avatar
grahamhughes
Offline
Forum Nokia Champion
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.
Reply With Quote

#5 Old this might help - 2004-11-18, 11:23

Join Date: Nov 2004
Posts: 3
pank_edge
Offline
Registered User
try setting image variable to null on initialization
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

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

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fdiscussionE2eforumE2enokiaE2ecomE2fforumE2fshowthreadE2ephpE3ftE3d134434X qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE44iscussionQ qdcZtypeQUqfntypeZE44iscussionContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZentertainmentQ qfnZtopicQUqfnTopicZj2meQ qfnZtopicQUqfnTopicZjavaQ qfnZtopicQUqfnTopicZmediaQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE44iscussionQ qfnZtypeQUqfntypeZE44iscussionContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE44iscussionQ qrdfZtypeQUqfntypeZE44iscussionContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ