Empty flag Error Mode
In my last article, I explained how the use of NULL pointers in place of different basic types of data is one of the most common causes of nullpointerexception anomalies. This time, I will explain how an empty pointer instead of an exception can cause problems. In Java programs, exception situations are typically handled by throwing exceptions and capturing them at the appropriate control point. But the often-seen approach is to indicate this by returning a null pointer value (and, possibly, printing a message to System.err). If the calling method does not explicitly check the null pointer, it may attempt to discard the return value and trigger a null-pointer exception.
You might guess that this pattern is called an empty flag error pattern because it is inconsistent with a null pointer as a sign of an exception condition.
Cause
Let's consider the following simple bridge class (from Bufferedreaders to iterators):
import java.io.BufferedReader;
import java.io.InputStreamReader;
Import java.io.IOException;
Import Java.util.Iterator;
public class Bufferedreaderiterator implements iterator {
Private bufferedreader internal;
Public Bufferedreaderiterator (BufferedReader _internal) {
this.internal = _internal;
Public boolean Hasnext () {
try {
Boolean result = true;
Let's suppose that lines in the underlying input stream are known
//To be no greater than-characters br> Internal.mark (80);
if (this.next () = = null) {
result = false;
}
Internal.reset ();
return result;
}
catch (IOException e) {
System.err.println (e.tostring ());
return false;
}
}
Public Object next () {
try {
return internal.readline ();
catch (IOException e) {
System.err.println (e.tostring ());
Return null;
}
}
public void Remove () {
//This iterator does not support the remove operation.
throw new Unsupportedoperationexception ();
}
}