Link to home
Start Free TrialLog in
Avatar of chaitu chaitu
chaitu chaituFlag for India

asked on

how to avoid indexoutofboundsexception in below code when we get list size as 1 or 2 or 3

			how to avoid indexoutofboundsexception in below code when we get list size as 1 or 2 or 3;how to rewrite the code to avoid this exception
				
				List finalList=  data.getSupp();
                	      if (null != finalList && !finalList.isEmpty()) {
				
                         			 Supp supp = (Supp)finalList.get(0);
                         			 form.setFirstName1(supp.getFirstName());
                         			 form.setLastName1(supp.getLastName());
                         		
                         		 
                         		
		                         		if(finalList.get(1) != null)
		                        		 {
		                        			 Supp supp = (Supp) finalList.get(1);
		                        			 form.setFirstName2(supp.getFirstName());
		                        			 form.setLastName2(supp.getLastName());
		                        		
		                        		 }
                         		
                         		 
							if(finalList.get(2) != null)
							 {
								 Supp supp = (Supp) finalList.get(2);
								 form.setFirstName3(supp.getFirstName());
								 form.setLastName3(supp.getLastName());
							
							 }
                        		
                         		
                         	
							if(finalList.get(3) != null)
							 {
								 Supp supp = (Supp) finalList.get(3);
								 form.setFirstName4(supp.getFirstName());
								 form.setLastName4(supp.getLastName());
							
							 }
                       		

				 }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of cmalakar
cmalakar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CEHJ
What about
if (null != finalList && !finalList.isEmpty() && finalList.size() > 3) {

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It would all depend on whether you need partial, or full setting of the form