Results 1 to 13 of 13
  1. #1
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default Nullpointer exception, even tho I can print data from the file!

    Hai!
    Heres my code:
    PHP Code:
    ScriptEngineManager factory = new  ScriptEngineManager();
            ScriptEngine engine = factory.getEngineByName("py");
            try{
                FileReader file = new FileReader("C:\\Users\\Andreas\\Desktop\\t.py");
    
                System.out.println(file);
                engine.eval(file);
                file.close();
            } catch (Exception e){
                e.printStackTrace();
            }
    Now I get nullpoint exception at the engine.eval part, Even tho I can print the file, assuring that it is NOT null!:eek:

    I have no clue whats wrong, do you?

  2. #2
    yanivby is offline Member
    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0

    Default

    Hi Addez,
    My quick guess is that engine itself is null. But if you post the complete stack trace it will be easier to troubleshoot.
    Thanks,
    yanivby

  3. #3
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    Your quick guess is the only possibillity.
    I die a little on the inside...
    Every time I get shot.

  4. #4
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    How can it be fixed?
    I got python installed and all..

  5. #5
    yanivby is offline Member
    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0

    Default

    Not quite - it's not obvious from his description whether the top of the stack is engine.eval. It's possible that the NPE was thrown within that method. Which is why I asked to see the trace.

  6. #6
    yanivby is offline Member
    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0

    Default

    Addez - the first step of fixing this would be to diagnose the problem. Posting the stack trace would help me do that.
    Thanks,
    yanivby

  7. #7
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    The nullpointerException is thrown at the line:
    engine.eval(file)

    Then it says no more..

  8. #8
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    6

    Default

    Null pointer exceptions are unchecked (the documentation will never say that a method can throw it). So it could be thrown from one of the methods you call.

    Can you add ex.printStackTrace() to the section of code where you check the NullPointerException and then run the program again? That should help figure out what's going on.

  9. #9
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    I suppose that if he says the NullPointerException was thrown in that specific line, he's correct. But, of course, you can't be sure.

    Before that line, add
    Java Code:
    System.out.println("Engine: "+engine);
    I die a little on the inside...
    Every time I get shot.

  10. #10
    yanivby is offline Member
    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by Addez View Post
    The nullpointerException is thrown at the line:
    engine.eval(file)

    Then it says no more..
    Addez, as RaIdEn noted, it would still be very helpful to see the ENTIRE stack trace of the exception. Please copy and paste it into your reply and it will make the people in this forum smarter about your problem.
    Now, if indeed the top of the stack trace is engine.eval(file), then the only possibility is that engine is null. If that's the case, check the documentation of ScriptEngineManager - does it say when it might return null for getEngineByName()? A probable guess is that you have missed a few configuration steps.
    If you post the name of the script engine framework you are using I'll try to help you with that.
    But again, you first need to verify whether engine is indeed null.
    Last edited by yanivby; 01-04-2010 at 10:26 AM.

  11. #11
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by yanivby View Post
    Addez, as RaIdEn noted, it would still be very helpful to see the ENTIRE stack trace of the exception. Please copy and paste it into your reply and it will make the people in this forum smarter about your problem.
    Now, if indeed the top of the stack trace is engine.eval(file), then the only possibility is that engine is null. If that's the case, check the documentation of ScriptEngineManager - does it say when it might return null for getEngineByName()? A probable guess is that you have missed a few configuration steps.
    If you post the name of the script engine framework you are using I'll try to help you with that.
    But again, you first need to verify whether engine is indeed null.
    ++

    Simple if (engine == null) statements will help confirm that easily.

  12. #12
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    Heres the whole output:
    Java Code:
    null
    java.lang.NullPointerException
            at testingplace.Main.main(Main.java:41)
    line 41 is that line I told u about earlier.

    Now I'v tried to fix this by doing this:

    PHP Code:
    List<String> list = new wut().getExtensions();
    for (int x = 0;x<list.size();x++){
      System.out.println(list.get(x));
    }
    
    class wut implements ScriptEngineFactory{
        public String getEngineName() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public String getEngineVersion() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public List<String> getExtensions() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public List<String> getMimeTypes() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public List<String> getNames() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public String getLanguageName() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public String getLanguageVersion() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public Object getParameter(String key) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public String getMethodCallSyntax(String obj, String m, String... args) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public String getOutputStatement(String toDisplay) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public String getProgram(String... statements) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
        public ScriptEngine getScriptEngine() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }
    now the wut class implements (Netbeans did this for me) all the abstract methods that the interface got.
    But I have no clue how I am suppose to use "getExtensions()" without implementing it. And unless I implement it, Im not allowed to use it.

    I need to use the getExtensions to know what extentions it finds. But how do I do that?

  13. #13
    yanivby is offline Member
    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0

    Default

    Hi Addez,
    No, implementing the interface on your own won't get you far in solving this.
    Here's a link to a great and thorough article about Java scripting. It should have everything you'll need to get started, and possibly fix this issue. They also have an example program that prints the available scripting languages.

    java.sun.com/developer/technicalArticles/J2SE/Desktop/scripting

    In addition, the following link explains how to use Python in the Java Scripting Engine:

    alittlemadness.com/2008/07/15/java-6-using-python-via-the-new-scripting-engine/

    Haven't tried it on your own, but it might work for you.

    Hope this helps!
    yanivby

Similar Threads

  1. NullPointer.exception in main (arrays)
    By Jana in forum New To Java
    Replies: 5
    Last Post: 02-20-2009, 07:41 PM
  2. nullpointer exception in jsp
    By fiero in forum JavaServer Pages (JSP) and JSTL
    Replies: 6
    Last Post: 11-07-2008, 02:44 PM
  3. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 12:04 AM
  4. NullPointer Exception
    By Preethi in forum New To Java
    Replies: 8
    Last Post: 02-06-2008, 04:40 PM
  5. Print a picture file
    By oli001 in forum New To Java
    Replies: 0
    Last Post: 11-26-2007, 02:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •