import java.applet.*;
import java.awt.*;
import java.net.*;public class imag extends Applet{
URL url;
Image image;
public void init(){String s="http://images.sohu.com/cs/ad/ctws-logo1.jpg";
try{url=new URL(s);}
catch(MalformedURLException e){System.out.println("URL格式有错");}image=getImage(url);
}
public void paint(Graphics g){
g.drawImage(image,0,0,this);
}
public static void main(String args[]){
Applet x=new imag();
Frame f=new Frame();
x.init();
x.start();
f.add(x);
f.setSize(400,300);
}
}可以编译,运行时:提示 Exeption in thread "main" java.lang.NullPointerException at............  这是什么原因啊

解决方案 »

  1.   

    在程序中没有"main()",你只有在APPLET里运行
      

  2.   

    import java.applet.*;
    import java.awt.*;
    import java.net.*;public class imag extends Applet{
    URL url;
    Image image;
    public void init(){String s="http://images.sohu.com/cs/ad/ctws-logo1.jpg";
    try{url=new URL(s);}
    catch(MalformedURLException e){System.out.println("URL格式有错");}image=getImage(url);
    }
    public void paint(Graphics g){
    g.drawImage(image,0,0,this);
    }
    }
    编译后,appletviewer imag 就可以了。
      

  3.   

    难道就不能建立一个Frame对象 添加applet对象 来显示图象 一定要在applet里面显示图象吗 我想知道我这个程序是不是那里有错误
      

  4.   

    import java.applet.*;
    import java.awt.*;
    import java.net.*;public class imag extends Applet{
    URL url;
    Image image;
    public void init(){

    String s = "http://images.sohu.com/cs/ad/ctws-logo1.jpg";try
    {
    url=new URL(s);
    }
    catch(MalformedURLException e)
    {
    System.out.println("URL格式有错");
    }try{
    image = getImage(url);
    }catch(NullPointerException e)
    {
    System.err.println("image is a null pointer");
    }
    }
    public void paint(Graphics g){
    g.drawImage(image,0,0,this);
    }
    public static void main(String args[]){
    Applet x=new imag();
    Frame f=new Frame();
    x.init();
    x.start();
    f.add(x);
    f.setSize(400,300);
    }
    }你出错的地方在于 image = getImage(url); 并没有按你的期望获得一个引用,在这里image是一个空指针,如果你把这一行注释去了,是可以编译过去的。