Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

java.lang.nullPointerException

import java.io.Console;

public class ConferenceRegistrationAssistant {

  public int getLineFor(String lastName) {
    /* If the last name is between A thru M send them to line 1
       Otherwise send them to line 2 */
    int line = 0;
    Console console = System.console();
    String attendeeLastName = console.readLine(" Enter your last name:  ");
    lastName = attendeeLastName;
    char letter = lastName.charAt(0);
    if ( letter >= 'A' && letter <= 'M' ){ line = 1;}
    else { line =2;}
    return line;
  }
}

3 Answers

Hi there,

As Grigorij said, there's some additional code in there that's not required in the challenge. You don't need user input as the lastName is provided as a parameter meaning you won't need the console object. That's possibly what's throwing the NullPointer as the tests behind the challenge aren't passing in user input so your string will never be initialised; that's not important ...

The points Grigorij made are correct as to the testing of the first letter; just by way of presenting the same information in a different manner, perhaps have a read through this post, as I wrote a long explanation for this.

There's no harm in testing for the initial letter being greater than 'A' and less than 'M'; it's just unnecessary as all letters are greater than or equal to 'A'. We must assume that lastName is validated before being passed to the method!

I hope that helps a little.

Steve.

Hi Mayank,

if you are struggling with a challenge then you don“t need to create a new Console object. Scip this lines:

Console console = System.console(); 
String attendeeLastName = console.readLine("Enter your last name: "); 
lastName = attendeeLastName; 

Your method accepts the input of the user already. This input is stored inside the String "lastName".

And you need to change your if condition to:

if ( letter <= 'M' ){ 
...
}

So everething under M is stored in one line and the rest goes into the other. If you use:

if ( letter >= 'A' && letter <= 'M' ){ 
...
} 

you wil always return the same line because every character a index 0 is bigger or equals then A.

Makes sense?

Grigorij

.

I refactored your code a bit and it seems to work as expected, not sure if this is what you were looking for.

import java.io.Console;

public class ConferenceRegistrationAssistant {
    static Console console = System.console();
    private static String attendeeLastName;
    private static char letter;
    private static int line;

    public static void main(String[] args) {
        getLineFor();
        System.out.println("Person with the last name of: " + attendeeLastName.toLowerCase() + ". Was sent to line: " + line); //In the println statement the user's input will be shown in lowercase and not caps.
    }

    public static int getLineFor() { /* If the last name is between A thru M send them to line 1 Otherwise send them to line 2 */ 
        attendeeLastName = console.readLine("What is your last name?: ");
        attendeeLastName = attendeeLastName.toUpperCase(); //Now the casing doesn't matter for the user input, it will always be in caps, when assigned to 'letter'.
        letter = attendeeLastName.charAt(0);               //Otherwise, if a user typed in any last name that starts with a lowercase 'm', would be sent to line 2.
                                                           //Because in your if condition, you're checking for a last name that starts with an uppercase 'M'.

        if(letter <= 'M') { //All you need to do for your if condition is check for a letter that is less than or equal to M, which will automatically cover (A - M).
            line = 1;       
            return line;
        } else { //Any last names that start with (N - Z) will be automatically assigned to line 2.
            line = 2;
            return line;
        }
    }
}

Let me know if you have any other questions.

Cool :thumbsup:

Thanks :)

Not sure if my code was down voted, but, in any case this thread was just posted on the forums, not under any code challenge so I wasn't aware that this was part of that. Just thought it was his own code that wasn't working so I tried to make it work as specified in his comment while getting rid of some redundant code.

I can't see that your answer has been down-voted; it has a score of 1 at the moment. What do you see?

It's at 0 for me. Oh i just tried closing and reopened that fixed it my bad. Looked as if it was downvoted.

No worries :+1: