我试图使PrintStream每次调用其方法时什么都不做。该代码显然没有错误,但是当我尝试使用它时,我得到了一个java.lang.NullPointerException: Null output stream. 我究竟做错了什么?
public class DoNothingPrintStream extends PrintStream {
public static final DoNothingPrintStream doNothingPrintStream = new DoNothingPrintStream();
private static final OutputStream support = new OutputStream() {
public void write(int b) {}
};
// ======================================================
// TODO | Constructor
/** Creates a new {@link DoNothingPrintStream}.
*
*/
private DoNothingPrintStream() {
super( support );
if( support == null )
System.out.println("DoNothingStream has null support");
}
}
Run Code Online (Sandbox Code Playgroud)