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 ?

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

1 Answer 1

5

Why not

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .