I am having a strange issue with the followin syntax:
The following code block doesn’t throw null pointer exception
Map<String, String> requestHeaderMap = FacesContext.getCurrentInstance().getExternalContext().getRequestHeaderMap();
if(!"partial/ajax".equals(requestHeaderMap.get("faces-request")))
{
System.out.println("This works");
}
But this one throws null pointer exception:
FacesContext.getCurrentInstance().getExternalContext().getRequestHeaderMap().get("faces-request").equals("partial/ajax")
I just couldn’t figure out whats wrong here. I don’t see any difference between two call except for readability.
The two do things in different orders. It’s like the difference between
x.equals(y)andy.equals(x). Ifyis null, only the second one of those will throw an NPE. In your case,requestHeaderMap.get("faces-request")is null.This doesn’t throw the exception because *.equals(null) is perfectly fine:
On the other hand, this is trying to dereference null: