AbstractStateMachine.java中的NullPointerException:如果使用Builder

时间:2016-04-07 11:39:09

标签: spring-statemachine

下面的代码抛出了一个NPE,我不知道出了什么问题。我找不到初始化currentState的代码。有人可以帮忙吗?

public class TestSMApplication {

StateMachine<String, String> buildMachine1() throws Exception {
    Builder<String, String> builder = StateMachineBuilder.builder();
    builder.configureConfiguration().withConfiguration()
            .listener(listener());

    builder.configureStates()
            .withStates()
            .initial("OPEN")
            .end("CLOSED")
            .states(new HashSet<String>(Arrays.asList("OPEN", "PROGRESS",
                    "CLOSED")));

    builder.configureTransitions().withExternal().source("OPEN")
            .target("PROGRESS").event("RUN").and().withExternal()
            .source("OPEN").target("TERMINATE").event("STOP");

    StateMachine<String, String> sm = builder.build();
    return sm;
}

public org.springframework.statemachine.listener.StateMachineListener<String, String> listener() {
    return new StateMachineListenerAdapter<String, String>() {
        @Override
        public void stateChanged(State<String, String> from,
                State<String, String> to) {
            System.out.println("State change to " + to.getId());
        }
    };
}

public static void main(String[] args) throws Exception {
    TestSMApplication tsm = new TestSMApplication();
    StateMachine<String, String> m1 = tsm.buildMachine1();
    m1.start();
    m1.sendEvent(Events.PROCESSING_SUCCEEDED.name());
}

}

我正在使用: org.springframework.statemachine:弹簧的statemachine核:1.1.0.M2

感谢Fredy

1 个答案:

答案 0 :(得分:0)

疯了,我无法重现错误,但发现了一个副本&amp;粘贴代码中的错误。更好的是:

package demo.sm;

import java.util.Arrays;
import java.util.HashSet;

import org.springframework.statemachine.StateMachine;
import org.springframework.statemachine.config.StateMachineBuilder;
import org.springframework.statemachine.config.StateMachineBuilder.Builder;
import org.springframework.statemachine.listener.StateMachineListenerAdapter;
import org.springframework.statemachine.state.State;

public class AnotherTest {

        StateMachine<String, String> buildMachine1() throws Exception {
            Builder<String, String> builder = StateMachineBuilder.builder();
            builder.configureConfiguration().withConfiguration()
                    .listener(listener());

            builder.configureStates()
                    .withStates()
                    .initial("OPEN")
                    .end("CLOSED")
                    .states(new HashSet<String>(Arrays.asList("OPEN", "PROGRESS",
                            "CLOSED")));

            builder.configureTransitions()
                .withExternal()
                    .source("OPEN")
                    .target("PROGRESS")
                    .event("RUN")
                    .and()
                 .withExternal()
                    .source("PROGRESS")
                    .target("CLOSED")
                    .event("STOP");

            StateMachine<String, String> sm = builder.build();
            return sm;
        }

        public org.springframework.statemachine.listener.StateMachineListener<String, String> listener() {
            return new StateMachineListenerAdapter<String, String>() {
                @Override
                public void stateChanged(State<String, String> from,
                        State<String, String> to) {
                    System.out.println("State change to " + to.getId());
                }
            };
        }

        public static void main(String[] args) throws Exception {
            TestSMApplication tsm = new TestSMApplication();
            StateMachine<String, String> m1 = tsm.buildMachine1();
            m1.start();
            m1.sendEvent("RUN");
            m1.sendEvent("STOP");
        }
}