• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException trying to fill array  RSS feed

 
Raven Ramirez
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Heres some snippets from my code:



the variable 'lines' is the amount of lines from a text file which I already looped which is three, already tested it and ouputed the value:
John
May 12, 1991
August 13, 2006

The null thing starts at line 7 so I just want to focus on why that there is an trying to get the name from the file?
Thanks a bunch
 
Joe Areeda
Ranch Hand
Posts: 334
2
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks to me like you are allocating an array to hold object but not allocating the objects.

May I suggest adding before line 7 something like:



Joe
 
Raven Ramirez
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I then get an error stating that there is no suitable constructor found for person(). Not sure what it exactly means. If it helps I'll add a snippet from my Person class with the constructors



This was pre-made by my instructor but I added the constructor at line 27 thinking that's what the error meant but I guess not
 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure about the person constructor. But the date constructor is way off. You cannot pass a Date type to it.
 
Raven Ramirez
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you talking about the constructor in the Person class? Well like I said this class was already made in the text book, my instructor just happened to make it for us.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you write this:

Person[] PersonArray = new Person[lines];

you are basically creating a box that can hold Person objects. Think of it as an empty egg carton.

You now need to create the Person objects to put into the box. To to that, you need to call a Person constructor.

Note: you state: "no suitable constructor found for person()." Did you mean "Person()"? Remember, java is case sensitive, so Person() is NOT THE SAME as person().

You need to call one of the constructors. From what I see, there is one that takes a String and two dates, one that takes a String, and one that takes another Person object.
 
Raven Ramirez
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah my bad, the person() is actually Person(). I just didn't capitalize it while typing.

Ok and I kinda forgot how to call the constructor, I want to call the String because the on with two dates is just complicated. I'be been doing hard coding for like the past month I kinda forgot how to do this lol. So Everything in my first code of snippet is correct and I just need to call the constructor?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe's post shows you how to call the no-arg constructor. However, since there are constructors defined, Java does not provide the no-arg one for you for free. If you create ANY constructor, you have to also create the no-arg one if you then want to use it.

So, to call the constructor that takes a string, just call it and pass it a string:



Looking at the constructor, it seems you should pass in a name, since what you pass in is what then Person.name member variable gets set to. Not sure what you want to pass in as the initial name.

Note/hint: an empty String is still a string...
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!