可以弄清楚为什么ScriptEngine引擎在代码中为null
这是我正在为MapLestory服务器运行的一些代码。每当应该发生像与NPC交谈之类的事件的脚本时,将运行此脚本以创建所谓的任何脚本(NPC,Portal,Event等)的脚本路径。
我还使用JDK1.7.0_80
lang-java
package scripting;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import client.MapleClient;
import tools.FileoutputUtil;
public abstract class AbstractScriptManager {
private static final ScriptEngineManager sem = new ScriptEngineManager(null);
protected Invocable getInvocable(String path, MapleClient c) {
return getInvocable(path, c, false);
}
protected Invocable getInvocable(String path, MapleClient c, boolean npc) {
FileReader fr = null;
try {
path = "scripts/" + path;
ScriptEngine engine = null;
if (c != null) {
engine = c.getScriptEngine(path);
}
if (engine == null) {
File scriptFile = new File(path);
if (!scriptFile.exists()) {
return null;
}
engine = sem.getEngineByName("JavaScript");
if (c != null) {
c.setScriptEngine(path, engine);
}
fr = new FileReader(scriptFile);
engine.eval(fr);
} else if (c != null && npc) {
c.getPlayer().dropMessage(-1, "You already are talking to this NPC. Use @ea if this is
not intended.");
}
return (Invocable) engine;
} catch (Exception e) {
System.err.println("Error executing script. Path: " + path + "\nException " + e);
FileoutputUtil.log(FileoutputUtil.ScriptEx_Log, "Error executing script. Path: " + path +
"\nException " + e);
return null;
} finally {
try {
if (fr != null) {
fr.close();
}
} catch (IOException ignore) {
}
}
}
}
这是我收到的蝙蝠错误:
错误执行脚本。路径:脚本/事件/someEvent.js异常 java.lang.nullpointerexception:无法调用 “ javax.script.scriptEngine.eval(java.io.reader)”,因为“引擎”为null
每当我尝试与使用此方法的某些东西进行交互时,这些错误就会抛出(即单击NPC或某些脚本时在服务器启动时运行)。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NullPoInterException
发生,因为您使用的Java运行时没有安装的“ JavaScript”脚本引擎,引起sem.getEngineByName(“ JavaScript”)
返回> null
。已经进行了验证(请参阅)通过运行以下代码:
当我在Windows 10上的各种Java实现上运行此代码时,我会得到以下结果:
如您所见,Java 7的OpenJDK版本没有JavaScript引擎,因为Mozilla Rhino图书馆不是开源的。您需要Java 7的Oracle版本才能获取JavaScript引擎。
您还可以看到JavaScript已在Java 15中删除,如 JDK 15发行说明:
The
NullPointerException
occurs because the Java Runtime you're using doesn't have a "JavaScript" Script Engine installed, causingsem.getEngineByName("JavaScript")
to returnnull
.That was verified (see comment) by running the following code:
When I run this on various Java implementations on Windows 10, I get the following results:
As you can see, the OpenJDK version of Java 7 doesn't have a JavaScript engine, because the Mozilla Rhino library isn't open-source. You need the Oracle version of Java 7 to get the JavaScript engine.
You can also see that JavaScript was removed in Java 15, as documented in the JDK 15 Release Notes:
我对JDK 17和
lib rhino.js.enengine-1.7.71.jar
也有同样的问题。我
也添加了我的班级路径。解决了错误。
I had the same problem with JDK 17 and
lib rhino.js.engine-1.7.71.jar
.I added
to my classpath too. That solved the error.
或者,只需直接使用犀牛本身,没有
scriptenginemanager
:pom.xml:
java代码:
Or, just directly use rhino itself, without
ScriptEngineManager
:pom.xml:
java code: