gpt4 book ai didi

java - AsyncEventHandler - 实时系统 Java

转载 作者:太空宇宙 更新时间:2023-11-04 09:36:46 26 4
gpt4 key购买 nike

我正在尝试制作一个模拟程序,其中:

  • jar 头被传送到传送带上
  • jar 已满
  • jar 头已检查
  • 如果 jar 头损坏,则会将其从传送带上移走

错误:

Exception in thread "RealtimeThread[pri=20,aperiodic]" 
java.lang.NullPointerException
at assignment$Sensor.run(assignment.java:99)
at javax.realtime.RealtimeThread$Logic.run(RealtimeThread.java:244)
at javax.realtime.MemoryArea.enter(MemoryArea.java)
at javax.realtime.MemoryArea.enterInternal(MemoryArea.java:1472)
at javax.realtime.RealtimeThread$Logic.run(RealtimeThread.java:230)

如果 jar 头损坏,我想使用 AsyncEventHandler 触发代码,但如果第一次尝试时发现 jar 头损坏,它会给出 NullPointerException 错误,但如果 jar 头在第一次循环中未损坏,则代码可以正常工作。

import java.util.Random;
import javax.realtime.*;

public class realtime{

AsyncEvent damage;

public static void main(String[] args) {

realtime a = new realtime();
}

public realtime() {

Can can = new Can(1);

Conveyer conveyer = new Conveyer(can);
Sensor Sensor = new Sensor(can);
Grabber grabber = new Grabber(can, Sensor);

ReleaseParameters relConveyer = new PeriodicParameters(new RelativeTime(1000,0));
ReleaseParameters relSensor = new PeriodicParameters(new RelativeTime(1000,0));

conveyer.setReleaseParameters(relConveyer);
Sensor.setReleaseParameters(relSensor);

conveyer.start();
Sensor.start();

damage = new AsyncEvent();
damage.setHandler(grabber);

}

class Can {

int id;
boolean filled;
boolean damaged;

public Can(int id) {
this.id = id;
}

public void isFilled(boolean status) {
this.filled = status; //Print if the Can is filled
}

public void isDamaged(boolean status) {
this.damaged = status;
}

}

class Conveyer extends RealtimeThread {

Can can;

Random random = new Random();

public Conveyer(Can can) {
this.can = can;
}

@Override
public void run() { //While loop can be used to repeat

while(true) {
System.out.println("Can " + can.id + " has entered the conveyer");
System.out.println("Can " + can.id + " is being filled");
can.isFilled(true); //Sleep to give time to fill
System.out.println("Can " + can.id + " is filled");
System.out.println("Can " + can.id + " is being scanned");
can.isDamaged(random.nextBoolean());
try {
waitForNextRelease();
}
catch (Exception e) {}
}

}

}

class Sensor extends RealtimeThread{

Can can;

public Sensor(Can can) {
this.can = can;
}

@Override
public void run() { //While loop can be used to repeat

while(true) {
if(can.damaged) {
System.out.println("Can " + can.id + " is Damaged!");
damage.fire();
} else {
System.out.println("Can " + can.id + " is moved to Stage 2");
}
try {
waitForNextRelease();
}
catch (Exception e) {}
}

}

}

class Grabber extends AsyncEventHandler {

Can can;
RealtimeThread rtt;
boolean damaged = false;

public Grabber(Can can, RealtimeThread rtt) {
this.can = can;
this.rtt = rtt;
}

public void handleAsyncEvent() {

System.out.println("Can " + can.id + " is disposed");

}

}

}

最佳答案

您的 Can 类没有默认构造函数。因此,在您的 Conveyor 类中,您有 Can can; 这可能会给您带来 NullPointerException。只需向 Can 类添加另一个构造函数,如下所示:

public Can() {};

关于java - AsyncEventHandler - 实时系统 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56417165/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com