2

I have an entity:

@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String login;
private String password;
public User() {

}
public User(String login, String password) {
    this.login = login;
    this.password = password;
}

public String getLogin() {
    return login;
}

public String getPassword() {
    return password;
}
}

And i created an interface with annotation @Repository, which looks like:

@Repository
public interface UserRepository extends JpaRepository<User, Integer> {


}

So, i need to save in my database new users. But, if i use @Autowired UserRepository in my @Service class and call "save" method, it throws me an error 505 with description: HTTP Status 500 – Internal Server Error Type Exception Report

Message Cannot invoke "com.example.testservletjsp.repository.UserRepository.save(Object)" because "this.userRepository" is null

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

java.lang.NullPointerException: Cannot invoke "com.example.testservletjsp.repository.UserRepository.save(Object)" because "this.userRepository" is null com.example.testservletjsp.account.UserService.saveUser(UserService.java:28) com.example.testservletjsp.servlets.ExplorerServlet.doPost(ExplorerServlet.java:68) javax.servlet.http.HttpServlet.service(HttpServlet.java:682) javax.servlet.http.HttpServlet.service(HttpServlet.java:765) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) Note The full stack trace of the root cause is available in the server logs.

My pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.3</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>test-servlet-jsp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>test-servlet-jsp</name>
<description>test-servlet-jsp</description>
<properties>
    <java.version>11</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
    </dependency>


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.30</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.2.8.RELEASE</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.3.1</version>
        </plugin>

   </plugins>
 </build>
What should i do..?
  • how do you autowire your field? How did you instantiate your Service class? Do other Spring functionalities (autowire, @Value("") ) work for that Service? – Stultuske Sep 29 '22 at 13:53
  • how can i autowire my field? i marked it as @Autowired – Murrchalkina Sep 29 '22 at 13:56
  • My Service was marked as @Service and other methods working... – Murrchalkina Sep 29 '22 at 13:57
  • just because it is marked as @Service is no guarantee it's correctly instantiated – Stultuske Sep 29 '22 at 13:58
  • i shouldnt mark it as service ?? – Murrchalkina Sep 29 '22 at 13:59
  • I didn't say that. But you also have to make sure it's instantiated by the Spring mechanism, and so far we can't tell whether or not that's the case – Stultuske Sep 29 '22 at 14:00
  • So i checked, and if i dont mark service as @service, it doesnt work too.. – Murrchalkina Sep 29 '22 at 14:01
  • but other methods, which dont use repository - working – Murrchalkina Sep 29 '22 at 14:02
  • again, I didn't say you SHOULDN'T annotate it as a Service. Do those other methods use an autowired field? – Stultuske Sep 29 '22 at 14:09
  • The servlet isn't managed by Spring so no autowiring will take place. Also `@Repository` on that interface is useless. You are using Spring why not use an controller and service instead of a servlet. Or maybe it is but judging from the stacktrace you are doing a `new UserService()` in there. But as there is nothing Spring related in your stacktrace I doubt the servlet is even managed by spring. – M. Deinum Sep 29 '22 at 14:13

1 Answers1

-2

As I don't have the entirety of your project I cannot see if there are some miss-configured classes, but you can always visit this (https://spring.io/guides/gs/accessing-data-mysql/) spring guide that teaches you all the details you have to keep in mind in order to get that project working.

SanQuinteros
  • 344
  • 4
  • 14