0

I am getting an error when I try to save data from a Visualforce page calling a controller method. Can anyone help me figure out how to avoid this error? The error is:

List index out of bounds: 1 Error is in expression '{!saveAdd}' in component in page rda_task: Class.RDATasks.saveAdd: line 68, column 1

Controller:

public with sharing class RDATasks{

    public list<Task> listTask{get;set;}
    public list<Task> listTaskFollow{get;set;}
    public integer n;

    Set<Task>Tasks = new Set<Task>();
    Set<Task>FollowTasks = new Set<Task>();

    private final Contact con;
    public RDATasks(ApexPages.StandardController myController){
        listTask = new List<Task>();
        listTaskFollow = new List<Task>();
        con=(Contact)myController.getRecord();
    }

//  Adds RDA Task to list
    public void RDA()
    {
        listTask.add(new Task(
           WhoId = con.Id,
           Status = 'Completed',
           ActivityDate = system.TODAY(),
           Subject = '',
           Description = '',
           OwnerId = UserInfo.getUserId(),
           WhatId = con.AccountId,
           RDA__c = TRUE)
           );
    }

//  Adds RDA Follow-Up Task to list
    public void RDAfollow()
    {
        n=listTask.size()-1;
        listTaskFollow.add(new Task(
           WhoId = con.Id,
           Status = 'In Progress',
           ActivityDate = system.TODAY()+7,
           Subject = 'Follow-Up: '+listTask[n].Subject,
           OwnerId = UserInfo.getUserId(),
           WhatId = con.AccountId,
           RDA__c = TRUE)
           );
    }

//  Saves of RDA & follow-up tasks & add another
    public PageReference saveAdd(){
        insert listTask;
        insert listTaskFollow;

        if(!listTaskFollow.isEmpty()){
            Tasks.addAll(listTask);
            FollowTasks.addAll(listTaskFollow);
        if(!Tasks.containsAll(FollowTasks)){
            integer last = listTaskFollow.size();
            listTask.add(listTaskFollow[last]);
        }
        }
        RDAfollow();
            {
            PageReference contactPage = new PageReference('/apex/RDA_Task?id=' + con.Id);
            contactPage.setRedirect(true);
            return contactPage;
            }
    }

//  Saves RDA & Follow-up Tasks added no additional
    public PageReference saveRDA(){
        insert listTask;
        insert listTaskFollow;

        if(!listTaskFollow.isEmpty()){
            Tasks.addAll(listTask);
            FollowTasks.addAll(listTaskFollow);
        if(!Tasks.containsAll(FollowTasks)){
            integer last = listTaskFollow.size();
            listTask.add(listTaskFollow[last]);
        }
        }
        RDAfollow();
            {
            PageReference contactPage = new PageReference('/' + con.Id);
            contactPage.setRedirect(true);
            return contactPage;
            }
    }
}
5

This one is easy to hit

 integer last = listTaskFollow.size();  // returns 1 if one member in list
 listTask.add(listTaskFollow[last]);    // index[1] should be index[0] when list has 1 element

You index lists using zero-based counting; size() is always 1 greater than the last index

  • So do I need to re-write as 'integer last = listTaskFollow.size() - 1;' – John Neilan Jan 25 '17 at 20:35
  • not only that, you have to check the listTaskFollow..size()>=1 before assignment – Santanu Boral Jan 25 '17 at 20:48
  • @JohnNeilan - yes – cropredy Jan 25 '17 at 20:57

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.