getCodeBase() gives nullpointer in Java Applet

2019-09-18 07:50发布

I have created an applet to read some info from a file on the server. I try to access the file using the following code:

Properties Settings = new Properties();
settings.load(new URL(getDocumentBase(), "settings.ini")).openStream());

All of a sudden, the second line is giving me the error:

java.lang.NullPointerException
at java.applet.Applet.getDocumentBase(Unknown Source)

My applet is signed and I access it through my localhost.Why can't I use getDocumentBase anymore?

Btw, I am using Netbeans Web Start option to create the necessary files (jars, html, jnlp) and then move them to my IIS local server.

SOLUTION

I'm loading the ini file from within the jar now:

Properties Settings = new Properties();
URL url = this.getClass().getResource("/myapplet/settings.ini");
settings.load(url.openStream());

标签: java file applet
2条回答
爷的心禁止访问
2楼-- · 2019-09-18 08:20

At first glance I would expect:

new URL(getCodeBase(), "settings.ini")

as getCodeBase gives the directory URL, getDocumentBase gives the HTML URL. That it worked previously is astonishing. Maybe the HTML URL ended with ?... and you read the HTML page?

查看更多
甜甜的少女心
3楼-- · 2019-09-18 08:25

SOLUTION

I'm loading the ini file from within the jar now:

Properties Settings = new Properties();
URL url = this.getClass().getResource("/myapplet/settings.ini");
settings.load(url.openStream());
查看更多
登录 后发表回答