0

I have a query that sometimes returns a value if the current user has a record. If they do not no value is loaded and my vf page does not show a gauge.

Today, I have been tweaking the controller to divide the value by 100,000 so the gauge is easier to read. When I look at the gauge on a page it gives an error that it is trying to de-reference a null object.

I think this is because I'm dividing by a null value. So I want to put in a if to only do the division if there is a value.

Does this seem like the right approach?

Here is my controller class:

public with sharing class ISOGaugeControllerIndividualCurMo {
public Integer getMax2() {
    Decimal max1 = 1;
    Integer max2 = 0;
    AggregateResult max =   [select sum(Goal__c) goal 
                            FROM ISO_Goal__c 
                            Where Goal__c != null 
                            AND (Goal_Date__c = THIS_MONTH OR Goal_Date__c = LAST_MONTH)
                            AND RecordType.Name = 'Individual Goal'
                            AND OwnerId = :UserInfo.getUserId()];

    max1 = (Decimal)max.get('goal');
    if (max1 == 1) {
    max2 = (Integer)max1.divide(100000, 0);
        }
    return max2;
    }
public List<gaugeData> getData() {

    AggregateResult Actuals =   [select sum(amount__c) sum 
                                FROM Actuals__c 
                                Where Amount__c != null 
                                AND (Actual_Date__c = THIS_MONTH OR Actual_Date__c = LAST_MONTH)
                                AND ISO_Goal__r.RecordType.Name = 'Individual Goal'
                                AND ISO_Goal__r.OwnerId = :UserInfo.getUserId()];

    List<gaugeData> data = new List<gaugeData>();
    data.add(new gaugeData('Actual', (Integer)((Decimal)actuals.get('sum')).divide(100000, 0)));
    return data;
}
// Wrapper class
public class gaugeData {

public String name { get; set; }
public Integer size { get; set; }

public gaugeData(String name, Integer data) {
    this.name = name;
    this.size = data;
    }
}   
}
3
  • Instead of using if (max1 == 1) why not just use if max1 != null ? – Morgan Marchese May 19 '16 at 18:08
  • Hey Dan, did you ever resolve this issue? – Adrian Larson May 24 '16 at 15:20
  • Yes, I used Sebastian's example. – Dan Wooding May 24 '16 at 16:45
4

I would do something like this. It's cleaner and achieves your stated goal

max2 = (max1 == null) ? 0 : (Integer)max1.divide(100000, 0);

Also, I would either remove the if statement above or incorporate it into the ternary, like the below

max2 = (max1 == null || max != 1) ? 0 : (Integer)max1.divide(100000, 0);
8
  • Um. You want to send all values except 1 to 0? That doesn't sound right... – Adrian Larson May 19 '16 at 18:19
  • I'm just mimicking the if statement the OP had around the division originally. Hence my two samples, he might have a good reason for doing it. – Sebastian Kessel May 19 '16 at 18:20
  • I don't was just trying to get some value in there so the page renders. – Dan Wooding May 19 '16 at 18:23
  • then the first sample is what you need – Sebastian Kessel May 19 '16 at 18:23
  • Still get dereference error 'public Integer getMax2() { AggregateResult max = [select sum(Goal__c) goal FROM ISO_Goal__c ... Decimal max1 = (Decimal)max.get('goal'); Integer max2 = (max1 == null) ? 0 : (Integer)max1.divide(100000, 0); return max2; }' – Dan Wooding May 19 '16 at 18:29
4

I commonly create a method called elide for this issue:

public Decimal elide(Decimal input) { return (input == null) ? 0 : input; }

With it, you can simply do:

max2 = (Integer)elide(max1).divide(100000, 0);
2
  • 1
    elide - I have not seen this word before in the context of computer science, only in speech. My similar function, clunkier-named is makeOperandable() – cropredy May 19 '16 at 21:45
  • @cropredy tbh, don't remember where I picked that term up from and I'm not entirely sure if it's correct. I've used it for a couple years now, though. – Adrian Larson May 19 '16 at 21:47

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.