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;
            }
    }
}

1 Answer 1

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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .