1

I am attempting to write a method that has to take in a Writer object and use that to provide output to a file. The current code I have is throwing a NullPointerException, presumably because either there is an error in how I am creating my BufferedWriter or else in some instances w (the Writer object) is being passed as null. I have no control over what is passed as w, and cannot change the exceptions that this method is able to throw.

My code is as follows:

public void write(Writer w, Stat s) throws IOException {
    try{
        BufferedWriter writeFile = new BufferedWriter(w);
        writeFile.write(s.getData());
        writeFile.flush();
    } catch (IOException e){
        ...
    }
}

Is there something I'm doing wrong?

(This assignment arises out of homework, but this question isn't the homework itself)

DTR
  • 372
  • 6
  • 22
  • Have you initialized the `w` and `stat` variables? Does `stat.getData()` actually return data? (I'm assuming `s` was supposed to be `stat` and you just typed it wrong in the question. Is that correct?) – Keith Oct 05 '15 at 18:58
  • Is there an error stacktrace? – Sascha Kolberg Oct 05 '15 at 18:58
  • Add null checks for both the writer and stat objects e. g. If(w ! =null & & s ! = null) after entering the try block –  Oct 05 '15 at 18:58
  • you should debug or add some print statement to see what the values of w, and s are. if these input parameters are null you cant do much about this. other than throw an exception – Marquis Blount Oct 05 '15 at 18:59
  • @Keith sorry, that was a type in creating the question. `stat.getData()` is actually `s.getData()` and yes, it does return data. `w` and `s` are provided in the method call. – DTR Oct 05 '15 at 19:00
  • @vikasn91 Thanks, I was racking my brain to think why `w` could be null, but it turns out that it was actually `s`. Problem solved. – DTR Oct 05 '15 at 19:05

1 Answers1

3

You need both Writer wand Stat s to be not null. Therefore you should reject them if they are null.

public void write(Writer w, Stat s) throws IOException {
   if (w == null)
       throw new IllegalArgumentException("writer is null");
   if (s == null)
       throw new IllegalArgumentException("stats is null");
   ...
wero
  • 32,544
  • 3
  • 59
  • 84
  • You should add that although the `Stat` object may be constructed, its `getData()` may be returning `null`, too. – Keith Oct 05 '15 at 19:02
  • this is double exception checking, ie the NullPointerException is ALSO doing this check. Better IMO if you are going to go this route would be to put try {} catch {} blocks around the lines where you could get the nullpointerexceptions, catch the NPEs, and throw an IllegalArgumentException initialized with the NPE exception – ControlAltDel Oct 05 '15 at 19:05
  • 1
    @Keith I don't think that this belongs into the sanitizing of arguments. `writeFile.write(s.getData());`does not throw a NPE if `s.getData()` returns null. – wero Oct 05 '15 at 19:05
  • Thanks for this, turns out that I was only thinking about the cases where `w` could be null, but it turns out the problem was being caused by `s`. – DTR Oct 05 '15 at 19:06
  • @DTR glad that you found it – wero Oct 05 '15 at 19:07
  • @wero To be pedantic, why throw an `IllegalArgumentException` for a null variable? Effective Java, Item # 60 states to use IAE's for invalid _non-null_ parameters. – Keith Oct 05 '15 at 19:10
  • @Keith this would be worth an own question! Some JDK code *indeed throws* a NPE if a required argument is null. On the other hand a NPE is *almost always* a sign that someone *forgot* to sanitize a argument. So seeing a NPE you simply don't know if it was a bug or a deliberate refusal. Using a `IllegalArgumentException` does not have this indecisiveness. Conclusion: I would change the recommendation of Effective Java#60. – wero Oct 05 '15 at 19:21
  • And it is its own [question](http://stackoverflow.com/questions/3881/illegalargumentexception-or-nullpointerexception-for-a-null-parameter) (with a _lot_ of opinions on the matter) :) – Keith Oct 05 '15 at 19:28
  • @Keith interesting. And the accepted (=official) answer deviates from the high-score (=best practice) answer :-) – wero Oct 05 '15 at 19:31
  • @wero FYI -- `BufferedWriter.write(...)` **does** throw an NPE if `s.getData()` returns null – Keith Oct 05 '15 at 20:30