I am trying to make a task where: You have 100 doors in a row. You make 100 passes by the doors. The first time through, you visit every door and toggle the door (if the door is closed, you open it; if it is open, you close it). The second time you only visit every 2nd door (door #2, #4, #6, ...). The third time, every 3rd door (door #3, #6, #9, ...), etc, until you only visit the 100th door.
Problem: When putting the keys and the values in the map I get a Null Point Exception.
Here is the code:
import org.apache.commons.collections4.*;
public class Doors_100
{
private BidiMap<Integer, Boolean> doors;
Random r = new Random();
public Doors_100()
{
for(int i = 1; i < 101; i++){
doors.put(Integer.valueOf(i), r.nextBoolean()); //here is the null-pointer
}
}
public void toggleDoors() {
for(int i = 1; i < 101; i++){
for(boolean value : doors.values()){
if(doors.getKey(value) % i == 0){
doors.get(value);
value ^= true;
System.out.println("Key: " + doors.getKey(value) + " Value: " + doors.get(value)
+ " at " + i);
}
}
}
}