• Post Reply Bookmark Topic Watch Topic
  • New Topic

Nullpointer exception not sure why  RSS feed

 
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys I am getting a null pointer exception and I'm not sure why,I am following a tutorial on udemy the program is basically a way to add contacts to a mobile phone I have followed the code almost word for word but I seem to be getting the error.

Exception in thread "main" java.lang.NullPointerException
at MobilePhone.findContact(MobilePhone.java:29)
at MobilePhone.addContact(MobilePhone.java:12)
at Main.main(Main.java:9)

here is the code

main


contact


mobilePhone


could someone please tell me why I am getting this error,and how would I go about solving this issue,

Thanks





 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the value of myContacts? Where do you create an instance of ArrayList?
 
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By default, instance variables are initialized to null.  So this is null.


Instead, if you initialize it, you have an actual list object:
 
Ranch Hand
Posts: 113
11
Eclipse IDE Java Netbeans IDE Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your ArrayList is never initialized, so it will result in a NullPointerException when you try to call its methods. You have to create an object and assign it to the reference variable.

And I don't see where your findContact(String con) method will return a value lesser than 0. So when you call the addContact method, the if statement will always evaluate to true.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:By default, instance variables are initialized to null.  So this is null.


Instead, if you initialize it, you have an actual list object:



Thanks guys I knew I must have forgot something,that fixed the compiling issue but I still have one issue with the code

when I run it I get the out put of contact is already on file

that output was supposed to show if the contact had been added twice but for example when I create the contact Bill then try to add it,it tells me that it's already on file and returns false I'm not sure why this is happening
 
João Victor Gomes
Ranch Hand
Posts: 113
11
Eclipse IDE Java Netbeans IDE Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:
Jeanne Boyarsky wrote:By default, instance variables are initialized to null.  So this is null.


Instead, if you initialize it, you have an actual list object:



Thanks guys I knew I must have forgot something,that fixed the compiling issue but I still have one issue with the code

when I run it I get the out put of contact is already on file

that output was supposed to show if the contact had been added twice but for example when I create the contact Bill then try to add it,it tells me that it's already on file and returns false I'm not sure why this is happening


I have already answered that. You never return a value lesser than 0 in your findContact(String con) method.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you look for the contact and not find it you return 0.
Then you check whether the value is >= 0 to decide if you should add a contact.
So the contact will never be added.

By the way, for handling duplicates you should use a Set instead of a List.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:When you look for the contact and not find it you return 0.
Then you check whether the value is >= 0 to decide if you should add a contact.
So the contact will never be added.

By the way, for handling duplicates you should use a Set instead of a List.


Thanks guys
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:When you look for the contact and not find it you return 0.
Then you check whether the value is >= 0 to decide if you should add a contact.
So the contact will never be added.

By the way, for handling duplicates you should use a Set instead of a List.


so instead of returning 0 in the findContacts method the better choice would to be return -1 for example?
thanks
 
Sheriff
Posts: 4293
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
-1 would work as it is an illegal value for an index.
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry to disapprove the solved status of this thr‍ead, but there is another problem I can see. Why are you using a List as a phone book? If you try to find a contact by name, you are going to have to do a linear search. There are other forms of data structure which lend themselves far better to a phone book. In fact, with the appropriate data structure you can enter the name and get the phone number without having to do any searching at all. It can link names to phone number objects. Have a look through the appropriate Java™ Tutorials section, and see what you can find.

The data structure I am thinking of will have problems if you have two friends with the same name, however.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!