I am adding objects to an ArrayList that is initialized as null inside a for each loop, but SonarLint is giving me a "NullPointerException" could be thrown when I try to add each object. Why is this error being pointed out?
public List<SatelliteData> getData()
{
SatelliteData satellite;
ArrayList<SatelliteData> satelliteList = null;
try(FileInputStream file = new FileInputStream(satelliteOutputFile))
{
TLEParser parsedFile = new TLEParser();
List<TLE> tleList = parsedFile.readFile(file);
for (TLE tle : tleList)
{
satellite = new SatelliteData(tle);
satellite.collectSatelliteData();
satelliteList.add(satellite); //A "NullPointerException" could be thrown; "satelliteList" is nullable here
}
}
catch (IOException ex)
{
LOGGER.error("IO Exception: " + ex);
notifyTheObservers("IO Exception: " + ex);
}
return satelliteList;
}