I'm using Firebase cloud messaging and one of the parameters is optional. I have following method below
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
String picUrl = null;
String toTel = remoteMessage.getData().get("To").toString();
if (remoteMessage.getData().get("Pic") != null)
picUrl = remoteMessage.getData().get("Pic").toString();
}
However I'm getting an exception at the if statement line
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
How can I successfully check to is there is a value for "Pic"?