I have a list of a bean class newSRMAPIResponseBeanList wherein I am always getting a null object at last which I am trying to remove but it is resulting in null pointer exception if I handle the exception, it is not removing that null value. Below is my code.
for (int i = 0; i < newSRMAPIResponseBeanList.size(); i++) {
try {
if (newSRMAPIResponseBeanList.get(i).getCompanyId().equals(null)) {
newSRMAPIResponseBeanList.remove(i);
}
}catch(Exception e) {
e.printStackTrace();
}
}
In the if condition itself it is failing. I want to remove the null value of company ID. Actually newSRMAPIResponseBeanList is a list of List<NewSRMAPIResponseBean> newSRMAPIResponseBeanList = new ArrayList<>();
and the bean class is as follows.
public class NewSRMAPIResponseBean {
private String companyId;
private String investmentId;
private String performanceId;
private List<String> values;
}
Is there any way I can remove that null value? I also tried using Java streams as follows.
List<NewSRMAPIResponseBean> finalList=newSRMAPIResponseBeanList.parallelStream()
.filter(Objects::nonNull)
.collect(Collectors.toList());
This too did not work.