简体   繁体   中英

ERROR while starting the Spring Boot application

I am new to spring Boot and spring when i try to run the following code i am getting NullPointerException .

@SpringBootApplication
public class cardApplication implements CommandLineRunner{

private JourneyService journeyService;

public static void main(String[] args) {
    SpringApplication.run(cardApplication.class, args);
}

@Override
public void run(String... args) throws Exception {
    Scanner scan = new Scanner(System.in);
    String name = scan.next();
    Double balance = scan.nextDouble();
    Card card = new Card(name, balance);

     // Travel begins


    Barrier checkIntoTube = new Barrier(card, Direction.IN, TravelMode.TUBE, Stations.Holborn);
    Barrier checkOutTube = new Barrier(card, Direction.OUT, TravelMode.TUBE, Stations.EarlsCourt);
    journeyService.startTubeJourney(checkIntoTube);

    journeyService.endTubeJourney(checkOutTube);

}

}

And My Exception trace looks like

2018-03-10 11:14:44.016  INFO 20240 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-03-10 11:14:44.039 ERROR 20240 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:793) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:774) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:335) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1234) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at com.alefedu.oyestercard.travel.OystercardApplication.main(OystercardApplication.java:17) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_73]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_73]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_73]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_73]
    at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:496) [spring-boot-maven-plugin-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_73]
Caused by: java.lang.NullPointerException: null
    at com.alefedu.oyestercard.travel.OystercardApplication.run(OystercardApplication.java:34) [classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:790) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    ... 11 common frames omitted

2018-03-10 11:14:44.043  INFO 20240 --- [           main] ConfigServletWebServerApplicationContext : Closing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@5599596c: startup date [Sat Mar 10 11:14:27 IST 2018]; root of context hierarchy
2018-03-10 11:14:44.050  INFO 20240 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
[WARNING] 
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:496)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:793)
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:774)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:335)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1234)
    at com.alefedu.oyestercard.travel.OystercardApplication.main(OystercardApplication.java:17)
    ... 6 more
Caused by: java.lang.NullPointerException
    at com.alefedu.oyestercard.travel.OystercardApplication.run(OystercardApplication.java:34)
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:790)
    ... 11 more
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

I do not understand where i am doing wrong.

I had the same issue. I have autowired the class which I have invoked. It worked for me.

@Autowired
private JourneyService journeyService;

In order for something to be injected by Springs IoC container it must be defined as a bean or be an internal Spring object such as ApplicationContext .

So, define a new XML configuration file or use Java configuration to define the service as a bean and then inject it with @Autowired or @Inject .

Please refer to the Spring documentation for more details.

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