0

I'm getting this error, can anyone help? I've tried wrapping the code in an if clause to prevent the error but it's not helping. The error is on QWeekly1K:

declaring variables before the method:

public List<AggregateResult> totalWeekly1AggK {get;set;}
public List<Daily_Forecast__c> Weekly1ListK {get;set;}
public object totalWeekly1K {get;set;}
public String sWeekly1K {get;set;}
public Decimal QWeekly1K {get;set;}

public void showAll(){
        //get today's DAY to parse daily forecasts in table
        todayDay = DateTime.now().format('EEEE');     

totalWeekly1AggK = new List<AggregateResult>([SELECT SUM(Forecast_Amount__c) 
            FROM Daily_Forecast__c WHERE Date__c = THIS_WEEK AND Day__c = 'Monday' AND User__r.Team__c = 'KCM' AND RecordType.Name = 'Weekly Forecast' GROUP BY Date__c]);
        if(totalWeekly1AggK.size()>0){
            totalWeekly1K = totalWeekly1AggK[0].get('expr0');
            if (totalWeekly1K == null) {
               totalWeekly1K = 0;
            }
    if (integer.valueOf(totalWeekly1K) > 0) {
        QWeekly1K = integer.valueOf(totalWeekly1K);
    }

    if (QWeekly1K != Null) {
        if (totalWeekly1K == 0) {
            QWeekly1K=0;
        }

        if (integer.valueOf(totalWeekly1K) == 0) {
            QWeekly1K = 0;
            sWeekly1K='0';
        }
    }

    if (QWeekly1K == NULL ){
        QWeekly1K=0;
    }

    if (Test.isRunningTest()) {
        newTotalK = 1;
        QTotalK=1;
    }

    if (QWeekly1K==NULL) {
        newTotalK=QTotalK;
    }

    if (QWeekly1K > 0) {
        if (todayDay == 'Monday' && QWeekly1K != NULL) {
            newTotalK = QWeekly1K + QTotalK;
        }

        sWeekly1K = String.format(QWeekly1K.format(), argsTotal);
    }
}

EDIT: Today is Tuesday so the error is in a new place, I've debugged and QTuesdayK is not null or 0, I don't understand the error

TuesdayListK = new List<Daily_Forecast__c>();
                    for(Daily_Forecast__c e :TuesdayList){
                        if(e.User__r.Team__c == 'KCM'){
                        TuesdayListK.add(e);}} 
    system.debug('TuesdayListK '+TuesdayListK);
    if(TuesdayListK.size()>0){        
        totalTuesdayAggK = new List<AggregateResult>([SELECT SUM(Forecast_Amount__c) 
                FROM Daily_Forecast__c WHERE Date__c = THIS_WEEK AND Day__c = 'Tuesday' AND User__r.Team__c = 'KCM' AND RecordType.Name = 'Daily Forecast' GROUP BY Date__c]);
            if(totalTuesdayAggK.size()>0){
            totalTuesdayK = totalTuesdayAggK[0].get('expr0');}
            if(integer.valueOf(totalTuesdayK)>0){
            QTuesdayK = integer.valueOf(totalTuesdayK);}
               if(integer.valueOf(totalTuesdayK)==0){
                    QTuesdayK=0;
                    sTuesdayK='0';
                }
        system.debug('totalTuesdayK '+totalTuesdayK);
            system.debug('QTuesdayK '+QTuesdayK);
            system.debug('sTuesdayK '+sTuesdayK);
            system.debug('newTotalK '+newTotalK);
            system.debug('QTotalK '+QTotalK);
            if(QTuesdayK>0){
                if(todayDay == 'Tuesday'){
                    newTotalK = 
                        QTuesdayK +   //this is the line that throws the error
                        QTotalK;}
                sTuesdayK = String.format(QTuesdayK.format(), argsTotal);}

    }         
     if(TuesdayListK.size()==0){sTuesdayK =NULL;}

debugLog

  • 1
    is totalWeekly1K null? can you put a system.debug in there? Integer.valueOf(totalWeekly1K) > 0 will throw an error if it's null. Also where are you declaring QWeekly1K? You are also not getting your aggregate result correct – EricSSH Apr 23 '18 at 17:20
  • What is the correct way to get the aggregate result? And if I set totalWeekly1K=0 why doesn't that resolve the error? – Jean Grey Apr 23 '18 at 19:10
  • what are you trying to aggregate? – EricSSH Apr 23 '18 at 19:31
  • turn developermode on for the running user and it will tell you the line number – cropredy Apr 23 '18 at 21:15
  • I'm trying to return the Sum of all forecasts for the given day. Today is tuesday so the error is in a different place in the code, I appended the original post above. – Jean Grey Apr 24 '18 at 14:49
2

I think you're wrong when you say the error is on QWeekly1K

That error is provided by several of the built in methods that apex provides. It most likely is caused by something like:

integer.valueOf(totalWeekly1K);

Where the value of totalWeekly1k is null.

Another common location is when trying to use math operators with null values:

Integer i = 0 + null; 

To troubleshoot this, expand your code to include debugs and identify where the null value is:

system.debug(totalWeekly1K);

if (integer.valueOf(totalWeekly1K) > 0) {
    QWeekly1K = integer.valueOf(totalWeekly1K);
}

To fix the issue you'll need to do a null check as the first item in your If Statement:

system.debug(totalWeekly1K);

if (totalWeekly1K != null && integer.valueOf(totalWeekly1K) > 0) {
    QWeekly1K = integer.valueOf(totalWeekly1K);
}

This is just 1 example there could be other places in your code that can throw this error. You'll have to do some troubleshooting to isolate exactly where the error is and resolve it.

Alternatively you can put something like this at the top of your class:

if (totalWeekly1K == null) {
    totalWeekly1K = 0;
}

Update:

Based on your debug, the error is because QTotalK is null and you're attempting to use it in a math equation. You need to set QTotalK to 0 before trying to use it.

  • Thank you for your reply. I tried both of your suggestions but still getting the error. I am declaring the variables at the beginning of the class before the method. I edited the original question to show this – Jean Grey Apr 23 '18 at 19:02
  • 1
    @JeanGrey I think you didn't quite get the full scope of my answer. I gave you a few examples but I didn't tell you exactly where your error is. This can happen with many built in methods. The integer.value of was just the one I pulled from your example to make it relatable. This can also happen in your string.format method. You're going to have to do some work on this one. – gNerb Apr 23 '18 at 19:11
  • I've debugged, the value of the integer is not null, why would the integer throw an error? – Jean Grey Apr 24 '18 at 14:55
  • You're still not comprehending. The integer may not be causing the issue. Let me say that again, The integer may not be causing the issue. Do I need to say it again? I'm sorry yes that's rude, but apparently it's also necessary. Read every word I put in my answer and in my comments. – gNerb Apr 24 '18 at 14:59
  • The visualforce page tells me which line has the error. It's the QWeekly1K/QTuesdayK. But in accordance with your sage advice I have debugged every single line of code in this section. results attached. – Jean Grey Apr 24 '18 at 20:02

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.