Tag Archives: List

[Solved] Sping controller Receives list entity parameter Error: nested exception is java.lang.IndexOutOfBoundsException: Index: 256, Size: 256

When the controller in spin receives entity parameters
if there is a list attribute in the entity, an error will be reported when the list value is too large

public class Model{
    private List<String> strings;
}

Error Messages: “Invalid property ‘rynrlist[256]’ of bean class [com.ac.intellsecurity.model.exam.records.ExamTrainingRecordsModel]: Index of out of bounds in property path ‘rynrlist[256]’; nested exception is java.lang.IndexOutOfBoundsException: Index: 256, Size: 256”

Reason:

Solution:
1. modified into jsonobject to receive parameters and then converted into an entity (recommended)

@RestController
@RequestMapping("/controller")
public class Controller {

    //Before Modified
    @PostMapping("/addModel")
        public AjaxResult addModel(Model model){
        //.........
    }

    //Modified
    @PostMapping("/addModel")
    public AjaxResult addModel(JSONObject json){
        //.........
        Model model = JSONObject.parseObject(json.toJSONString(),Model.class);
    }
}    

2. Add method in controller

@InitBinder
protected void initBinder(WebDataBinder binder) {
    binder.setAutoGrowNestedPaths(true);
    binder.setAutoGrowCollectionLimit(1024);
}

 

An error is reported when traversing the list collection to delete elements

An error is reported when traversing the list collection to delete elements

 

When traversing the list collection to delete elements, problems such as index out of bounds exception or incomplete content traversal will occur

Example:

List< String> al = new ArrayList< String>();

    al.add(“12”);

    al.add(“1”);

    al.add(“13”);  

    int size = al.size();

 

 

 

Problem 1: index out of bounds exception in thread “main” java.lang.indexoutofboundsexception: index: 2, size: 2

  for(int i=0; i< size; i++){

    if(“1”.equals(al.get(i)))

      al.remove(i);

   }

Problem analysis: delete an element after meeting the conditions. There are only two elements in the collection. Only al. Get (0) and al. Get (1) can be used. When al. Get (2) obtains elements, the index exceeds the limit. Some students may want to change the number of sizes above to al. Size (). Although the index crossing problem is solved, there is a problem of incomplete traversal

 

 

Problem 2: incomplete content traversal

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

    if(“1”.equals(al.get(i)))

      al.remove(i);

   }

Problem analysis: delete an element after meeting the conditions, and no longer cycle the third element

 

 

Correct code:

Iterator< String> it = al.iterator();

    while(it.hasNext()){

    if(“1”.equals(it.next()))

      it.remove();

   }