I have read many of the other answers on StackOverflow regarding this same type of problem, however, mine is different than any I've read.
I am having troubles adding Objects to an ArrayList. I keep getting NullPointerException's. As you can see, my list is initialized, and I am feeding it the proper type of objects. It fails at the first one. You can see my println before trying to add to the ArrayList, and the println shows all information correctly, nothing is null, those are all the properties of the object. It tries to add that object to the ArrayList, and throws a NullPointerException.
Please help sort me out so I can move on from this issue. Thanks guys!
protected ArrayList units = new ArrayList<Fleet>();
public FleetParser(JSONObject json)
{
super(json);
}
@Override
public void process(JSONObject o)
{
Fleet fleet = new Fleet();
fleet.inflateFromJSON(o);
if(fleet.getUid() != null) {
System.out.println(fleet.getUid() + " " + fleet.getPuid() + " " + fleet.getShips() + " " + fleet.getName());
units.add(fleet);
}
}
Edit: Here is the Stacktrace from the console in Intellij. You will notice it executes the first println just fine.
:compileJavaNote: /home/cody/IdeaProjects/NeptuneCommand/src/main/java/com/codyhalovich/neptunecommand/Parsers/FleetParser.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
:processResources UP-TO-DATE
:classes
:runSimple
Exception in thread "main" java.lang.NullPointerException
78 17 45 New Bellatrix I
at com.codyhalovich.neptunecommand.Parsers.FleetParser.process(FleetParser.java:25)
at com.codyhalovich.neptunecommand.Parsers.BaseParser.initParse(BaseParser.java:24)
at com.codyhalovich.neptunecommand.Parsers.BaseParser.<init>(BaseParser.java:16)
at com.codyhalovich.neptunecommand.Parsers.FleetParser.<init>(FleetParser.java:14)
at com.codyhalovich.neptunecommand.Parsers.ReportParser.<init>(ReportParser.java:26)
at com.codyhalovich.neptunecommand.NeptuneCommand.main(NeptuneCommand.java:25)
:runSimple FAILED
Here is the Interface that is implemented by a base abstract class
public interface ParserInterface {
ArrayList<Object> units = new ArrayList<Object>();
JSONObject json = null;
void initParse();
void process(JSONObject o);
java.util.ArrayList getUnits();
}
Here is the BaseParser abstract class
public abstract class BaseParser implements ParserInterface {
private JSONObject json;
protected ArrayList units = new ArrayList<Object>();
public BaseParser(JSONObject json)
{
this.json = json;
this.initParse();
}
public void initParse()
{
for(Object key: json.keySet())
{
if(key != null) {
this.process((JSONObject) json.get(key));
}
}
}
public abstract void process(JSONObject json);
public ArrayList getUnits()
{
return this.units;
}
}
The piece of code that is running this is:
JSONObject fleets = report.getFleets();
FleetParser fleetParser = new FleetParser(fleets);
unitsin the subclass has been initialiased at the time that the base class constructor is running. Incidentally, it's a REALLY bad idea to have two fields of the same name - one in the base class and one in the subclass. It leads to confusing bugs like this one.ArrayList<Object> unitsandJSONObject jsoninParserInterface, anyway you already declared them inBaseParser. Also you shouldn't be invoking non-private (or non-final, or non-static) methods in constructor (public void initParse()) since they are polymorphic so they could be overridden in derived class.