Feels like a super basic question, but I can’t wrap my head around it.
There is a class PriceGroup, with no constructor of whom I’m making an object of.
class SmallPortfolio{
String id;
// all the investments (stocks) belonging to this portfolio.
List<Investment> invList = new ArrayList<Investment>();
}
Now in a separate class, I’m creating a SmallPortfolio object named Spor.
//
String id = InstanceID.getTextNormalize(); //this value is taken from an element from jdom.
SmallPortfolio Spor;
Spor.id = id;
//some code that creates a list of investments
//some code that creates a list of SmallPortfolio objects
In java, how do you give a null object’s field a value getting by the NullPointerException?
An alternate solution would be somehow declaring a SmallPortfolio object that wasn’t null.
It’s a strange problem, but the test program I’m using cannot be modified and it has no constructors for SmallPortfolio.
All you need to do is initialize Spor:
This will create a non-null object. Also, if you’re going to be adding an id to it after each initialization, you may as well add that to the constructor. Hope that helps!