Skip to main content
Tweeted twitter.com/#!/StackSalesforce/status/354466496082214913
added 60 characters in body
Source Link
Robert Sösemann
  • 38.2k
  • 29
  • 173
  • 508

I am getting a System.NullPointerException: Attempt to de-reference a null object in the last line (the one with the return) of the following code.

  ...
  AggregateResult result = getResult(...);
  Decimal value = (Decimal) result.get(fieldName);
  return (value == null) ? 0 : value;
}

This really sounds strange to me aas I just added this line to prevent Nullreference errors. As this code is - let me called it - hard to debug I need to analyse this in my head and not in the IDE ;)

I can only think of two3 error sources that would throw other errors:

  1. AggregateResult result is null -> Would throw an error in the line above.
  2. fieldName does not exist on on result -> Would also throw an error in the line above
  3. Value can not be casted into a Decimal -> Would show different Exception

Might this be be a side effect of code in the return statement?!

I am getting a System.NullPointerException: Attempt to de-reference a null object in the last line (the one with the return) of the following code.

  ...
  AggregateResult result = getResult(...);
  Decimal value = (Decimal) result.get(fieldName);
  return (value == null) ? 0 : value;
}

This really sounds strange to me a I just added this line to prevent Nullreference errors. As this code is - let me called it - hard to debug I need to analyse this in my head and not in the IDE ;)

I can only think of two error sources that would throw other errors:

  1. AggregateResult result is null -> Would throw an error in the line above.
  2. fieldName does not exist on on result -> Would also throw an error in the line above
  3. Value can not be casted into a Decimal -> Would show different Exception

I am getting a System.NullPointerException: Attempt to de-reference a null object in the last line (the one with the return) of the following code.

  ...
  AggregateResult result = getResult(...);
  Decimal value = (Decimal) result.get(fieldName);
  return (value == null) ? 0 : value;
}

This really sounds strange to me as I added this line to prevent Nullreference errors. As this code is - let me called it - hard to debug I need to analyse this in my head and not in the IDE ;)

I can only think of 3 error sources that would throw other errors:

  1. AggregateResult result is null -> Would throw an error in the line above.
  2. fieldName does not exist on on result -> Would also throw an error in the line above
  3. Value can not be casted into a Decimal -> Would show different Exception

Might this be be a side effect of code in the return statement?!

Source Link
Robert Sösemann
  • 38.2k
  • 29
  • 173
  • 508

How can that code ever produce a NullReference in the last line?

I am getting a System.NullPointerException: Attempt to de-reference a null object in the last line (the one with the return) of the following code.

  ...
  AggregateResult result = getResult(...);
  Decimal value = (Decimal) result.get(fieldName);
  return (value == null) ? 0 : value;
}

This really sounds strange to me a I just added this line to prevent Nullreference errors. As this code is - let me called it - hard to debug I need to analyse this in my head and not in the IDE ;)

I can only think of two error sources that would throw other errors:

  1. AggregateResult result is null -> Would throw an error in the line above.
  2. fieldName does not exist on on result -> Would also throw an error in the line above
  3. Value can not be casted into a Decimal -> Would show different Exception