• 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

My BufferedReader works in constructor, but not beyond it.

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahhh... Thank you. It's important to know the right nomenclature and what exactly is going on.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic