user7085962 user7085962 - 22 days ago 6
Android Question

PutLong on a null Long in argument Bundle

Given a new instance method of a Fragment:

public static MyFragment newInstance(Long someLong) {
Bundle args = new Bundle();
MyFragment fragment = new WorkoutDetailsRoutineInfoFragment();
args.putLong(ARGS_LONG, someLong);
fragment.setArguments(args);
return fragment;
}


If
someLong
is null, this doesn't work.


Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'long java.lang.Long.longValue()' on a null object reference


How do I get it to store the Long even if it's null?

Answer

Check if its null and if its null add -1 if its not null add the Long object. Pretty easy

Comments