Do check Map is null?

Good day!

I have a function:
public Map<String, Object> getTestMap(MyClass myObject) {
 Map<String, Object> myResultMap = new HashMap<String, Object>();
 myResultMap.put("myKey", myObject.getMyValue());

 return myResultMap;
}

and the following line of code:
Map<String, Object> resultMap = getTestMap(myObject);

Could you tell me whether in the future to check resultMap to nullwhen I want to get values by key?
For example:
Map<String, Object> resultMap = getTestMap(myObject);
if (resultMap != null) {
 MyObject Object = resultMap.myKey;
}
March 23rd 20 at 19:22
2 answers
March 23rd 20 at 19:24
Solution
1. You incorrectly remove a value from a map by key, you should use the get method -> resultMap.get(myKey);
2. Check for null when you access a resultMap is not needed because you create an empty MEPU method getTestMap and never resultMap not null;
3. Checking for null necessary in the method getTestMap because you call myObject.getMyValue() without checking for null, here's a NullPointerException may occur if the method is called with a parameter whose value is null.
4. To declare a final Map resultMap how to protect yourself from the temptation to assign null - jermey commented on March 23rd 20 at 19:27
March 23rd 20 at 19:26
In order to avoid such issues, I recommend all the methods, arguments and fields to mark @NonNull and @Nullable.
All fields and local variables that the logic is not changed - final.
Declare variables as close as possible to the place of their use.

The answer to the question - no, in this case no need to check. But if the method library and is not marked with the annotation - it is necessary.

Even better to take Kotlin and do not suffer:)

Find more questions by tags Java