I have the following runnable code:
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);
}
}
When I run this in eclipse it works. But when I run it on google chrome with the following html document:
<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>
I get the following null pointer exception.:
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)
How do I fix?
http://vickysoderlund.atspace.cc/ /---= webpage with errored japplet on it
You should beable to download the entire folder here: https://drive.google.com/file/d/0B0ng1kJkou-lcUlmWk91bEVXX28/edit?usp=sharing
And SRC code is here: https://drive.google.com/file/d/0B0ng1kJkou-lYXRERWFnU1czX0k/edit?usp=sharing
The problem had to do with security. When I signed the Applet, and gave it a good permissions node in the manifest it got rid of the error.
I added the following to the manifest:
Permissions: all-permissions
Codebase: *.hobogames.atspace.cc
Application-Name: GreenCube
I signed it in the command prompt using the following commands:
cd homeDirectory
keytool -genkey -alias alias -keystore .keystore
cd directoryOfJar
jarsigner jar-file alias
Where alias is usually your name.
This solved some security issues, and allowed me to load the image.