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 8 of 8

Thread: NullPointerException...how?

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default NullPointerException...how?

    I bet this will be an easy diagnosis but I can't seem to find why I am getting a NullPointerException. I used to make things like this all the time so it's extra weird. Any help will be appreciated.

    import java.util.Scanner;
     
    public class Main {
     
    private static Scanner input = new Scanner(System.in);
    private static String cmd;
     
    	public static void main(String[] args){
    		while(true/*!cmd.equals("quit")*/){
    			cmd.equals(input.nextLine());
    			processCommand();
    		}
     
    	}
     
    	//processes commands. Just testing right here ATM
    	public static void processCommand(){
    		if(cmd.equals("hi")){
    			System.out.println("boo");
    		}
    	}

  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: NullPointerException...how?

    Looks like there was an attempt to access a variable that has a null value.
    What line did the error message report? What variable(s) are on that line? Which one(s) have a null value?

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException...how?

    Exception in thread "main" java.lang.NullPointerException
    at myname.youllmakefunofmeifyouseethisnamesoIwortethi sinstead.Main.main(Main.java:19)

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: NullPointerException...how?

    The error message tells you the NPE occurs on line 19. So look at the variables on that line to find out which one is null. Then fix it. On the previous line add one or more print statements to print out all variables to find out which one is null.
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException...how?

    Line 19 is the cmd.equals(input.nextLine()); So I should set it as something useless then?

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: NullPointerException...how?

    Huh?
    Improving the world one idiot at a time!

  7. #7
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    My Mood
    Fine
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException...how?

    **removed**
    Last edited by jps; August 13th, 2013 at 02:01 AM. Reason: spoonfeeding

  8. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: NullPointerException...how?

    Quote Originally Posted by illusionust View Post
    Line 19 is the cmd.equals(input.nextLine()); So I should set it as something useless then?
    Note that this line does not give cmd a value, but attempts to call a method on the variable cmd

    @divcret Please see The problem with spoonfeeding

Similar Threads

  1. NullPointerException
    By deathmatex in forum Exceptions
    Replies: 4
    Last Post: March 27th, 2012, 03:54 AM
  2. [SOLVED] NullPointerException
    By macko in forum What's Wrong With My Code?
    Replies: 14
    Last Post: June 21st, 2011, 11:35 AM
  3. [SOLVED] Nullpointerexception
    By kbwalker87 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 14th, 2010, 10:33 PM
  4. [SOLVED] NullPointerException
    By javapenguin in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 1st, 2010, 12:10 AM
  5. NullPointerException
    By bbr201 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 29th, 2010, 07:06 PM