1

What can it be the reason to get NullPointerException sometimes with below code piece.

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)

My code i trying to read that file extension .fscript ; the problem is getResourceAsStream(name) is returning null

Here is all code:

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();
    }
}
  • Why did you leave a JavaScript demo for Java code? Did you even test to see that it doesn't run anything? – Tim Biegeleisen Mar 05 '17 at 14:22
  • 2
    It usually happens when the file is not found –  Mar 05 '17 at 14:22
  • `FScriptInterpreter fscript; fscript.loadDefinitions(fixture("replace-scheduler.fscript"));` you are trying to invoke method for fscript, while it's not initialized. What else do you expect from that besides NPE? :D – Malakai Mar 05 '17 at 14:24
  • why are you calling loadDefinitions in fscript. fscript is null, of course this will throw NPE – Yohannes Gebremariam Mar 05 '17 at 14:25
  • It’s a little bit unfair to mark this as a duplicate since the answers to the other question are mostly concerned with NPE in one‘s own code, not so much with NPE inside standard Java code. The [API doc of `getResourceAsStream()`](http://docs.oracle.com/javase/8/docs/api/java/lang/ClassLoader.html#getResourceAsStream-java.lang.String-) says “An input stream for reading the resource, or null if the resource could not be found”. So since this happens in the line where you do `new InputStreamReader(stream)`, a guess is you’re passing a `null` from `getResourceAsStream()` on to the constructor. – Ole V.V. Mar 05 '17 at 15:04

0 Answers0