1
String str = null;
Date d = Date.valueOf(str);

I am getting the following error when running the above snippet in Anon Apex.

Line: 2, Column: 1
System.NullPointerException: Argument cannot be null.

This is quite strange.

I have a field that contains string data and my target field is of Date Datatype as such I am using Date.ValueOf to convert it.

The quirk here is that the string field may also be null.

My code is failing because Date.ValueOf() does not accept null value as its input parameter.

What I hoped was that upon seeing a null value in the parameter, the Date.ValueOf() will also return a null value.

Can anyone tell me what should I do here ?

What is the null equivalent for a date in Salesforce ?

  • Why not a simple null check before performing your necessary operations – Antonio Manente Apr 8 '16 at 0:59
  • I use the conversion in assignment operation for object initialization..something like this new Obj( Date_Value = Date.ValueOf(String) ); – Aaron Wilfred Apr 8 '16 at 1:04
  • Not sure how can I do a null check in my case..see my prev comment – Aaron Wilfred Apr 8 '16 at 1:04
5

Why not

Date d = String.isBlank(str)
          ? null
          : Date.valueOf(str);  
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.