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

Apex class is:

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>

bob_buzzardbob_buzzard

Do you get a line number with the exception?

TaskTask

No,I didn't receive any line number in exception.

After saving my Apex class and VF page(LogACallForCollections),then if I give

/apex/LogACallForCollections
It is showing error as:
Attempt to de-reference a null object
An unexpected error has occurred.

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

Karthikeyan JayabalKarthikeyan Jayabal

Since your VF page uses Task standard controller, you need pass a valid task id in the page URL as shown below:

/apex/LogACallForCollections?id=00T9000000L8gYD

Hope this helps.

 

bob_buzzardbob_buzzard

That's not strictly true - if you don't pass an id the controller will instantiate a new, empty task for you.

 

In your case, you don't pass it an id but then attempt to access the task information.

TaskTask

Now it is showing error as:

Visualforce Error

System.NullPointerException: Attempt to de-reference a null object
Class.LogACallForCollections.<nit: line 24, column 1

bob_buzzardbob_buzzard

I suspect this line may be the culprit:

 

  loan = getLoanById(activity.WhatId);

 

as you don't appear to initialie activity, so this will be null.