• Post Reply Bookmark Topic Watch Topic
  • New Topic

Initializing a class with no variables  RSS feed

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm getting a NullPointerException run I run my code. The data being passed isn't null. The only thing itself that is would be the ContactFileManager object. Since that class has no variables, I'm wondering how can I get past the exception.








 
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you provide the error message, or better yet, the full stacktrace?
 
Matthean Brown
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exception in thread "main" java.lang.NullPointerException
at brownm23.ContactBook.save(ContactBook.java:40)
at brownm23.ContactApp.main(ContactApp.java:97)
Java Result: 1
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you've posted didn't produce those errors.
 
Matthean Brown
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add in contacts and then try to write them to a file. I forget to mention what I was trying to do.
 
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Knute is saying is that the code you posted and the stacktrace to provided do not match. Can you provide a the code with a matching stacktrace?
 
Matthean Brown
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Odd, I thought I might have removed some code that I posted which might change some numbers, but it looks the same as I'm running, and I get the same error message. It trips up for me here:



If it makes it past there, then I am confused.
 
Bartender
Posts: 1838
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess Knute was pointing out that the stack trace indicated the error message coming from:

However, the save method in the code you have posted shows up as lines 8-21 and line 40 is between the load and add methods.

But yes, you have identified the line which the null pointer exception occurs on.
Now, what on this line is null? fileMan, fileName or entries?
What change can you do to your code to make it a non null value?

 
Matthean Brown
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically head desked when I read the question about making it not null. It now goes through the process of writing the objects to a file, but I can't find the file afterwards.
 
Stefan Evans
Bartender
Posts: 1838
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somehow I thought that would be the case. Sometimes you get too close to the code, and a fresh pair of eyes is needed to spot the bleeding obvious.

With regards to finding that file, I would recommend adding the following to help you locate it:




Also, don't forget to flush and close your streams.



 
Matthean Brown
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the write to work.



In terms of the read, it outputs this: "brownm23.Contact@2fc14f68" which is the project and contact class name along with some other stuff. readContacts returns an array.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Careful examination of the Object#toString() method shows that is what it returns. If you want something more comprehensible, you will have to override the toString method in your class.
 
Matthean Brown
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I went and cleaned up more than I thought I should have since the base of this was from a previous assignment, but it seems to be working fine now. Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!