Kyle Stan
Kyle Stan

Reputation: 11

java.lang.NullPointerException: null when try to inject spring container in controller

hello guys i have a noob question .i try to build a simple login page. i do have @resource the jparepository at my controller .but log said nullpointexception. i have try main start with getbean() to reach user in my sql ,it can be done. but why cant i reach it in controller. sorry its my first time ask so that forgive my formatted form.

@Controller
public class indexController {
    
    @Resource
    private jpaRepository2 jpa2;
    
    @GetMapping("/index")
    public  String index()
    {
        return "index";
    }

    @RequestMapping("/login")
    public  String login(@RequestParam("username") String username,@RequestParam("password") String password)
    {
        user user2 = jpa2.findByName(username);
        if(password == user2.getPassword())
        {
            return "index";
        }else
        {
            return "logged";
        }
    }

**@Repository
public interface jpaRepository2 extends JpaRepository<user,Integer>{

    @Query(value="select * from user where username = ?",nativeQuery=true)
     user  findByName( String name);
    
}**

entity
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="user")
public class user {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private Integer id;
    @Column(name="username")
    private String username;
    @Column(name="password")
    private String password;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "user [username=" + username + ", password=" + password + "]";
    }
    
}

the following is log:......

java.lang.NullPointerException: null
at com.mickey.controller.indexController.login(indexController.java:37) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_281]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_281]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_281]

Upvotes: 0

Views: 295

Answers (2)

matthung
matthung

Reputation: 45

Use @Autowired to annotate jpaRepository2 to replace @Resource annotation.

@Resource inject bean default is match by field name, not by type, or use @Resource(name = "jpaRepository2").

--Fix--

It looks like the problem is not caused by above. I doubt your indexController is not managed by Spring IoC container, make sure your indexController instance is not create by new indexController();

Upvotes: 1

Zaur
Zaur

Reputation: 226

Hi Kyle please change @Controller anotation @RestController

Upvotes: 0

Related Questions