How can I avoid NullPointerExceptions? I have tried using try-catch blocks but that didn’t work.
My program is supposed to get data from Google Calendar. I am getting many elements, but here it is just one element as an example…
for (Iterator<?> i = eventsToday.iterator(); i.hasNext();) {
Component component = (Component) i.next();
String Attachement=null;
if(component.getProperty(Property.ATTACH).toString().trim()!=null){
Attachement=component.getProperty(Property.ATTACH).toString();
}
}
Could someone please help me with this.
Test if
component.getProperty() == nullthe null pointer exception is due to thetoString()being called on a null object. The following example will work. (replaceObject owith the real type returned bygetProperty())