简体   繁体   中英

NullPointerException when binding ConfigurationProperties (Spring Boot)

I am struggling with some code and I don't have a clue why... Here is my code:

@SpringBootApplication
@EnableConfigurationProperties(WorkflowRootProperties.class)
@ComponentScan
@ActiveProfiles("test")
public class TestApplication {
    public static void main(String[] args) {    
        SpringApplication.run(TestApplication.class, args);
    }
}

And my properties bean

@ConfigurationProperties("workflow")
public class WorkflowRootProperties {
    private List<Datacenter> datacenter = new ArrayList<>();

    public List<Datacenter> getDatacenter() {
        return datacenter;
    }

    public void setDatacenter(List<Datacenter> datacenter) {
        this.datacenter = datacenter;
    }
}

Which references another bean

public class EnvProperties {
    public static class Datacenter {
        private List<Env> env = new ArrayList<>();

        public List<Env> getEnv() {
            return env;
        }

        public void setEnv(List<Env> env) {
            this.env = env;
        }
    }

    public static class Env {
        private List<Instance> instance = new ArrayList<>();

        public List<Instance> getInstance() {
            return instance;
        }

        public void setInstance(List<Instance> instance) {
            this.instance = instance;
        }
    }

    public static class Instance {
        private String port;

        public String getPort() {
            return port;
        }

        public void setPort(String port) {
            this.port= port;
        }
    }
}

Finally, my properties are

workflow.datacenter[1].env[1].instance[1].port=8080
workflow.datacenter[2].env[1].instance[1].port=8082
workflow.datacenter[1].env[1].instance[2].port=8080

My Error is at init, I get a NPE which I cannot understand.

java.lang.IllegalStateException: Failed to load ApplicationContext...

Caused by: org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'workflow.datacenter' to java.util.List at org.springframework.boot.context.properties.bind.Binder.handleBindError(Binder.java:250) at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:226) at org.springframework.boot.context.properties.bind.Binder.lambda$bindBean$4(Binder.java:331) at org.springframework.boot.context.properties.bind.JavaBeanBinder.bind(JavaBeanBinder.java:72) at org.springframework.boot.context.properties.bind.JavaBeanBinder.bind(JavaBeanBinder.java:61) at org.springframework.boot.context.properties.bind.JavaBeanBinder.bind(JavaBeanBinder.java:53)...

Caused by: java.lang.NullPointerException: null at java.util.TreeMap.compare(TreeMap.java:1294) at java.util.TreeMap.put(TreeMap.java:538) at java.util.TreeSet.add(TreeSet.java:255) at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169) at java.util.LinkedList$LLSpliterator.forEachRemaining(LinkedList.java:1235) at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580) at java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:270) at java.util.Iterator.forEachRemaining(Iterator.java:116) at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) at java.util.stream.ReferencePipeline.collect(ReferencePipeline.j ava:499) at org.springframework.boot.context.properties.bind.IndexedElementsBinder.assertNoUnboundChildren(IndexedElementsBinder.java:137)...

Could you please help me?

Seems to work if you start indexing from 0:

workflow.datacenter[0].env[0].instance[0].port=8080
workflow.datacenter[1].env[0].instance[0].port=8082
workflow.datacenter[0].env[0].instance[1].port=8080

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