-1

I have a for loop in which i need to insert a condition. But the issue is in the first iteration am getting a null pointer exception as the values inside the condition will only be populated in the second iteration.

for (int i = 0; i < linkElements.size(); i++) {
    System.out.println(linkElements.size());
        // I need to add a condition here
    generatelocators("select", linkElements.get(i), driver);
}
Sreejith C
  • 25
  • 1

2 Answers2

0

You can avert a NullPointerException in a condition, if your initial condition gets a condition. To be more specific, if the original condition ensures a certain data constellation that is only possible starting from the second iteration and onward, but you want the loop to go through the first iteration, you can write a for-loop like this:

boolean isFirstIteration = true;
for (int i = 0; i < linkElements.size() && (isFirstIteration || yourActualCondition);
        i++, isFirstIteration = false) {

    System.out.println(linkElements.size());
        // I need to add a condition here
    generatelocators("select", linkElements.get(i), driver);
}

In other words, nest your actual condition in an outer condition which OR-links it with the secondary condition isFirstIteration. Then ensure, that isFirstIteration is only true once at the beginning.

There might still be a null pointer exception eventually (or some other exception) if your loop has progressed a bit. This is impossible to know, as you have omitted the condition in your question. With more details on the condition, we can maybe help you more.

TreffnonX
  • 2,924
  • 15
  • 23
0

Here are two for loop solutions that skip only the first element in the collection:

for (int i = 1; i < linkElements.size(); i++) {
    // your code here
}

for (int i = 0; i < linkElements.size(); i++) {
    if(i > 0) {
        // your code here
    }
}

With iterator:

linkElements.listIterator(1) // skip first element
        .forEachRemaining(element -> {
            // your code here
        });

// Replace 'Integer' with linkElements' actual type
Iterator<Integer> iterator = linkElements.listIterator(1); // skip first element
while (iterator.hasNext()) {
    Integer element = iterator.next();
    // your code here
}

If there are more null elements or they are not on a fixed index, then a null check is a more proper solution:

for (int i = 0; i < linkElements.size(); i++) {
    if(linkElements.get(i) != null) {
        // your code here
    }
}

for (Integer element : linkElements) { // Replace 'Integer' with linkElements' actual type
    if (element != null) {
        // your code here
    }
}

linkElements.forEach(element -> {
    if (element != null) {
        // your code here
    }
});

linkElements.iterator()
        .forEachRemaining(element -> {
            if (element != null) {
                // your code here
            }
        });

// Replace 'Integer' with linkElements' actual type
Iterator<Integer> iterator = linkElements.iterator();
while (iterator.hasNext()) {
    Integer element = iterator.next();
    if (element != null) {
        // your code here
    }
}

And two solutions using Stream API:

linkElements.stream()
    .skip(1L) // skip first element
    .forEach(element -> {
         // your code here
    });

linkElements.stream()
     .filter(Objects::nonNull) // skip null elements
     .forEach(element -> {
         // your code here
     });
MartinBG
  • 1,500
  • 13
  • 22