Well I would like to know your thoughts on this ambiguous behaviour of the function Integer.valueOf().
It throws "System.NullPointerException: Argument cannot be null" when the input parameter is string variable that is been assigned null.
Whereas the same function works fine if the input parameter is integer variable that is been assigned null.
Integer i = null;
System.debug(Integer.valueOf(i));// works fine and print null
String s = null;
System.debug(Integer.valueOf(s));// throws System.NullPointer Exception
4 risposte
Hi Chandra,
valueOf(stringToInteger) expects the value to be non-null, and since null is given in this case, it then throws NullPointerException.A simple fix can be casting null explicitely to Object, so that it uses valueOf(fieldValue) version where null value is handled:
Integer i;
System.debug(Integer.valueOf(i));// works fine and print null
String s;
System.debug(Integer.valueOf((Object)s));// works fine too
Below is reference in context of Java language:
https://stackoverflow.com/questions/3131865/why-does-string-valueofnull-throw-a-nullpointerexception
Please let me know if that helps.
Thanks,
Pooja