简体   繁体   中英

AsyncEventHandler - Real Time System Java

Im trying to make a simulation program in which:

  • A can is passed on to a conveyor
  • the can is filled
  • the can is checked
  • if the can is damaged, it will be removed from the conveyor

errors:

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)

I want to use the AsyncEventHandler to trigger a code if the can is damaged but it gives a NullPointerException error if the can is found damaged on the first try however the code works fine if the can is not damaged on the first loop.

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");

        }

    }

}

You do not have a default constructor for your Can class. So in your Conveyor class you have Can can; This probably gives you the NullPointerException . Just add another constructor to your Can class like so:

public Can() {};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM