Null pointer with image

S

Shelly

I teach Java in High School and we use CodeWarrior as our IDE.

I recently had students run into a problem that I cannot figure out but
I have isolated the problem. They wrote an applet. It runs. When
they add a label and the icon for that label points to a valid image
(using the design view Object Inspector to place it in the label), the
code crashes with a null pointer exception. The stack trace is:

java.lang.NullPointerException
at sun.awt.SunToolkit.getImageFromHash<SunToolkit.java:222>
at sun.awt.SunToolkit.getImage<SunToolkit.java:279>
at ThisApp.initComponents<Thisapp.java:94>
at ThisApp.init<Thisapp.java:82>
at sun.applet.AppletPanel.run<AppletPanel.java:339>
at java.lang.Thread.run<Thread.java:479>

If we remove the image from from the label, then it runs. Note that
this is in the init method and not in the constructor. Also, the code
for this was automatically generated by CodeWarrior and was not
modified by us.

Thanks for any help.

Sheldon
 
B

Boobay

Be sure you're pointing to the correct image and refer to documentation
as much as possible.
 
S

Shelly

In the "Object Inspector" for that label I click on "Icon". I then use
the browser that pops up to select the image. Clicking "run" generates
new code which points to the loading of this label with the icon and
the error results.

IOW, no human intervention.
 
S

Shelly

the Icon says:

?/:K:\Java\Craps2\Pictures\BigDie1.gif

K is the label attached to the network drive that is mne. The rest is
proper directory structure to the particular file.

If I open K:\Java\Craps2\Pictures\BigDie1.gif in Internet Explorer, it
displays perfectly, The applet does not, of course.
 
S

Shelly

the Icon says:

?/:K:\Java\Craps2\Pictures\BigDie1.gif

K is the label attached to the network drive that is mne. The rest is
proper directory structure to the particular file.

If I open K:\Java\Craps2\Pictures\BigDie1.gif in Internet Explorer, it
displays perfectly, The applet does not, of course.
 
T

Thomas Fritsch

Shelly said:
In the "Object Inspector" for that label I click on "Icon". I then use
the browser that pops up to select the image. Clicking "run" generates
new code which points to the loading of this label with the icon and
the error results.

IOW, no human intervention.

I don't follow your conclusion: "My Java-code is tool-generated ==> that
code is bug-free"
I recommend, you post the generated code-snippet and how you call it from
hand-written code, so that people can look what is wrong.
 
O

Oliver Wong

Shelly said:
the Icon says:

?/:K:\Java\Craps2\Pictures\BigDie1.gif

K is the label attached to the network drive that is mne. The rest is
proper directory structure to the particular file.

If I open K:\Java\Craps2\Pictures\BigDie1.gif in Internet Explorer, it
displays perfectly, The applet does not, of course.

Can you post an SSCCE? http://mindprod.com/jgloss/sscce.html

- Oliver
 
T

Thomas Hawtin

Shelly said:
I recently had students run into a problem that I cannot figure out but
I have isolated the problem. They wrote an applet. It runs. When
they add a label and the icon for that label points to a valid image
(using the design view Object Inspector to place it in the label), the
code crashes with a null pointer exception. The stack trace is:

java.lang.NullPointerException
at sun.awt.SunToolkit.getImageFromHash<SunToolkit.java:222>
at sun.awt.SunToolkit.getImage<SunToolkit.java:279>
at ThisApp.initComponents<Thisapp.java:94>
at ThisApp.init<Thisapp.java:82>
at sun.applet.AppletPanel.run<AppletPanel.java:339>
at java.lang.Thread.run<Thread.java:479>

It's difficult to tell what the problem is without having the source.
The line numbers do not match up to the two copies of the JDKs I've
looked at. From the line number differences, it looks as if you are
using the URL version of getImage. Possibly the URL is null, or
URL.openConnection returns null.

As an applet, it may be prevented from loading files. Using
Class.getResource may be more appropriate the supplying URLs for local
files or the web server.

Tom Hawtin
 
N

Nigel Wade

Shelly said:
the Icon says:

?/:K:\Java\Craps2\Pictures\BigDie1.gif

K is the label attached to the network drive that is mne. The rest is
proper directory structure to the particular file.

If I open K:\Java\Craps2\Pictures\BigDie1.gif in Internet Explorer, it
displays perfectly, The applet does not, of course.

That refers to a local file on the client, the applet cannot access this. The
image should be embedded within the jar.
 
T

Tom Sanders

Sheldon,

Had a similar problem some time ago. Depending on your code, filenames can
be case sensitive. And since you have uppercase characters in your
filename... Just thinking...

In my case it took me more than a week to find out :-((

HTH
Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,049
Members
48,769
Latest member
Clifft

Latest Threads

Top