I am trying to search account numbers on an excel file, which in return, i will get a 'total amount due' if the account number exists in the sheet. I can scan using this code, however, I get error if the account number does not exists in the sheet.
for(int i = 0; i < accno.size(); i++){
for(int j = rowAccNo1+1; j < row.getRowNum(); j++){
srcaccno = formatter.formatCellValue(sheet.getRow(j).getCell(0));
totalamt = formatter.formatCellValue(sheet.getRow(j).getCell(10));
if(accno.get(i).contains(srcaccno)){
totalamountdue.add(i, totalamt);
}
}
}
Edit: Hi, I am using Apache POI.
I declared accno as below:
static ArrayList<String> accno = new ArrayList<String>();
and I am getting this error. perhaps Java can't find the account number that's why he throws this. But I do not know if I can add an empty or null on my arraylist.
at java.util.ArrayList.rangeCheckForAdd(Unknown Source) at java.util.ArrayList.add(Unknown Source) at com.joven.DataExtractionTool.App.compareMastertoSourceABC(App.java:372) at com.joven.DataExtractionTool.App.main(App.java:78) at com.joven.DataExtractionTool.AppTest.testApp(AppTest.java:17) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at junit.framework.TestCase.runTest(TestCase.java:154) at junit.framework.TestCase.runBare(TestCase.java:127) at junit.framework.TestResult$1.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at junit.framework.TestResult.run(TestResult.java:109) at junit.framework.TestCase.run(TestCase.java:118) at junit.framework.TestSuite.runTest(TestSuite.java:208) at junit.framework.TestSuite.run(TestSuite.java:203) at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:131) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Thank you for your response!
What I really like to do is that, if accno exists in the sheet, get totalamountdue. if it does not exists, put blank on totalamountdue so that it will display blank if i output the arraylist in console.
accno, can it containsnull? This doesn't seems rightif(accno.get(i).contains(srcaccno)){Listdon't like to add value into a index bigger than their current size. Now, did you need to have the List to match the account one ? I guess, so you need to add a default value if the account is not foud (null, -1, ...)