Click here to Skip to main content
15,351,641 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I keep receiving Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4 at covid19.Covid19.main(Covid19.java:70) but the array I used is null so it shouldn't have a specified length

What I have tried:

String[] CovidRecordList = null;
ArrayList CovidRecordClassList = new ArrayList<>();

for (String covidListItem: CovidList)
{
CovidRecordList = covidListItem.split(",");

Country CountryClass = new Country();
CovidRecord CovidRecordClass = new CovidRecord();

CovidRecordClass.date = CovidRecordList[0];
CountryClass.iso3 = CovidRecordList[1];
CountryClass.continent = CovidRecordList[2];
CountryClass.countryName = CovidRecordList[3];
CountryClass.lat = CovidRecordList[4];
CountryClass.lon = CovidRecordList[5];
CovidRecordClass.cumulativePositive = CovidRecordList[6];
CovidRecordClass.cumulativeDeceased = CovidRecordList[7];
CovidRecordClass.cumulativeRecovered = CovidRecordList[8];
CovidRecordClass.currentlyPositive = CovidRecordList[9];
CovidRecordClass.hospitalized = CovidRecordList[10];
CountryClass.nuts = CovidRecordList[11];

CovidRecordClassList.add(CovidRecordClass);
}
Posted
Updated 21-May-22 23:46pm
Comments
zemiak123 22-May-22 9:02am
   
What is in CovidList ?
this error you get if data item is too short ie.

String[] CovidList = {
"0,1,2,3",
"0,1,2,3",
"0,1,2,3",
};

1 solution

We have no idea which lin e is line 70 - from the error message:
Index 4 out of bounds for length 4 at covid19.Covid19.main(Covid19.java:70)
So the first thing you need to do is work out which line it is. Most editors support CTRL+G to go directly to a line number.

Then put a breakpoint on that line, and run your code in the debugger. When it hits the breakpoint, it will stop and you can look at the array indexes involved and work out which one is out of bounds. Then you can work back to find out why, then you can styart fixing it.

But we can't do any of that for you!
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900