Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

minAge from a txt file

 
Angel Zaykov
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic