okay i'm trying to stop the console displaying a null expection but i'm not sure why its doing it i was wondering if you guys could help me
package miniass3; public class AirLock { private boolean lockState = false; public AirLock(boolean newLock){ lockState = newLock; } public boolean isLockState() { return lockState; } public void setLockState(boolean lockState) { this.lockState = lockState; } }
package miniass3; public class AirlockDoor implements Runnable { private AirLock lockOperator; private String doorName; public AirlockDoor(String name, AirLock airLock){ doorName = name; } public AirlockDoor() { } public void run() { while (true){ if(lockOperator.isLockState() == false){ System.out.println("False"); } } } public synchronized void requestToOpen() { System.out.println(doorName + " requests to be opened"); } }
package uk.ac.aber.dcs.airlock; import miniass3.AirLock; import miniass3.AirlockDoor; public class AstronautSimulator { public static void main(String [] args){ simulate(); } private static void simulate() { // Create an AirLock object // STEP 1: CREATE AN EMPTY VERSION OF THE AirlockDoor CLASS AirlockDoor[] doors = new AirlockDoor[2]; // For this prototype this can be an empty (no method) // class. It is a common object used in the Door class // that contains the single lock-key that protects the // imaginary airlock room from having two doors open at // the same time // STEP 2: CREATE THIS CLASS AirLock MainAirLock = new AirLock(false); // Give the doors a name (for debugging purposes) and // a common airlock object used to store the "key" used // by a critical section of code in the Door class doors[0] = new AirlockDoor("Door 1", MainAirLock); doors[1] = new AirlockDoor("Door 2", MainAirLock); // AirlockDoor implements Runnable and so make sure // we tie the objects to Thread objects Runnable threadJob = new AirlockDoor(); // STEP 3: CREATE TWO THREADS FOR THE TWO DOORS AND START THEM // ENTER CODE HERE Thread doorController1 = new Thread(threadJob); Thread doorController2 = new Thread(threadJob); doorController1.setName("DoorLock"); doorController2.setName("DoorLock"); doorController1.start(); doorController2.start(); // Loop infinitely which try's to open the airlock doors while (true){ // random() returns a value in range 0.0..<1.0 multiplied by 10 and mod doors.length // to return either 0 or 1 in order to randomly decide which door to request int doorNum = (int)((Math.random() * 10) % doors.length); // STEP 4: MAKE A REQUEST TO OPEN THE DOOR AND THEN ADD A DELAY BEFORE // TRYING TO OPEN ANOTHER DOOR // ENTER CODE HERE doors[doorNum].requestToOpen(); try{ Thread.sleep(800); } catch(InterruptedException ex){ ex.printStackTrace(); } } } }