• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Values are assigning null

 
Miryanda Johnson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello! I'm hoping someone can help me demystify why the values are returning null. I know this has to be rather obvious but I'm not seeing it. Any help is appreciated.

I believe the issue is occuring under the addContact() method when the values are being assigned. I'm using an ArrayList to send all the information to the constructor, but whatever I'm doing is either overrode or returning a null value.



 
Randall Twede
Ranch Hand
Posts: 4439
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm in no condition to figure this out right now, but i just wanted to say. null pointer exceptions always seem obvious after you have solved them, but before that they can drive you crazy.
 
Miryanda Johnson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was able to get the values to stop assigning null values to the variables, by chagning the constructor:



However, whenever calling addContact() twice, the values are being overridden.




I want it so row one is all displaying whatever is entered, then row two displays whatever was entered the second time.

Any help?
 
Randall Twede
Ranch Hand
Posts: 4439
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
good job fixing the constructor i didn't notice you were doing it backwards(wrong). someone else will have to help with the other problem. i don't know the Scanner class and as i implied i am kind of drunk.
 
Miryanda Johnson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All my variables were declared as static. That was causing the overriding I was seeing
 
Randall Twede
Ranch Hand
Posts: 4439
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
good job Miryanda! static variables are the exception not the norm. most of the time you want an instance variable.
 
Randall Twede
Ranch Hand
Posts: 4439
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and welcome to the ranch. and don't worry if you question seems obvious or stupid. you should see some of the questions i have asked here.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lots of books show you static fields throughout, but that is not usually good practice.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic