当我尝试运行以下代码时,我是Spring和Spring的新手,我得到了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);
}
}
我的异常跟踪看起来像
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] ------------------------------------------------------------------------
我不明白我在哪里做错了。
答案 0 :(得分:1)
我有同样的问题。我已经自动启动了我调用过的类。它对我有用。
@Autowired
private JourneyService journeyService;