troubleshooting Question

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

Avatar of chaitu chaitu
chaitu chaituFlag for India asked on
Java
4 Comments2 Solutions694 ViewsLast Modified:
			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());
							
							 }
                       		

				 }
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 2 Answers and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros