I have a problem to handle an Arraylist with empty fields.
As input I have values from an Excel List and there some cells empty (show example).
Column A Column B Column C Column D
Val1 Val2 Val3 Val4
Bla1 Bla2 Bla3
Hug1 Hug2 Hug3 Hug4
...
These Values are in an Arraylist so far so good. Now I have a Switch Case where I choose now Column D to work with. The Problem now is that I don’t can handle null fields. These must be removed or get a step over, because my output does not have a null. As I’m trying to do this I got an null pointer exception.
Here is some code:
private Arraylist<ExcelReader> el;
private void doSomething() {
switch (chooseColumn) {
// ArrayList is loaded
//…
case “D”:
// here I want to remove the null fields
for (int c = 0; c <= el.size(); c++) {
if(el.get(c).getColumnD().isEmpty()) {
el.remove(c);
}
}
break;
// …
}
I’ve tried it at the point where I write it back to a file, but got the null pointer exception. Where is my mistake? What am I doing wrong?
el.get(c).getColumnD().isEmpty())will be a problem if you havenullvalues. You could check there if youget(c)returnnull.el.get(c)is the one return null, you can see the duplicate to find really nice solutions. If not, this will require some improvments or a condition to filter those without removing it of the list