-2

The following code is from Bates and Sierra:

public class Book {
    private String title; // instance reference variable

    public String getTitle() {
       return title;
    }

    public static void main(String [] args) {
       Book b = new Book();
       String s = b.getTitle(); // Compiles and runs
       String t = s.toLowerCase(); // Runtime Exception!
    }
}

Why does String t = s.toLowerCase() cause a runtime exception when String s = b.getTitle() does not?

Rudi Visser
  • 21,350
  • 5
  • 71
  • 97

6 Answers6

3

It's easy: the b reference to Book is not null, but you never assigned a value to title. It's null.

duffymo
  • 305,152
  • 44
  • 369
  • 561
2

Because title is never given any value. Non-primitive instance fields, like title, default to a value of null. So when you say b.getTitle(), that returns null. Then s is null, and when you try to dereference null, meaning use the . operator on it, you get a NullPointerException. Try:

Book b = new Book(); // b is now a Book object with a null title
b.title = "Programming Java"; // b's title is now a String instead of null
String s = b.getTitle(); // s is now the title that we added to the book
String t = s.toLowerCase(); // t is now the same title in lower case
Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
2

Null references can be passed on as many times as needed, the NPE will occur only when you want to actually do something with the reference. To avoid the NPE, do something like

private String title = ""; // instance reference variable

That way, your title var will not be null when the Book Object is created

A--C
  • 36,351
  • 10
  • 106
  • 92
  • "only when you want to actually do something with the reference" Now I get it! It can be null and referenced unless you want to do something with it in which case an error is generated. Thanks so much! – user1828538 Dec 22 '12 at 22:06
  • If you put your mouse below the numbers on the top left of people's answers, a hollowed out checkmark will be displayed. Click it and it turns green. That's it! – A--C Dec 23 '12 at 01:31
1

new Book() will make a book instance with null title private String title;.

when you try String t = s.toLowerCase(); s is null and hence the result

Subin Sebastian
  • 10,870
  • 3
  • 37
  • 42
0

Objects in java get null as default value, String is an object thus if not initialized it would be null.

Why does String t = s.toLowerCase() cause a runtime exception when String s = b.getTitle() does not?

Well, b is initialized thus it would not throw NPE when invoked its method, however b.getTitle() would return null as title isn't initialized, thus you get NPE when you invoke toLowerCase() on s (which is null).

String s = b.getTitle(); ---> s=null;
String t= s.toLowerCase(); ---> t= null.toLowerCase();
Throws NPE;
PermGenError
  • 45,977
  • 8
  • 87
  • 106
0

b is a valid initialized Book object. However no title is defined. So b.getTitle() returns null. With s.toLowerCase() you try to execute a method on null which causes a NullPointerException.

You can Avoid this by giving title a default value:

private String title = "default title..";
micha
  • 47,774
  • 16
  • 73
  • 80