We help IT Professionals succeed at work.

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

chaitu chaitu
chaitu chaitu used Ask the Experts™
on
			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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:

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.size() > 1 && finalList.get(1) != null)
		                        		 {
		                        			 Supp supp = (Supp) finalList.get(1);
		                        			 form.setFirstName2(supp.getFirstName());
		                        			 form.setLastName2(supp.getLastName());
		                        		
		                        		 }
                         		
                         		 
							if(finalList.size() > 2 && finalList.get(2) != null)
							 {
								 Supp supp = (Supp) finalList.get(2);
								 form.setFirstName3(supp.getFirstName());
								 form.setLastName3(supp.getLastName());
							
							 }
                        		
                         		
                         	
							if(finalList.size() > 3 && finalList.get(3) != null)
							 {
								 Supp supp = (Supp) finalList.get(3);
								 form.setFirstName4(supp.getFirstName());
								 form.setLastName4(supp.getLastName());
							
							 }
                       		

				 }

Open in new window

Top Expert 2016

Commented:
What about
if (null != finalList && !finalList.isEmpty() && finalList.size() > 3) {

Open in new window

Awarded 2011
Awarded 2011
Commented:

if they cannot be null once they are persent in the list you probably don't need to check on that:
					
				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..size() >1)
		                        		 {
		                        			 Supp supp = (Supp) finalList.get(1);
		                        			 form.setFirstName2(supp.getFirstName());
		                        			 form.setLastName2(supp.getLastName());
		                        		
		                        		 }
                         		
                         		 
							if(finalList..size() >2)
							 {
								 Supp supp = (Supp) finalList.get(2);
								 form.setFirstName3(supp.getFirstName());
								 form.setLastName3(supp.getLastName());
							
							 }
                        		
                         		
                         	
							if(finalList.size() >3)
							 {
								 Supp supp = (Supp) finalList.get(3);
								 form.setFirstName4(supp.getFirstName());
								 form.setLastName4(supp.getLastName());
							
							 }
                       		

				 }

Open in new window

Top Expert 2016

Commented:
It would all depend on whether you need partial, or full setting of the form