-3

As a simple example, there's a class called 'Car'. There's a method in it that takes a String variable called 'result':

public class Car {

    public void move(String result) {
        System.out.println(result);
    }

}

Then there's the controller that uses the car class:

@Controller
public class carController {

    private Car myCar;

    myCar.move("Vroom vroom");
}

However, when it tries to pass in "Vroom vroom", it crashes and gives the following:

Caused by: java.lang.NullPointerException: null

I debugged it and found it crashes while executing the line that calls the method with the value that needs to be passed in.

It seems you can't pass variables from a Spring Boot controller into a model class.

Here's the actual error message from my project:

    Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-04-15 16:54:46.331 ERROR 7388 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:787) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:768) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:322) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at com.demo.coronavirus.CovidUkApplication.main(CovidUkApplication.java:13) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_241]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_241]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_241]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_241]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.2.6.RELEASE.jar:2.2.6.RELEASE]
Caused by: java.lang.NullPointerException: null
    at com.demo.coronavirus.controller.DisplayCasesController.lambda$0(DisplayCasesController.java:35) ~[classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:784) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    ... 10 common frames omitted
MrLem
  • 19
  • 4

1 Answers1

-1

First of all, your Car class has not been instantiated. You need to instantiate a Car object to do your task by

Car myCar=new Car();

Secondly, you dont have any vroom method for your Car class, instead, move(String str) method. So, try this way to achieve what you want:

Car myCar=new Car();
myCar.move("Your String here");
user404
  • 1,934
  • 1
  • 16
  • 32