wood burning stoves
The moose likes Beginning Java and the fly likes minAge from a txt file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "minAge from a txt file" Watch "minAge from a txt file" New topic

minAge from a txt file

Angel Zaykov

Joined: Jan 20, 2013
Posts: 1
Hello everyone ! I will be very glad if you can help me with the solution for my problem. My program reads from a txt file. After it opens the file I should write a method for the person with the min age from the file.

And the output is :

Name: Bhreac BLACK Age: 34
Name: Moibeal WALLACE Age: 7
Name: Morogh GORDON Age: 80
Name: Boyd JOHNSTON Age: 16
Name: Crissy FERGUSON Age: 27
Name: Daileass ANDERSON Age: 45
the min age is
Exception in thread "main" java.lang.NullPointerException
at Collection.minAge(Collection.java:44)
at Collection.main(Collection.java:58)

any suggestions why my method for the min Age is wrong (think) ?
Steve Luke

Joined: Jan 28, 2003
Posts: 4181

Hi, Welcome to the Ranch!

First, let me point you to this wiki page: UseCodeTags (<- click). They make your code much easier to read (as you can see, I took the liberty of adding the tags for you).

Your problem is that you have an ArrayList<Person> named people that you try to use on line 44. The people ArrayList is never initialized to an actual List. You have a method which is supposed to do it but you never call that method. You need to make sure that list is created before you try to use it.

Later on you will get into trouble because you never actually put anything into the list, so even though you read the Person objects from the file, and when you make the ArrayList, the Person objects won't be in the list. You need to add them to the list in your readFromFile method so you can access them later on.

jQuery in Action, 3rd edition
subject: minAge from a txt file
jQuery in Action, 3rd edition