有时使用下面的代码片段获取NullPointerException的原因是什么。
Exception in thread "main" java.lang.NullPointerException
[java] at java.io.Reader.<init>(Reader.java:78)
[java] at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
[java] at helloworld.HelloWorld.fixture(Unknown Source)
[java] at helloworld.HelloWorld.replaceScheduler(Unknown Source)
[java] at helloworld.HelloWorld.test(Unknown Source)
[java] at helloworld.HelloWorld.main(Unknown Source)
&#13;
我的代码我试图读取该文件扩展名.fscript;问题是getResourceAsStream(name)返回null
以下是所有代码:
public class HelloWorld {
FScriptInterpreter fscript;
Node rootNode;
private Reader fixture(String name) {
InputStream stream = HelloWorld.class.getClassLoader().getResourceAsStream(name);
return new InputStreamReader(stream);
}
private Component getChildByName(Component parent, String name)
throws NoSuchInterfaceException {
ContentController cc = Fractal.getContentController(parent);
for (Component child : cc.getFcSubComponents()) {
if (name.equals(Fractal.getNameController(child).getFcName())) {
return child;
}
}
return null;
}
public void replaceScheduler(Component root) throws Exception {
Component client = getChildByName(root, "client");
fscript.loadDefinitions(fixture("replace-scheduler.fscript"));
fscript.apply("replace-scheduler", new Object[] { rootNode });
Component newScheduler = getChildByName(client, "client1");
assertNotNull("New scheduler not found.", newScheduler);
}
public void test() throws Exception {
Factory factory = FactoryFactory.getFactory(FactoryFactory.FRACTAL_BACKEND);
Component root = (Component) factory.newComponent("helloworld.ClientServerImpl",new HashMap());
Fractal.getLifeCycleController(root).startFc();
((java.lang.Runnable)root.getFcInterface("r")).run();
replaceScheduler(root);
}
public static void main(String[] args) throws Exception {
HelloWorld h=new HelloWorld();
h.test();
}
}
&#13;