• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Trouble finding NullPointerException

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to write a program that reads words from a text file and adds the words with a certain character length to a set. But when I try to run it, I get a NullPointerException and I am stuck. Any help would be greatly appreciated.

 
Rancher
Posts: 1043
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A stack trace reveals the line where the Exception was thrown.
 
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

We can only help with more details. For example, where does the Exception occur? Please post the entire stack trace (using copy‑and‑paste).
I can see some design I am not happy with. Your class should have a constructor and all the fields should be initialised in the constructor (or, for static fields, an initialiser). I would put the reading details in a method in the class, not the main method. Then you can shorten the main method to its ideal length: one statement.

Are you really calling the readFile() method from the main method? Does that actually compile? It is necessary for us to see the real code if we are to help.
 
Campbell Ritchie
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is LinkedSet? I can’t find it in the API. You really do have to post the real code, or we can’t help. Please copy and paste it into your next post.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If that's the actual code I'm surprised it gets as far as a NullPointerException. You don't have a valid main method - it isn't static. And if it was static, it wouldn't compile without further changes.
 
You get good luck from rubbing the belly of a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic