function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TaskTask 

Attempt to de-reference a null object

My Apex class:

public with sharing class LogACallForCollections
{
    public static final String PTP_STATUS_NAME = 'PTP - Promise to pay';
    public static final String PROMISE_STATUS_MADE = 'Made';
    public static final String PROMISE_STATUS_KEPT = 'Kept';
    public static final String PROMISE_STATUS_BROKEN = 'Broken';
    public static final String DEFAULT_USER_TYPE = 'collections';

    private Task activity;   
    private Loan__c loan;
    private PageReference acctPage;
    private PageReference requestPage;
    
    public Boolean required { get; set; }
    public Task task {get;set;}

    public LogACallForCollections(ApexPages.StandardController controller)
    {   
        List<Task> a = new List<Task>();
        if (controller != null)
        {
            task = (Task)controller.getRecord();
            a = [SELECT Id,
                               PTP_Date__c,
                               PTP_Time__c,
                               Promise_Status__c,
                               User_Type__c,
                               Hold_Date__c,
                               Hold_Time__c,
                               WhatId,
                               Action_Taken__c,
                               Call_Results__c,
                               PTP_Amount__c,
                               PTP_Method__c,
                               Source_of_Funds__c,
                               Description   
                        FROM Task WHERE id = :task.Id];
            loan = getLoanById(activity.WhatId);
        }
    }

    public LogACallForCollections()
    {
    }
   
    public void change()
    {
        if (activity.Call_Results__c == PTP_STATUS_NAME)
        {
            required = false;
        }
        else
        {
            required = true;        
        }
    }

    private Loan__c getLoanById(Id loanId) {
        Loan__c[] loans = [SELECT Id,
                                  Name,
                                  Loan_WV_Number__c,
                                  Total_to_Current__c,
                                  Customer_Home_Phone__c,
                                  Customer_Name__c,
                                  Customer_Cell_Phone__c,
                                  Joint_Customer_Full_Name__c,
                                  Joint_Customer_Phone__c
                           FROM Loan__c
                           WHERE id = :loanId];
        if (loans.size() > 0)
        {
            return loans.get(0);
        }                                     
        return null;
    }

    public Loan__c getLoan()
    {
        if (loan == null)
        {
            loan = getLoanById(ApexPages.currentPage().getParameters().get('id'));   
        }
        return loan;
    }
    
    public Integer getPtpTime()
    {
        if (activity != null)
        {
            return activity.PTP_Time__c.hour();        
        }
        return 0;
    }
    
    public void setPtpTime(Integer hour)
    {
        if (activity != null)
        {
            activity.PTP_Time__c = makePtpTime(hour);
        }   
    }
    
    public Integer getHoldTime()
    {
        if (activity != null)
        {
            if (activity.Hold_Time__c == null)
                return -1;
            else
                return activity.Hold_Time__c.hourGmt();    
        }
        return -1;
    }
    
    public void setHoldTime(Integer value)
    {
        if (activity != null)
        {
            if ( value == -1 )
                activity.Hold_Time__c = null;
            else
                activity.Hold_Time__c = Datetime.newInstanceGmt(1999, 1, 1, value, 0, 0);
        }   
    }  

    private DateTime makePtpTime(Integer hour)
    {
        return DateTime.newInstance(1999, 1, 1, hour, 0, 0);
    }

    public Task getActivity()
    {
        if (activity == null)
        {
            activity = new Task();
            activity.PTP_Date__c = System.today().addDays(1);
            activity.PTP_Time__c = makePtpTime(DateTime.now().hour());
            activity.Promise_Status__c = PROMISE_STATUS_MADE;
            activity.User_Type__c = DEFAULT_USER_TYPE;
            activity.Hold_Date__c = System.today().addDays(1);
            activity.Hold_Time__c = null;
        }
        return activity;
    }

    private Boolean checkPTPAmount()
    {
        if (activity.PTP_Amount__c == null || loan.Total_to_Current__c == null)
        {
            return false;
        }
        if ((activity.PTP_Amount__c > (loan.Total_to_Current__c * 0.51) || activity.PTP_Amount__c == 50.0) &&
             activity.PTP_Amount__c <= loan.Total_to_Current__c)
             {
                return true;
             }
        return false;
    }

    private Boolean checkPTPDate()
    {
        Date dt = System.today();
        dt.daysBetween(activity.PTP_Date__c);
        return (dt.daysBetween(activity.PTP_Date__c) < 0 || dt.daysBetween(activity.PTP_Date__c) >= 31);
    }

    private static DateTime makePromiseDate(Task task)
    {
        Date currDate = task.PTP_Date__c;
        Time currTime = task.PTP_Time__c.time();
        return DateTime.newInstance(currDate, currTime);
    }

    private Boolean checkTotalToCurrentAmount()
    {
        if (loan.Total_to_Current__c == null || loan.Total_to_Current__c == 0)
        {
            return true;
        }
        return false;
    }
    
  public PageReference Submit()
  {
      Boolean ptpF = PTP_STATUS_NAME.equals(activity.Call_Results__c);
      checkTotalToCurrentAmount();

//    System.debug('ptpF: ' + ptpF + ' && ' + 'checkTotalToCurrentAmount(): ' + checkTotalToCurrentAmount() );
    if (ptpF && checkTotalToCurrentAmount())
    {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'The Loan`s Total-to-current is 0.'));
        return acctPage;
    }
    checkPTPAmount();
    checkPTPDate();
//    System.debug('!ptpF: ' + !ptpF + '  ||  ( ptpF: ' + ptpF + ' && ' + 'checkPTPAmount(): ' + checkPTPAmount() + ')');
//    System.debug('if ( ptpF: ' + ptpF + ' && checkPTPDate(): ' + checkPTPDate());
    if (!ptpF || (ptpF && checkPTPAmount()))
    {
        if (ptpF && checkPTPDate())
        {
          ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'PTP Date can be for no more than 31 calendar days in the future.'));
          return acctPage;
        }

      activity.OwnerID = UserInfo.getuserid();
      activity.ActivityDate = System.Today();
    //  activity.WhatId = getLoan().Id;
      activity.Status = 'In Progress';
      activity.subject = activity.Call_Results__c;
      activity.Priority = 'High';
      activity.IsReminderSet = true;

      if (activity.Call_Results__c == 'LM - Left message')
      {
          loan.Note_Date__c = System.Today();
          update loan;
      }

      if (activity.Hold_time__c != null)
      {
          loan.Note_Date__c = activity.Hold_date__c;
          update loan;
      }

      if (activity.Call_Results__c == 'Legal Review')
      {
          Account accountInf = [SELECT Id,Name,legal__c FROM account where id = : [select customer_number__c  from loan__c where id =: loan.Id].customer_number__c];
          accountInf.legal__c = true;
          upsert accountInf;
      }

      if (activity.Call_Results__c == PTP_STATUS_NAME)
      {                  
        activity.ReminderDateTime = makePromiseDate(activity);
        activity.Hold_date__c = activity.PTP_Date__c;
        activity.Hold_time__c = activity.PTP_Time__c;
        activity.PTP_TimeOnly__c = activity.PTP_Time__c.formatGmt('hh:mm a');
        loan.Note_Date__c = activity.Hold_date__c;
        loan.Has_PTP__c = true;
        update loan;
      }
      else
      {
        Time holdTime = null;
        if (activity.Hold_time__c != null)
        {
            holdTime = activity.Hold_time__c.time();
        }
        else
        {
            holdTime = Time.newInstance(0, 0, 0, 0);
        }
        activity.ReminderDateTime = DateTime.newInstance(activity.Hold_date__c, holdTime);
      }
      upsert activity;
//     acctPage = new ApexPages.StandardController(loan).view();
//     acctPage.setRedirect(true);
    }
    else
    {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'PTP Amount could not be greater than Total-to-current or less than 51% of Total-to-current.'));    
    }
    PageReference requestPage = Page.Congratulations;
    requestPage.setRedirect(true);
    return requestPage;
  }    
    
    public void is()
    { //activity.Hold_date__c = System.Today();
    }
    
    public void getIsEmpty()
    { //activity.Hold_date__c = System.Today();
      acctPage = new ApexPages.StandardController(loan).view();
      acctPage.setRedirect(true);
    }
/*    
    public PageReference cancel()
    { return new PageReference('/' + getLoan().Id);
    }   
*/    
}

 

VF page code is:

<apex:page standardController="Task" extensions="LogACallForCollections">
<apex:detail subject="{!task.id}"/>

....

</apex:page>

Bhawani SharmaBhawani Sharma
Which line is throwing exception?
JohnSchultzJohnSchultz

Without knowing the line that is throwing the error, I would guess that the problem is in your constructor. In particular, take a look at this line:

 

loan = getLoanById(activity.WhatId);

I'm not sure if you have other code that you're not showing us, but the variable "activity" doesn't appear to ever be set to a value. Like I said, without seeing all of the code, this is just a guess.

souvik9086souvik9086

Yes you are right. Just check how to populate this value activity.WhatId. It might not contain any value.

 

If this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.

Thanks