Results 1 to 9 of 9
  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default [SOLVED] java.lang.NullPointerException

    :confused:

    Java Code:
    import java.io.*;
    /**
     * This program executes an external program.
     * @author http://www.rgagnon.com/javadetails/java-0014.html - adapted
     *
     */
    public class ExecuteExternalProgram {
    
    
    	public static void main(String[] args) {
    		
    		 try {
    		    [B]  Process p = Runtime.getRuntime().exec
    		      	(System.getenv("C:\\Program Files\\Java\\jdk1.6.0_14\\bin\\jconsole"));
                            p.waitFor();[/B]
    		    }
    		    catch (Exception err) {
    		      err.printStackTrace();
    		    }
    		    
    	}
    }
    Error:
    java.lang.NullPointerException
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at ExecuteExternalProgram.main(ExecuteExternalProgram .java:13)
    Last edited by jon80; 06-14-2009 at 09:09 PM.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    This
    Java Code:
    System.getenv("C:\\Program Files\\Java\\jdk1.6.0_14\\bin\\jconsole")
    is returning null.

    "getenv" is for getting environement variables, and when the requested variable doesn't exist (as
    Java Code:
    "C:\\Program Files\\Java\\jdk1.6.0_14\\bin\\jconsole"
    doesn't) it returns null, and exec throws an NPE when called with null.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Adding more, just try to debug the code and see before asking such questions lol. And also if you have find the solution for your question, if you can put a comment back there. It's much better. :)

  4. #4
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    I thought that this was not named a Rant forum...

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    I don't know where you are seeing any rants, although one is now about to come.

    I have seen multiple questions from you here, and most of them could be solved by simply taking two seconds of actually looking at what you are doing. And for the others you have gotten good advice and seem to take umbrage at it.

    Oh, well, another addition to the ignore list.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  7. #7
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Yes jon... sometimes you post without thinking.

    If you can't solve something, don't just immediately post on the forum. Try to solve it, read API docs, etc. Most of the time it's far better (and sometimes easier) for you to find the problem than it is to have others find it for you.

    Also, Runtime.exec executes system console commands. If you are executing a java program, use java MyApp, or if it is a jar file use java -jar MyJar.jar. For other programs (though yours appears to be a java application) see this thread. It's long and has quite a few errors and some guesswork, but we did get it eventually.
    Last edited by Singing Boyo; 06-17-2009 at 07:13 AM.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  8. #8
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    I leave it up to you to think whatever you like, and without feeling the need to justify myself, it's perfectly normal that people ask for help when they are stuck. I'm not sure why this seems to sound personal, however, I've seen a lot of posts that seek help that do not have "rant" kind of comments looking down on newbies.

    If you have a problem helping other people, it would be just polite to ignore the forum than insulting other members! Why have you setup a forum after all??

    Btw, I do look up the API books etc just for your frigging knowledge!


    Cheers.
    Last edited by jon80; 06-17-2009 at 09:03 AM.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    No here insulting on you jon. What they want to do is helping you in your questions. But not just giving a code segment or a hint in straightly. They(including me) want to guide you to the solution while learning related stuff. That's why we ask from you to search on API, Forum a and web before asking question.

    You ask a question, and within couple of minutes/hours you mark the thread as solved. That means you found the solution before we commenting on it. So what my suggestion is, if you have spend some times on that way, the way you found the solution, it's much useful to you.

Similar Threads

  1. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 3
    Last Post: 02-28-2009, 05:41 AM
  2. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 1
    Last Post: 02-27-2009, 12:36 PM
  3. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 2
    Last Post: 02-27-2009, 10:11 AM
  4. java.lang.NullPointerException
    By stevemcc in forum AWT / Swing
    Replies: 2
    Last Post: 02-08-2008, 09:01 AM
  5. java.lang.NullPointerException
    By ravian in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 07:39 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
  •