我有以下可运行的代码:
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文档在谷歌浏览器上运行它时:
<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
答案 0 :(得分:0)
问题与安全性有关。当我签署Applet,并在清单中给它一个好的权限节点时,它就摆脱了错误。
我在清单中添加了以下内容:
Permissions: all-permissions
Codebase: *.hobogames.atspace.cc
Application-Name: GreenCube
我使用以下命令在命令提示符中对其进行了签名:
cd homeDirectory
keytool -genkey -alias alias -keystore .keystore
cd directoryOfJar
jarsigner jar-file alias
别名通常是你的名字。
这解决了一些安全问题,并允许我加载图像。