getCodeBase()在Java小程序中提供空指针
我创建了一个小程序,用于从服务器上的文件中读取一些信息。我尝试使用以下代码访问该文件:getCodeBase()在Java小程序中提供空指针,java,file,applet,Java,File,Applet,我创建了一个小程序,用于从服务器上的文件中读取一些信息。我尝试使用以下代码访问该文件: Properties Settings = new Properties(); settings.load(new URL(getDocumentBase(), "settings.ini")).openStream()); 突然,第二行给了我一个错误: java.lang.NullPointerException at java.applet.Applet.getDocumentBase(Unknown
Properties Settings = new Properties();
settings.load(new URL(getDocumentBase(), "settings.ini")).openStream());
突然,第二行给了我一个错误:
java.lang.NullPointerException
at java.applet.Applet.getDocumentBase(Unknown Source)
我的小程序已签名,我可以通过本地主机访问它。为什么我不能再使用getDocumentBase
顺便说一句,我正在使用netbeanswebstart选项创建必要的文件(jar、html、jnlp),然后将它们移动到我的IIS本地服务器
解决方案
我现在正在从jar中加载ini文件:
Properties Settings = new Properties();
URL url = this.getClass().getResource("/myapplet/settings.ini");
settings.load(url.openStream());
Properties Settings = new Properties();
URL url = this.getClass().getResource("/myapplet/settings.ini");
settings.load(url.openStream());
乍一看,我希望:
new URL(getCodeBase(), "settings.ini")
当getCodeBase给出目录URL时,getDocumentBase给出HTML URL。
它以前的工作是令人惊讶的。可能HTML URL以?。。。您阅读了HTML页面吗?解决方案
我现在正在从jar中加载ini文件:
Properties Settings = new Properties();
URL url = this.getClass().getResource("/myapplet/settings.ini");
settings.load(url.openStream());
Properties Settings = new Properties();
URL url = this.getClass().getResource("/myapplet/settings.ini");
settings.load(url.openStream());
调用start()中的代码,而不是init()?调用System.out.println(url.getPath())并在java控制台中查看它。可能HTML得到了一个带有代码库的元,或者小程序HTML是错误的。很好,你找到了一个务实的解决方案。我不知道你问为什么它们会起作用是什么意思。这里的一个快速测试显示了applet viewer中的工作,正如我所期望的(我目前没有配置浏览器插件)。在哪个版本的JRE(供应商)中,它没有提供代码库和文档库?HTML已经被删除了吗?HTML的内容是什么?