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
VidhiVidhi 

List out of bound exception

hello Friends,

 

I am tring to put my data in one wrapper class list but is ts givin the list out of bound exception.

public class ClS_RecentItem {
public string RecOpp{get;set;}
public list<opportunity> lstOpp{get;set;}
public list<string> name{get;set;}
public list<id> ids{get;set;}

public list<wrpclass> lstWrp{get;set;}

public void Search()
{


name= new list<string>();
ids= new list<id>();
lstWrp=new list<wrpclass> ();
Http httpProtocol = new Http();
HttpRequest request = new HttpRequest();
request.setHeader('Authorization', 'OAuth '+UserInfo.getSessionId());
request.setEndPoint('https://cs6.salesforce.com/services/data/v26.0/recent');
//request.setEndPoint('https://ap1.salesforce.com/services/data/v26.0/sobjects/Opportunity');
request.setMethod('GET');
HttpResponse response = httpProtocol.send(request);
String jsonInput = response.getBody();
system.debug('===>'+jsonInput);
System.JSONParser parser = JSON.createParser(jsonInput);

system.debug('***parser***'+parser);

String JSONString = JSON.serialize(jsonInput);

system.debug('***response***'+JSONString);
while (parser.nextToken() != null) {
System.debug('Current token: ' + parser.getCurrentToken());
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText() == 'name'))

{
parser.nextToken();
name.add(parser.getText());
System.debug('---------------name---------------------Recent Item = '+name);
System.debug('-----------------------------------------------Recent Item = '+parser.getText());

}
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&(parser.getText() == 'id'))
{
parser.nextToken();

ids.add(parser.getText());


System.debug('------------------name--------------------Recent Item id = '+ids);
System.debug('-size of name= '+name.size());
System.debug('-size of nids= '+name.size());
integer i;
for(i=0;i<name.size();i++){
{

for(string s:name){
wrpclass w=new wrpclass(s);
lstWrp.set(i,w);            //Here i am getting error


}
for(id d:ids){
wrpclass a=new wrpclass(d);
lstWrp.set(i,w);    //Here i am getting error


}

System.debug('-Wrapper List--------- = '+lstWrp);
}




}

}
}

}


public class wrpclass{

public string objname{get;set;}
public string objid{get;set;}
public wrpclass(string l)
{

objname=l;

}
public wrpclass(id d)
{

objid=d;

}
}
}

 

 

Any help will be appreciated

SchemeWalkerSchemeWalker

I'm confused - in your for loop (below as written) it looks like you have a list of "wrpclass" (defined as public iin the beginning). The two constructors you have inside that class "wrpclass" are building a new instance of a "wrpclass" by either passing in a String or an Id. In your loop below you are doing a setter by passing in an Integer (i) and a new instance of "wrpclass" creating by passing in a string. So the first thing to think about is that you are not adding any items to your list like you did with Ids (ds.add(parser.getText());). The second thing is that if you want to set values in the "wrapclass" I would think (and I could be wrong) that you would create a new instance of "wrapclass" and then set the values for the string and Id. In this case you are trying to set the id, string not on the instance of the "wrapclass" but on the list itself. I would think you would build a new "wrapclass" instance, set the values of of the object name and the Id and then add that instance to the list you have. Again, I could be reading it wrong but that is what it appears.

 

//**** your current for loop *****

 

for(i=0;i<name.size();i++){
        {

        for(string s:name){
        wrpclass w=new wrpclass(s);
        lstWrp.set(i,w);            //Here i am getting error
        }

Adam HowarthAdam Howarth

Hey,

 

You should read about the list method set here : List Methods

 

You are trying to input an object at an index that doesnt exist.

 

Think about it. What would happen with the following:

 

List<a> list = new List();

list.set(10000000000, 'value');

 

If this was allowed there would be 9999999999 null values up until the one you set.

 

So... you have two options.

 

1. Set the size of the list:

 

lstWrp = new list[name.size()];

integer i;
for(i=0;i<name.size();i++){
{

for(string s:name){
wrpclass w=new wrpclass(s);
lstWrp.set(i,w);            //Here i am getting error

}

 2. Just add it in using the add method? (Unless the order of the items in the list matter, then use this one.)

integer i;
for(i=0;i<name.size();i++){
{

for(string s:name){
wrpclass w=new wrpclass(s);
lstWrp.add(w);


}

 

Let me know how it goes man.

 

Cheers