This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes My BufferedReader works in constructor, but not beyond it. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "My BufferedReader works in constructor, but not beyond it." Watch "My BufferedReader works in constructor, but not beyond it." New topic
Author

My BufferedReader works in constructor, but not beyond it.

Alan Branch
Greenhorn

Joined: May 06, 2010
Posts: 3
I construct my reader through another classes constructor, thus:


I'm getting the first two char of the file, but when I try to grab the third, I get a null pointer exception.

Any help would be GREATLY appreciated.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14193
    
  20

Welcome to JavaRanch. Please UseCodeTags when you post source code.

About the NullPointerException: Carefully look at the error message, especially the stack trace. It should tell you exactly in which line of the source code the exception happens. Look at that line in your source code and think of what is null there that isn't supposed to be null. You usually get a NullPointerException when you try to call a method on a variable that is null.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
Welcome to the Ranch I have added code tags because you are new (as Jesper mentioned), and you can see how much better the post looks.
Beware of having Readers as fields of the class; they are often better declared as local variables. You are failing to close the Reader; this can potentially lead to resource leaks. You ought to close the Reader in a "finally" block; example here.

Don't use system.out in a catch; use System.err. Your error message is a little too basic; you might do better with e.printStackTrace(), which gives you more details. Beware of the read() method, which only reads one character at a time, and often doesn't provide what you need. If you need lots of characters, you might do well to put your reading into a loop.
Alan Branch
Greenhorn

Joined: May 06, 2010
Posts: 3
Ahhh...Thank you. I'll be sure to add CodeTags in the future.

I found my problem. I had declared my BufferedReader outside of my constructor. Then of course when I declared it again within the constructor, it wiped the original.

Easy fix was to just delete the BufferedReader before readIt within my constructor.

Sooner or later I'm going to stop doing that.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
Alan Branch wrote: . . . Then of course when I declared it again within the constructor, it wiped the original. . . . .
No, it didn't wipe the original. It created two separate references and ignored the original. This is called shadowing.
Alan Branch
Greenhorn

Joined: May 06, 2010
Posts: 3
Ahhh... Thank you. It's important to know the right nomenclature and what exactly is going on.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
You're welcome
 
GeeCON Prague 2014
 
subject: My BufferedReader works in constructor, but not beyond it.