It's not the first time I've used Spring BOOT, but it's not the first time on the Web. When trying to save an object to DB, this error appears. As if Spring Autowired hadn't inserted anything. Any help would be highly appreciated.
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.1)
2022-07-14 21:02:40.268 INFO 15252 --- [ main] r.k.ShipManager.ShipManagerApplication : Starting ShipManagerApplication using Java 17.0.3.1 on HOMEY with PID 15252 (D:\myWorkspace\ShipManager\target\classes started by Huawei BOB-WAI9 in D:\myWorkspace\ShipManager)
2022-07-14 21:02:40.270 INFO 15252 --- [ main] r.k.ShipManager.ShipManagerApplication : No active profile set, falling back to 1 default profile: "default"
2022-07-14 21:02:40.585 INFO 15252 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2022-07-14 21:02:40.623 INFO 15252 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 33 ms. Found 4 JPA repository interfaces.
2022-07-14 21:02:40.898 INFO 15252 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2022-07-14 21:02:40.937 INFO 15252 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.6.9.Final
2022-07-14 21:02:41.080 INFO 15252 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2022-07-14 21:02:41.153 INFO 15252 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2022-07-14 21:02:41.267 INFO 15252 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2022-07-14 21:02:41.286 INFO 15252 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL55Dialect
2022-07-14 21:02:41.849 INFO 15252 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2022-07-14 21:02:41.855 INFO 15252 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2022-07-14 21:02:42.137 INFO 15252 --- [ main] r.k.ShipManager.ShipManagerApplication : Started ShipManagerApplication in 2.156 seconds (JVM running for 2.621)
2022-07-14 21:02:47.211 INFO 15252 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-07-14 21:02:47.226 ERROR 15252 --- [ main] o.s.boot.SpringApplication : Application run failed
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:774) ~[spring-boot-2.7.1.jar:2.7.1]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:755) ~[spring-boot-2.7.1.jar:2.7.1]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.7.1.jar:2.7.1]
at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:164) ~[spring-boot-2.7.1.jar:2.7.1]
at ru.kazberov.ShipManager.ShipManagerApplication.main(ShipManagerApplication.java:28) ~[classes/:na]
Caused by: java.lang.NullPointerException: Cannot invoke "ru.kazberov.ShipManager.repo.ShipRepository.save(Object)" because "this.shipRepository" is null
at ru.kazberov.ShipManager.controllers.Controllers.menu23(Controllers.java:348) ~[classes/:na]
at ru.kazberov.ShipManager.controllers.Controllers.menu2(Controllers.java:244) ~[classes/:na]
at ru.kazberov.ShipManager.controllers.Controllers.menu(Controllers.java:57) ~[classes/:na]
at ru.kazberov.ShipManager.ShipManagerApplication.run(ShipManagerApplication.java:41) ~[classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:771) ~[spring-boot-2.7.1.jar:2.7.1]
... 4 common frames omitted
2022-07-14 21:02:47.228 INFO 15252 --- [ main] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2022-07-14 21:02:47.229 INFO 15252 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2022-07-14 21:02:47.243 INFO 15252 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
My main class:
@SpringBootApplication
public class ShipManagerApplication implements CommandLineRunner {
public static WithWindow window; // the object that displays the window
public static void main(String[] args) {
ConfigurableApplicationContext context = new SpringApplicationBuilder(ShipManagerApplication.class).headless(false).run(args);
}
@Override
public void run(String... args) throws Exception {
WithWindow withOkno = new WithWindow(); // creates a JFrame
window = withOkno; // saves to header
Controllers controllers = new Controllers(withOkno);
controllers.menu(); // displays the menu
}
}
My controller class:
@Controller
public class Controllers {
@Autowired
private ShipRepository shipRepository;
public Controllers() {}
public Controllers(WithWindow window, SharedDatabase base) {
this.window = window;
this.base = base;
}
public void menu23() {
Ship ship = new Ship();
this.shipRepository.save(ship);
}
My model class:
@Entity
@Table(name="ships")
public class Ship {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name; // ship's name
@OneToMany(mappedBy="ship", fetch=FetchType.LAZY, cascade=CascadeType.ALL)
private List<Room> rooms;
@OneToMany(mappedBy="ship", fetch=FetchType.EAGER, cascade=CascadeType.ALL)
private List<Passenger> passengers;
@OneToMany(mappedBy="ship", fetch=FetchType.LAZY, cascade=CascadeType.ALL)
private List<Staff> staff;
private Date startDate;
@Transient
private SharedDatabase base;
@Transient
private WithWindow window;
public Ship(){}
}
And repository of the model:
@Repository
public interface ShipRepository extends CrudRepository<Ship, Long> {
}
I hope for your help
CodePudding user response:
Since you created the controller yourself with the "new" keyword instead of defining a bean and injecting it, spring does not know about your controller instance and that you expected it to inject a dependency. A detailed explanation can be found in this thread.
CodePudding user response:
It tells you what the exception is... Caused by:
java.lang.NullPointerException: Cannot invoke "ru.kazberov.ShipManager.repo.ShipRepository.save(Object)" because "this.shipRepository" is null