Loadimage?

hey everyone when i compile my code

import java.applet.Applet.*;

import java.awt.*;

import javax.swing.*;

public class Loadimage extends JApplet {

private Image User1;

private ImageIcon User2;

public void intit()

{

User1 = getImage(getCodeBase(),"g.jpg" );

User2 = new ImageIcon( "g.jpg");

}

public void paint( Graphics g )

{

g.drawImage( User1, 0, 0, this );

g.drawImage( User1, 0, 120,

getWidth(), getHeight() -120, this );

User2.paintIcon( this, g, 180, 0 );

}

}

it works with an error

Exception in thread "AWT-EventQueue-2" java.lang.NullPointerException

at Loadimage.paint(Loadimage.java:20)

at sun.awt.RepaintArea.paintComponent(Unknown Source)

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)

Exception in thread "AWT-EventQueue-2" java.lang.NullPointerException

at Loadimage.paint(Loadimage.java:20)

at sun.awt.RepaintArea.paintComponent(Unknown Source)

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)

Exception in thread "AWT-EventQueue-2" java.lang.NullPointerException

at Loadimage.paint(Loadimage.java:20)

at sun.awt.RepaintArea.paintComponent(Unknown Source)

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)

how will i fix this error and get my image to print?

[3327 byte] By [JollyRogera] at [2007-10-3 5:22:21]
# 1
sorry to paint onto the screen?
JollyRogera at 2007-7-14 23:29:24 > top of Java-index,Java Essentials,Java Programming...
# 2
The User1 or User2 is null can you find out which one?By the way creating the image icon as by giving only the file name will not work in applets. You should give the URL to the image. If it is in the code base you can use getResource method to obtain the URL
LRMKa at 2007-7-14 23:29:24 > top of Java-index,Java Essentials,Java Programming...
# 3
> The User1 or User2 is null can you find out which one?Not only that they look like class names. Learn the Java naming conventions. The variable names should be "user1" and "user2".
camickra at 2007-7-14 23:29:24 > top of Java-index,Java Essentials,Java Programming...
# 4

oi thanks but i rewrite teh script.

so now it works

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.awt.Graphics2D;

public class Loadimage extends Applet implements MouseListener {

private Image User1;

private boolean inited;

private Graphics gl = null;

public Loadimage() {

inited = false;

}

public void run() {

init();

}

public void init() {

//notImage = getImage(getCodeBase(), "images/not.gif");

User1 = getImage(getCodeBase(), "user.png");

//User1 = newImage("user.png");

if (User1 != null)

inited = true;

//this.paint(gl);

addMouseListener(this);

repaint();

}

public void destroy() {

removeMouseListener(this);

}

public void update(Graphics g){

paint(g);

}

public void paint( Graphics g ) {

if (g == null) {

System.out.println("no g local");

this.showStatus("no g local");

return;

}

gl = g;

if (!inited) {

g.drawString("Not inited", 500, 500);

System.out.println("not inited");

this.showStatus("not inited");

return;

}

try {

g.setColor(Color.black);

g.drawImage( User1, 55, 55, null );

} // end try

catch (NullPointerException npe) {

npe.printStackTrace();

showStatus("nullpointer: " + User1.toString());

} // end catch

} // end paint

public void mouseReleased(MouseEvent e) {

int x = e.getX();

int y = e.getY();

repaint();

}

public void mousePressed(MouseEvent e) {

repaint();

}

public void mouseClicked(MouseEvent e) {

repaint();

}

public void mouseEntered(MouseEvent e) {

repaint();

}

public void mouseExited(MouseEvent e) {

repaint();

}

public void mouseMoved(MouseEvent e) {

repaint();

}

}

now all i must learn is how to paint another picture behind it

JollyRogera at 2007-7-14 23:29:24 > top of Java-index,Java Essentials,Java Programming...