Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

java.lang.NullPointerException

0 views
Skip to first unread message

none

unread,
Apr 9, 2007, 5:46:58 AM4/9/07
to
> Exception in thread "main" java.lang.NullPointerException
> at javax.swing.ImageIcon.<init>(ImageIcon.java:138)
> at EsempiJava.Capitolo10.AboutPanel.<init>(ResourceTest.java:59)
> at EsempiJava.Capitolo10.ResourceTestFrame.<init>(ResourceTest.java:29)
> at EsempiJava.Capitolo10.ResourceTest.main(ResourceTest.java:13)
> Java Result: 1
> BUILD SUCCESSFUL (total time: 10 seconds)

These are the msg .... the problem is in the first line (
javax.swing.ImageIcon.<init>(ImageIcon.java:138)
and the others are in consequence of it ?
Thanks a lot.

PS- I am reading "Core Java 2 " vol 1 - Fundamentals (Mc Graw Hill ) .
this is chapter 10....and this is a bug in its program
ResourceTest.java......!!!!

Andrew Thompson

unread,
Apr 9, 2007, 11:09:54 AM4/9/07
to
none wrote:
>> Exception in thread "main" java.lang.NullPointerException
>> at javax.swing.ImageIcon.<init>(ImageIcon.java:138)

My best guess* is that in the code lines invoked before
that, a call to getResource was made for the image file
(a .jpg, .gif, .png etc.). But it could not be found on the
classpath.

>These are the msg .... the problem is in the first line (
>javax.swing.ImageIcon.<init>(ImageIcon.java:138)
>and the others are in consequence of it ?

The other lines I trimmed are simply the execution
path of the code. They indicate what parts of the code
were visited before the failure. There is only one error.

>PS- I am reading "Core Java 2 " vol 1 - Fundamentals (Mc Graw Hill ) .
> this is chapter 10....

* ..but I am not reading that book, and cannot find
the code on the net, and you did not post it.
So everything I wrote above is simply 'guesses'.

>..and this is a bug in its program
>ResourceTest.java......!!!!

Please fix those sticky '.' and '!' keys.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-setup/200704/1

none

unread,
Apr 9, 2007, 11:55:58 AM4/9/07
to
A part of the program is below and among + signs where I get the error.

> class AboutPanel extends JPanel
> {
> public AboutPanel()
> {
> setLayout(new BorderLayout());
>
> // aggiunge area di testo
> textArea = new JTextArea();
> add(new JScrollPane(textArea), BorderLayout.CENTER);

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>
> // add About button
> URL aboutURL = AboutPanel.class.getResource("about.gif");

// I ' ve added the instrution below to trace the preceding
// instruction and I get null . And the about.gif image is
// in the same folder of the .java program .

>
> System.out.println(aboutURL);


>
> JButton aboutButton = new JButton("About",
> new ImageIcon(aboutURL));
> aboutButton.addActionListener(new AboutAction());
> add(aboutButton, BorderLayout.SOUTH);

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Thanks a lot in advance.

Andrew Thompson

unread,
Apr 9, 2007, 1:03:33 PM4/9/07
to
none wrote:
>A part of the program is below ..

Since we do not yet know why this program is failing,
it might be the parts that are not shown, that are causing
the error.

>..and among + signs where I get the error.
>
>> class AboutPanel extends JPanel

What package is AboutPanel declared in? *

..


>> URL aboutURL = AboutPanel.class.getResource("about.gif");
>
> // I ' ve added the instrution below to trace the preceding
> // instruction and I get null .

That was a sensible thing to check.

>...And the about.gif image is


> // in the same folder of the .java program .

* If AboutPanel is in package (and directory)
"helpcomponents", the path to "about.gif" is
"helpcomponents/about.gif".

none

unread,
Apr 9, 2007, 2:11:06 PM4/9/07
to
It 's only a file , can you test it ?


>
> import java.awt.*;
> import java.awt.event.*;
> import java.io.*;
> import java.net.*;
> import javax.swing.*;
>
> public class ResourceTest
> {
> public static void main(String[] args)
> {
> ResourceTestFrame frame = new ResourceTestFrame();
> frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> frame.setVisible(true);
> }
> }
>
> /**
> Un frame con un pannello che ha componenti che mostrano
> accesso alle risorse da un file JAR.
> */
> class ResourceTestFrame extends JFrame
> {
> public ResourceTestFrame()
> {
> setTitle("ResourceTest");
> setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
> setContentPane(new AboutPanel());
> }
>
> public static final int DEFAULT_WIDTH = 300;
> public static final int DEFAULT_HEIGHT = 300;
> }
>
> /**
> Un pannello con un'area di testo e un pulsante "About". Premendo
> il pulsante si riempie l'area di testo con un testo da una risorsa.
> */


> class AboutPanel extends JPanel
> {
> public AboutPanel()
> {
> setLayout(new BorderLayout());
>
> // aggiunge area di testo
> textArea = new JTextArea();
> add(new JScrollPane(textArea), BorderLayout.CENTER);
>

> // aggiunge pulsante About


> URL aboutURL = AboutPanel.class.getResource("about.gif");
>

> System.out.println(aboutURL);
>
>
> JButton aboutButton = new JButton("About",
> new ImageIcon(aboutURL));
> aboutButton.addActionListener(new AboutAction());
> add(aboutButton, BorderLayout.SOUTH);
> }
>

> private JTextArea textArea;
>
> private class AboutAction implements ActionListener
> {
> public void actionPerformed(ActionEvent event)
> {
> try
> {
> // legge testo da risorse nell'area di testo
> InputStream in = AboutPanel.class.
> getResourceAsStream("about.txt");
> BufferedReader reader = new BufferedReader(new
> InputStreamReader(in));
> String line;
> while ((line = reader.readLine()) != null)
> textArea.append(line + "\n");
> }
> catch(IOException exception)
> {
> exception.printStackTrace();
> }
> }
> }
> }

Andrew Thompson

unread,
Apr 9, 2007, 2:32:21 PM4/9/07
to
none wrote:
>It 's only a file..

What? What is only a file? Are you
referring to the GIF, the Java source, the
package (directory) it is in? Something else?

> .., can you test it ?

What to do you want me to test?

Please refrain from top-posting. I
find it very confusing.
<http://www.physci.org/codes/javafaq.html#toppost>

Message posted via http://www.javakb.com

none

unread,
Apr 9, 2007, 3:59:50 PM4/9/07
to
Andrew Thompson ha scritto:

> none wrote:
>> It 's only a file..
>
> What? What is only a file? Are you
> referring to the GIF, the Java source, the
> package (directory) it is in? Something else?
>
>> .., can you test it ?
>
> What to do you want me to test?
>
> Please refrain from top-posting. I
> find it very confusing.
> <http://www.physci.org/codes/javafaq.html#toppost>

> What? What is only a file? Are you
> referring to the GIF, the Java source, the
> package (directory) it is in? Something else?

Java source ,of course !

> What to do you want me to test?

the program , if you want ,
not if it is a tragedy , of course.


> Please refrain from top-posting. I
> find it very confusing.

It's a tragedy , I'm afraid.

>

0 new messages