-1

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);
                   }
               }
        }
    }
Dimitar
  • 4,402
  • 4
  • 31
  • 47

2 Answers2

0

You need to initialize doors:

private BidiMap<Integer, Boolean> doors = new SomethingImplementingBidiMap<>();
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • i had but BidiMap is an abstract map. If you know what this is please share :) – Dimitar Apr 03 '15 at 10:39
  • Have you looked at the [javadoc](https://commons.apache.org/proper/commons-collections/javadocs/api-3.2.1/org/apache/commons/collections/BidiMap.html) for known implementing classes? – Andy Turner Apr 03 '15 at 10:42
  • yes, there it is iplemented through DualHashBidiMap(), which my JE says it doesn`t exist – Dimitar Apr 03 '15 at 10:46
0

BidiMap is an interface.
Use a class that implements it (like TreeBidiMap).

Look at this link.

Dimitar
  • 4,402
  • 4
  • 31
  • 47