0

I have the class Memory which is a list of blocks, and Blocks which is a list of tuples and Tuple.

Memory m;
for(Block b:m.blocklist){
    for (Tuple t:b.tuplelist){
        //do something
    }
}

The above code works fine, every single tuple goes through it but the problem is even after the last tuple, it continues on leading to a null pointer exception. I can't modify the memory class, blocks nor tuples so how do I avoid the error?

2
  • Would it help to check if the block or tuple you get from the list is null? Commented Oct 23, 2014 at 9:43
  • Thanks for the help. Could've sworn I tried this myself but for some reason it didn't work "-_- Commented Oct 23, 2014 at 10:20

3 Answers 3

1

Supposing that the NPE appears on the line of the second for you can add a check for null:

Memory m;
for(Block b:m.blocklist){
   if (b != null) {
     for (Tuple t:b.tuplelist){
        //do something
      }
   }
 }
Sign up to request clarification or add additional context in comments.

Comments

0

If one of the blocks is null, you'll get an NPE.

Check for null:

Memory m;
for(Block b:m.blocklist){
    if (b != null) {
        for (Tuple t:b.tuplelist){
            //do something
        }
    }
}

Comments

0

Perhaps in the last Blocklist the Tuplelist is null?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.