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 Objects (Retired) Creating the MVP Comparing Characters

Furkan Demirkan
Furkan Demirkan
7,926 Points

An error at Conference Registration Assistant. What is java.lang.NullPointerException ?

Im honestly pretty much a noob in Java, so I dont even know if Im doing things right... When I tried my code at the "Conference Registration Assistant" challenge, it returned the error "java.lang.NullPointerException (line 6)". Can someone please inform me what my mistake was, and if there are anymore? Thanks in advance

ConferenceRegistrationAssistant.java
import java.io.Console;
public class ConferenceRegistrationAssistant {

  public int getLineFor(String lastName) {
    Console console = System.console();
    lastName = console.readLine("Enter a last Name: ");
    char firstLetter = lastName.charAt(0);
    if(firstLetter <= 'M') {
    System.out.println("line 1");
    } else{
    System.out.println("line 2");
    }

    int line = 0;
    return line;
  }

}

2 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Don't worry about grabbing the value from the user, it is passed in through the method. In my test I am calling the method ConferenceRegistrationAssistant.getLineFor and passing in different lastName values. So use that to check in your code.

You want to return line, so in your if statement, why not set it. Code looks good, you are super close.

Furkan Demirkan
Furkan Demirkan
7,926 Points

Thanks for the prompt response! I just realised what 'int line = 0' was supposed to be used for, now I passed the challenge :) Again, thanks you. I've been trying to get it right for 20 minutes now.

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Frukan;

Great that you made it through the challenge and figured it out. Nice work!

Mind if we spend a minute on the question in your post title, What is java.lang.NullPointerException?

Let's start by looking at the documentation.

public class NullPointerException

extends RuntimeException

Thrown when an application attempts to use null in a case where an object is required. These include, in no specific order:

  1. Calling the instance method of a null object.
  2. Accessing or modifying the field of a null object.
  3. Taking the length of null as if it were an array.
  4. Accessing or modifying the slots of null as if it were an array.
  5. Throwing null as if it were a Throwable value.

Spectacular. What does all that mean, right? Let's look at an example for clarification.

NpeExample.java
public class NpeExample {
        public static void main(String[] args) {
              String s = null;
              int length = s.length();  //  Bingo! NullPointerException Error
        }
}

We declare an object, s, and assign it to a value of null, which basically creates the object and then points it to an unknown piece of data, right? Then in the next line we ask for it's length. There in lies the problem, we can't ask Java to provide a length of an unknown object. The other cases mentioned above are caused by similar situations.

One thing about NPEs to note is that they occur at runtime, so your code can compile completely fine and then... BAM! when you run your code you get errors.

Post back if that doesn't make any sense or you would like further explanation, examples, or discussion.

Happy coding,

Ken

Furkan Demirkan
Furkan Demirkan
7,926 Points

Thanks for the help! I googled the exception before but it all seemed like gibberish to me, but it makes much more sense now :)