File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Trouble finding NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Trouble finding NullPointerException" Watch "Trouble finding NullPointerException" New topic

Trouble finding NullPointerException

Jonathan Elens

Joined: Sep 21, 2012
Posts: 1
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.

Ivan Jozsef Balazs

Joined: May 22, 2012
Posts: 970
A stack trace reveals the line where the Exception was thrown.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46334
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

Joined: Oct 13, 2005
Posts: 46334
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.
Matthew Brown

Joined: Apr 06, 2010
Posts: 4543

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.
I agree. Here's the link:
subject: Trouble finding NullPointerException
It's not a secret anymore!