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

new URL ( )

0 views
Skip to first unread message

Ando

unread,
Dec 23, 2002, 10:50:58 AM12/23/02
to
What is wrong with this statements ? I get "unreported exception
java.net.MalformedURLException : must be caught or declared to be thrown. I
had found this "new URL(::::)" example in a place that now I cannot
remember.

Image image = Toolkit.getDefaultToolkit().getImage( new
URL("C:/PgmJava/PROVAIMG/tomino.gif"));

Thanks in advance.Regards
Ando

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.net.URL;
//import java.applet.*;
public class Grafico extends Frame {

Image image;

public Grafico (){

Image image = Toolkit.getDefaultToolkit().getImage( new
URL("C:/PgmJava/PROVAIMG/tomino.gif"));
ImagePanel imagepanel = new ImagePanel(image);
setIconImage(image);
setSize(400,200);
show();
JFrame f= new JFrame("Mia Frame");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0); }});
f.getContentPane().add(imagepanel, BorderLayout.CENTER);
f.setSize(new Dimension(550,100));
f.setVisible(true);

}


class ImagePanel extends JPanel {
Image image;

public ImagePanel (Image image) {
this.image = image;}

public void paintComponent(Graphics g){
super.paintComponent(g);

g.drawImage(image, 50, 50, this);


}}

public static void main ( String[] args) {

new Grafico();
} }

A Bag Of Memes

unread,
Dec 23, 2002, 12:51:25 PM12/23/02
to

"Ando" <rlnd...@hotmail.com> wrote in message
news:ChGN9.24186$R45.1...@news2.tin.it...

> What is wrong with this statements ? I get "unreported exception
> java.net.MalformedURLException : must be caught or declared to be thrown.
I
> had found this "new URL(::::)" example in a place that now I cannot
> remember.
>
> Image image = Toolkit.getDefaultToolkit().getImage( new
> URL("C:/PgmJava/PROVAIMG/tomino.gif"));

There's nothing wrong with that statement. The compiler is telling you to
handle the exception it might throw. Either wrap it in a try/catch block or
add a throws clause to the enclosing method declaration.

Ando

unread,
Dec 23, 2002, 11:20:06 AM12/23/02
to
The preceding error was due to a "protocol error" and I have changed the
"new URL("c:/....")
with "new URL("http:/....").
I 've coded the try / catch instructions .Follow the"printstackTrace".
Why NullPointerException at
at PROVAIMG.Grafico$ImagePanel.paintComponent(Grafico.java:44)

Regards.

java.lang.NullPointerException
at sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:48)
at sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:715)
at sun.java2d.pipe.ValidatePipe.copyImage(ValidatePipe.java:147)
at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:2782)
at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:2772)
at PROVAIMG.Grafico$ImagePanel.paintComponent(Grafico.java:44)
at javax.swing.JComponent.paint(JComponent.java:804)
at javax.swing.JComponent.paintChildren(JComponent.java:643)
at javax.swing.JComponent.paint(JComponent.java:813)
at javax.swing.JComponent.paintChildren(JComponent.java:643)
at javax.swing.JComponent.paint(JComponent.java:813)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:552)
at javax.swing.JComponent.paintChildren(JComponent.java:643)
at
javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4742)
at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4688)
at javax.swing.JComponent.paint(JComponent.java:794)
at
java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
at
sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
at
sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
at java.awt.Container.paint(Container.java:1123)
at sun.awt.RepaintArea.paint(RepaintArea.java:180)
at
sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:260)
at java.awt.Component.dispatchEventImpl(Component.java:3586)
at java.awt.Container.dispatchEventImpl(Container.java:1437)
at java.awt.Window.dispatchEventImpl(Window.java:1566)
at java.awt.Component.dispatchEvent(Component.java:3367)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:445)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.ja
va:190)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java
:144)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:130)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:98)
java.lang.NullPointerException
at sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:48)
at sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:715)
at sun.java2d.pipe.ValidatePipe.copyImage(ValidatePipe.java:147)
at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:2782)
at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:2772)
at PROVAIMG.Grafico$ImagePanel.paintComponent(Grafico.java:44)
at javax.swing.JComponent.paint(JComponent.java:804)
at javax.swing.JComponent.paintChildren(JComponent.java:643)
at javax.swing.JComponent.paint(JComponent.java:813)
at javax.swing.JComponent.paintChildren(JComponent.java:643)
at javax.swing.JComponent.paint(JComponent.java:813)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:552)
at javax.swing.JComponent.paintChildren(JComponent.java:643)
at
javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4742)
at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4688)
at javax.swing.JComponent.paint(JComponent.java:794)
at
java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
at
sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
at
sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
at java.awt.Container.paint(Container.java:1123)
at sun.awt.RepaintArea.paint(RepaintArea.java:180)
at
sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:260)
at java.awt.Component.dispatchEventImpl(Component.java:3586)
at java.awt.Container.dispatchEventImpl(Container.java:1437)
at java.awt.Window.dispatchEventImpl(Window.java:1566)
at java.awt.Component.dispatchEvent(Component.java:3367)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:445)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.ja
va:190)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java
:144)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:130)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:98)
java.lang.NullPointerException
at sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:48)
at sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:715)
at sun.java2d.pipe.ValidatePipe.copyImage(ValidatePipe.java:147)
at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:2782)
at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:2772)
at PROVAIMG.Grafico$ImagePanel.paintComponent(Grafico.java:44)
at javax.swing.JComponent.paint(JComponent.java:804)
at javax.swing.JComponent.paintChildren(JComponent.java:643)
at javax.swing.JComponent.paint(JComponent.java:813)
at javax.swing.JComponent.paintChildren(JComponent.java:643)
at javax.swing.JComponent.paint(JComponent.java:813)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:552)
at javax.swing.JComponent.paintChildren(JComponent.java:643)
at
javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4742)
at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4688)
at javax.swing.JComponent.paint(JComponent.java:794)
at
java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
at
sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
at
sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
at java.awt.Container.paint(Container.java:1123)
at sun.awt.RepaintArea.paint(RepaintArea.java:180)
at
sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:260)
at java.awt.Component.dispatchEventImpl(Component.java:3586)
at java.awt.Container.dispatchEventImpl(Container.java:1437)
at java.awt.Window.dispatchEventImpl(Window.java:1566)
at java.awt.Component.dispatchEvent(Component.java:3367)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:445)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.ja
va:190)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java
:144)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:130)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:98)


Moshix

unread,
Dec 23, 2002, 11:07:08 AM12/23/02
to rlnd...@hotmail.com
Hi Ando,

Nothing is wrong with this statement by its own...
The problem is that the URL c'tor is declared as "throws
MalformedURLException" (which is a cheched exception).

For more information on exception handling check the Java tutorial from Sun:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/exception.html

For the moment try the following code:

try {


Image image = Toolkit.getDefaultToolkit().getImage(
new URL("C:/PgmJava/PROVAIMG/tomino.gif"));

} catch (MalformedURLException mue) {
mue.printStackTrace();
}

MoshiX

Ando

unread,
Dec 23, 2002, 12:36:47 PM12/23/02
to

The preceding error was due to a "protocol error" and I have changed the
"new URL("c:/....") with "new URL("http:/....").
I 've coded the try / catch instructions .Follow the"printstackTrace".
Why NullPointerException at
PROVAIMG.Grafico$ImagePanel.paintComponent(Grafico.java:44)
that is my statement " g.drawImage(image0,0,this);" ?
Maybe that means that the getImage of "tomino.gif" is failed, I think .
What is wrong ?
Thanks a lot
0 new messages