2

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.

9
  • You have not autowired service. Commented Mar 10, 2018 at 6:46
  • Still not working after autowired. Commented Mar 10, 2018 at 6:59
  • First try writing without any code inside run method Commented Mar 10, 2018 at 7:00
  • Are you sure you have pasted correct code? Because in the stacktrace, class name is OystercardApplication and in the code class name is cardApplication Commented Mar 10, 2018 at 7:02
  • Yes, I just changed the app name while writing here. @SanjayPatel it was working with code inside run method except when i call my journeyService method Commented Mar 10, 2018 at 8:04

2 Answers 2

3

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

@Autowired private JourneyService journeyService; 
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.