2

My code is as follows:

public class BoxingUnboxingExample {
    public static void main(String[] args) {
        Integer i1 = null;
        Integer i3 = 10;
        BoxingUnboxingExample b = new BoxingUnboxingExample();
        b.go(i3);
        b.go(i1);
    }
    private void go(int a){
        System.out.println("a");
    }
}

Now my question is:

Sometimes I get the following error message,

Exception in thread "main" java.lang.NullPointerException
at scjp.wraperExample.BoxingUnboxingExample.main(BoxingUnboxingExample.java:12)
a

Whereas, I think It should always be the following,

a
Exception in thread "main" java.lang.NullPointerException
at scjp.wraperExample.BoxingUnboxingExample.main(BoxingUnboxingExample.java:12)

Am I correct?

bane
  • 811
  • 1
  • 8
  • 23
Ketan Bhavsar
  • 5,338
  • 9
  • 38
  • 69

5 Answers5

2

One reason could be the exception stack trace uses standard error (System.err) to output the error data while System.out.println uses standard output (System.out).

That means both are using different mechanisms to output the data, these may not be properly synchronized.

You can also refer this earlier question.

ex

Community
  • 1
  • 1
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
1

Output and Error are two different streams System.err and System.out.

Read this:

It is acceptable—and normal—for standard output and standard error to be directed to the same destination, such as the text terminal. Messages appear in the same order as the program writes them, unless buffering is involved. (For example, a common situation is when the standard error stream is unbuffered but the standard output stream is line-buffered; in this case, text written to standard error later may appear on the terminal earlier, if the standard output stream's buffer is not yet full.)

from wikipeda

so your output stream (System.out.println) is actually buffered. Internally it calles the BufferedWriter.write() methode. take alook at this: How System.out.println() really works

CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
  • This does not explain why output is like that. – Smit Jan 16 '13 at 06:09
  • Sure, the methode expects an int and its beeing called with a null, an Integer is a reference so it can be null, but an in tis a prempotive type it can not be null. – CloudyMarble Jan 16 '13 at 06:12
  • @MeNoMore: sure, but that's not the question - his question is about the order in which "a" and the exception info are being show, doesn't correspond to the order in which the statements are excecuted. The first call has an int, so it should be displayed first ("a"), it's only after the second call that the exception should be shown. – ilias Jan 16 '13 at 06:14
1

There are at least 2 effects at play here, both related to how System.out and System.err are (naturally) 2 different streams.

Buffering

System.err and System.out are both PrintStreams, which are buffered by default, however System.err is usually set to auto-flush after every write (of a byte array or whenever a newline is written).

As such, if System.err is flushed before System.out is flushed, it will appear on screen first.

The reading of those streams

In editors, it is not uncommon for both System.out and System.err to be displayed within a single console. The way this happens will influence the order in which they are displayed. Eclipse has 2 processes, one for every stream, to read from the streams and display them. The order in which they will read (and as such, display) is non-deterministic. The process that reads from System.err would have a 50% chance of being the first to receive input if both streams are written to quasi-simultanously. As such, they may appear to be randomly interleaved.

Community
  • 1
  • 1
ilias
  • 511
  • 4
  • 7
1

If you are in Eclipse, this is a known problem, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=32205

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0
public class Main {

        public static void main(String[] args) {
            System.setErr(System.out); // this line seems to solve the problem
            System.out.println("test");

            throw new RuntimeException("Test");
        }


}
jhegedus
  • 20,244
  • 16
  • 99
  • 167