I am trying to filter out some of the data from a List of Entity Objects. Here is my sample code:
List<Student> lstUniversalEvents = getAllStudentEvents(StudentID,startTime,endTime);
List<Student> lstTriggerEvents=null;
if(lstUniversalEvents.size()>0)
{
for (Student event1 : lstUniversalEvents)
{
if(strHighSchool.equals("true"))
{
if(event1.getClass().equals("HIGH_SCHOOL"))
{
lstTriggerEvents.add(event1);
}
}
}
}
The line lstTriggerEvents.add(event1); is throwing a java.lang.NullPointerException.
Trouble-shooting, I found the following:
lstUniversalEvents list has data.if(lstUniversalEvents.size()>0) was PASSEDif(strHighSchool.equals("true")) was PASSEDif(event1.getClass().equals("HIGH_SCHOOL")) was PASSEDSo basically, it's failing to add the event to the new list lstTriggerEvents.
How do I fix this?
You never initialized lstTriggerEvents, it's still null.
Change the declaration to
List<Student> lstTriggerEvents=new ArrayList<Student>();
When you declare a variable you have to initialize it, otherwise the JVM assigns a default value. For reference types (like your list) that value is null.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With