It appears that the code snippet from your question is inside a loop that walks multiple values of the key
variable, expecting all the bits that you set before the current iteration to be available during the current iteration as you go through the loop.
To address this problem you need to create the BitSet
object prior to entering the loop, and keep re-using it, instead of creating it every time that you go through the if
statement.
Move this line
BitSet bitSet1 = new BitSet(256);
to outside the loop that is implied in your snippet, or make it a variable in your class to fix the problem.