Week 9 Exercise 26 PhoneBook Filtered Search

116 views
Skip to first unread message

David Henley

unread,
Oct 15, 2015, 4:44:12 PM10/15/15
to mooc.fi
Hi,

I'm having problems with week 9 exercise 26.

It is giving me Null Pointer Exceptions that don't make sense with the input, and I am getting the EXACT console output as the exercise.


Best regards


Supported languages are English and Finnish

Please use our build in TMC Pastebin when asking questions related to your code  
 
TMC Plugin has built-in code sharing feature. 
In NetBeans, select TMC -> Send code to TMC pastebin. Copy the link you receive and paste it to your message.  

You may use our web interface to share code snippets: http://paste.mooc.fi

ljleppan

unread,
Oct 19, 2015, 2:51:17 AM10/19/15
to moo...@googlegroups.com
Hi David,

The NullPointerException is coming from line 103 in the class PhoneBook, which reads as follows:

if (person.getName().contains(keyword) || person.getAddress().contains(keyword)) {

The problem here is that when you create a new Person, you set his/her address as null on line 13 of the Person class.  

Suppose you have a single person in the phone book with only a phone number and no address. 
Now if you call the method filtered(), then it first calls person.getName(), which returns a
String and then calls contains(keyword) on that String. Assuming the String does not 
contain the keyword, we then move onwards to the second part of the if clause. In this part we 
first call person.getAddress(),  which in our case returns null. And then we try to call the method 
contains(keyword) of the null object, which is not allowed and causes the NullPointerException.

EDIT: The formatting was funky, added some linebreaks. Sorry.

-Leo
Reply all
Reply to author
Forward
0 new messages