[英]Exception in thread "DoorLock" java.lang.NullPointerException
hello the program complies but when the program runs it shows this error then carries on displaying the threads etc the format on this post took so long to post.... 1 2 3 4 5 6 7 8 9 10 11 12 13412414145256t423626262626263573你好程序符合但是当程序运行时它显示这个错误然后继续显示线程等这个帖子上的格式花了很长时间发布.... 1 2 3 4 5 6 7 8 9 10 11 12 13412414145256t423626262626263573
Exception in thread "DoorLock" java.lang.NullPointerException
at miniass3.AirlockDoor.run(AirlockDoor.java:15)
at java.lang.Thread.run(Unknown Source)"
Here is my code:这是我的代码:
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();
// 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", null);
doors[1] = new AirlockDoor("Door 2", null);
// AirlockDoor implements Runnable and so make sure
// we tie the objects to Thread objects
Runnable threadJob = new AirlockDoor(null, MainAirLock);
// 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();
}
}
}
}
Separate:分离:
package miniass3;
public class AirlockDoor implements Runnable {
private AirLock lockOperator;
private String doorName;
// private boolean Door1OpenRequest = false;
//private boolean Door2OpenRequest = false;
public AirlockDoor(String doorName, AirLock airLock) {
this.doorName = doorName;
}
public void run() {
while (true) {
//this is the line that fails
if(lockOperator.isLockState() == false); {
System.out.println("False");
}
}
}
public void requestToOpen() {
System.out.println(doorName + " requests to be opened");
}
}
sperate分散
package miniass3;
public class AirLock {
private boolean lockState = false;
// TODO: Change this to look like setLockState method.
public void setAirLock(boolean newLock) {
this.lockState = newLock;
}
public boolean isLockState() {
return lockState;
}
public void setLockState(boolean lockState) {
this.lockState = lockState;
}
}
lockOperator is not initialized. lockOperator 未初始化。 But you are trying to access the lockOperator.但是您正在尝试访问 lockOperator。
public AirlockDoor(String doorName, AirLock airLock)
{
this.lockOperator = airLock; // you missed to assign the airLock to lockOperator. So that the lockOperator is not yet assigned
this.doorName = doorName;
}
instead of代替
public AirlockDoor(String doorName, AirLock airLock)
{
this.doorName = doorName;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.