• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

minAge from a txt file

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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) ?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I love a woman who dresses in stainless steel ... and carries tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic