This is my for loop function inside the servlet
Here i have a question .
The Data will be passed from the USER Interface to this .
In some conditions , some information (For example symbol or side ) may not be passed , then in those conditions , i am getting NullPointreException as null would be supplied to it .
List<Bag> bags = new ArrayList<Bag>(bagdata.length);
for (FormBeanData ld : data) {
Bag bag = new Bag();
bag.symbol = ld.getSymbol();
bag.side = ld.getSide();
bags.add(bag );
}
Is there anyway we can handle such situations ??
Thank you .
The code you submitted most likely does not even compile:
bags.add(Bag)does not make sense. You need to add thebagobject (the instance), notBag(the class).In any case if you want to check if a reference is
nulljust check it:You also have to check before iterating