Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: Exception in thread "main" java.lang.NullPointerException

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    57
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Exception in thread "main" java.lang.NullPointerException

    Hi guys, my first post over here, I hope somebody can help me, thanks in advantage and sorry for my English.

    I'm creating this simple program that read the information from a cmd variable and I want to add some If and Else (If this contains the "x" string, then do this, else, ....)

    This is my code:

    import java.io.*; 
     
    public class ugh 
    { 
    	public static void main(String args[]) throws IOException, InterruptedException 
    	{ 
    		String line;
    		CharSequence look = "108";
     
     
    		Process p=Runtime.getRuntime().exec("cmdkey /list"); 
    		p.waitFor(); 
    		BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream())); 
    		line=reader.readLine(); 
     
    		while(line!=null) 
    		{ 
    			line=reader.readLine(); 
     
    			if(line.contains(look))
    			{
     
    				System.out.println("Yes");
    			}
     
    		}	
     
    	} 
     
     
     
    }

    I get the expected behavior but followed by this error:

    Yes
    Exception in thread "main" java.lang.NullPointerException
    at ugh.main(ugh.java:24)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Exception in thread "main" java.lang.NullPointerException
    at ugh.main(ugh.java:24)
    There is a variable with a null value when the code on line 24 is executed. Look at that line, find the variable with the null value and then backtrack to find out why that variable does not have a valid value.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2013
    Posts
    57
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    The only variable with a null value is "While" and in the line 24 this is what is located "if(line.contains(look))". And in the matter of the null value of "while" I have no idea what to insert over there since if I change that value the whole code is not going to work. Any recommendations?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    only variable with a null value is "While"
    while is a keyword. You shouldn't use that name for a variable.

    I don't see any variable named While in the posted code?

    Look at the code on line 24 again and find the name of the variable that has the null value.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jan 2013
    Posts
    57
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Thanks you very much for your fast response, but I am still lost, in line 24 if I am not mistaken the variable does not have a null value, sirry for my ignorance I'm just learning.

    --- Update ---

    Well, in the While statement I assigned a null value to the variable called "line" ("while(line!=null)") but if I change that value I still get the same error in line 24, where "the problem" is at, and I have played with the code trying to find a solution and I go no where...

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    To fix your problem, you need to understand what variable is null and how it got the null value.
    What variable has the null value on line 24? How did that variable get the null value?

    When you can answer those questions, then work on solving the problem.


    in the While statement I assigned a null value to the variable called "line"
    Not correct. The while statement tests if the variable line does not have a null value.
    line=reader.readLine();
    The above statement assigns a value to line
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    JosPhantasmE (January 18th, 2013)

  8. #7
    Member
    Join Date
    Jan 2013
    Posts
    57
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Perfect, thank you, that solved my problem, I just deleted the "line=reader.readLine();" from while and is not giving me the error, but it start printing "yes" infinite times. Any ideas?

    --- Update ---

    But since I deleted that command/line(?) The if is not working, is like the logic when nuts.

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    start printing "yes" infinite times
    There is a loop that is not ending. What condition controls the looping? When will that condition be false?
    There must be some code inside the loop that changes the condition so it is false.

    The normal way to read a line and test if it is null:
    while((rec = ref.readLine()) != null) {
       //  here rec has a  non-null value
    The expression inside the inner () returns what is in the rec variable as its value and that is then tested with the != null compare.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Member
    Join Date
    Jan 2013
    Posts
    57
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    That's all my code... I know is a mess, that's why I'm here, so I can learn and fix it.

    So you understand better:

    The purpose of this Java Program is to execute the "cmdkey /list" command (which check all the credentials saved in a computer) and then read the output of the command and check if certain credentials are present or not by using this: if(line.contains(look)) and then if not I'm creating an "else" that will add them.

    I haven't move to the "else" part because I want to get this right, I don't want my app to keep running (looping)>

    Just check, if yes then okay and close(end); else add the credentials and close(end)

    --- Update ---

    Thanks.

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Is the code working now?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Member
    Join Date
    Jan 2013
    Posts
    57
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    No it is not working property. This is what I have so far:

    //By José D. Hernández, 2013.
     
    import java.io.*; 
     
    public class ugh 
    { 
    	public static void main(String args[]) throws IOException, InterruptedException 
    	{ 
    		String line;
    		CharSequence look = "127.0.0.0";
     
     
    		Process p=Runtime.getRuntime().exec("cmdkey /list"); 
    		p.waitFor(); 
    		BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream())); 
    		line=reader.toString();
     
    		while(line!=null) 
    		{ 
    			line=reader.readLine();
     
    			if(line.contains(look))
     
    				System.out.println("Yes");
    				//p=Runtime.getRuntime().exec("C:\\Users\\user\\user.rdp");
     
    			else
    				break
    				;
    		}	
    	} 
    }

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    See my post #8 on how to use a while() when reading.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    Member
    Join Date
    Jan 2013
    Posts
    57
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    I fixed the problem, thank you very much:

    This is my actual code:

    //By José D. Hernández, 2013.
     
    import java.io.*; 
    import java.util.Scanner;
     
    public class ugh 
    { 
    	public static void main(String args[]) throws IOException, InterruptedException 
    	{ 
    		String line, usern, passw;
    		CharSequence look = "127.0.0.1";
     
    		Scanner scan = new Scanner (System.in);
     
    		Process p=Runtime.getRuntime().exec("cmdkey /list"); 
    		p.waitFor(); 
    		BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream())); 
    		line=reader.toString();
     
    		while(line!=null) 
    		{ 
     
    			if(line.contains(look)) 
     
    				p=Runtime.getRuntime().exec("mstsc.exe C:\\Windows\\test.rdp");
     
    			else;
     
    			line=reader.readLine();
     
     
     
    		}
     
    	}
    }

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    What is the
    else;
    for?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #15
    Member
    Join Date
    Jan 2013
    Posts
    57
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Not yet finished, I'll post the complete version here.

Similar Threads

  1. Replies: 5
    Last Post: December 9th, 2012, 02:25 PM
  2. Exception in thread "main" java.lang.NullPointerException
    By SoooConfused in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 25th, 2012, 09:06 PM
  3. Exception in thread "main" java.lang.NullPointerException problem.......
    By Adam802 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 20th, 2012, 02:23 AM
  4. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  5. Exception in thread "main" java.lang.NullPointerException
    By manzili in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 9th, 2011, 12:02 PM