我有以下可运行代码:
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JPanel;
public class Main extends JApplet{
public static Main instance;
public static ImageIcon getImage(String fname){
return new ImageIcon(instance.getImage(instance.getDocumentBase(),fname));
}
public void init(){
instance = this;
@SuppressWarnings("serial")
JPanel p = new JPanel(){
ImageIcon img = getImage("YES.png");
public void paintComponent(Graphics g){
super.paintComponent(g);
if(img != null)
g.drawImage(img.getImage(),0,0,getWidth(),getHeight(),null);
}
};
this.setContentPane(p);
}
}
当我在 Eclipse 中运行它时,它可以工作。但是当我使用以下 html 文档在 google chrome 上运行它时:
<html>
<head>
<title>VResponse</title>
<style type="text/css">
body{
background-color:white;
padding:1%;
}
</style>
</head>
<body>
<div>
<applet code="Main.class" width="800" height="600" />
</div>
</body>
</html>
我得到以下空指针异常:
java.lang.NullPointerException
at sun.awt.SunToolkit.getImageFromHash(Unknown Source)
at sun.awt.SunToolkit.getImage(Unknown Source)
at Main.getImage(Main.java:15)
at Main$1.<init>(Main.java:21)
at Main.init(Main.java:20)
at com.sun.deploy.uitoolkit.impl.awt.AWTAppletAdapter.init(Unknown Source)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
我该如何解决?
http://vickysoderlund.atspace.cc/ /---= 带有错误 japplet 的网页
您应该可以在此处下载整个文件夹: https ://drive.google.com/file/d/0B0ng1kJkou-lcUlmWk91bEVXX28/edit?usp=sharing
SRC 代码在这里: https ://drive.google.com/file/d/0B0ng1kJkou-lYXRERWFnU1czX0k/edit?usp=sharing