Is it a bad practice to instantiate private variable in a class to avoid the NullPointerException when using the variable, for example:
public Class MyClass{
private HashMap<String, String> myHashMap = new HashMap<String, String>();
public HashMap<String, String> getMyHashMap (){return myHashMap; }
public HashMap<String, String> setMyHashMap (String myString){ /* treatment to set the myHashMap under some conditions */}
}
If we don't instantiate the myHashMap, the method getMyHashMap() may return null Is it a better practice that the caller of getMyHashMap() checks for the null value? Is there a coding good practice to be applied in those circumstances?