Example

public class ClassA {

private ClassB b;

public void aMethod () {

getB().useB();
}

public ClassB getB () {

return b;
}
}



Solution
In the above example, there is no opportunity for b to be instantiated. It will be null; a NullPointerException will result if aMethod() is invoked. To fix, instantiate b or create a method to allow for its instantiation.


public class ClassA {

private ClassB b;

public void aMethod () {

b.useB();
}

public ClassB getB () {

return b;
}

public void setB (ClassB b) {

this.b = b;
}
}