Reputation: 545
I am having a problem with my image not being displayed on my applet, here is the code, all pertaining to the image;
Image globe;
//Adding Image in init()
Image globe = getImage (getCodeBase (), "C:/Users/Andrew/Downloads/Computer Science/globe.jpg");
//later in code
public void paint (Graphics g)
{
g.drawString ("'The Best Travel Agency in the world' - John Travelta", 400, 675);
g.drawImage(globe, 0, 100, this);
} // paint method
public boolean action (Event e, Object o)
{
if (e.target == DomRep)
{
String DomRepBox = JOptionPane.showInputDialog ("Please Enter your name: ");
}
return true;
}
this is the error I am receiving;
java.lang.NullPointerException
at sun.java2d.pipe.DrawImage.copyImage(Unknown Source)
at sun.java2d.pipe.DrawImage.copyImage(Unknown Source)
at sun.java2d.SunGraphics2D.drawImage(Unknown Source)
at sun.java2d.SunGraphics2D.drawImage(Unknown Source)
at CPT.paint(CPT.java:129)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Upvotes: 0
Views: 1216
Reputation: 168825
Image globe = getImage (getCodeBase(), "relative/path/to/globe.jpg");
Should work just fine for an applet. If the applet is in the same directory as the HTML, this is even easier.
Image globe = getImage (getDocumentBase(), "globe.jpg");
Upvotes: 1
Reputation: 506
I don't understand what the getImage() method is, but normally you import images like this:
try {
globe = ImageIO.read(new URL("URL_OF_FILE.png"));
} catch (IOException e) {
e.printStackTrace();
}
Upvotes: 1