使用springboot JPA查询数据库,报错java.lang.NullPointerException

Provinces.java

package com.spring.inter.bean;  
  
import javax.persistence.Column;  
import javax.persistence.Entity;  
import javax.persistence.Id;  
import javax.persistence.Table;  
//     @Entity   对实体注释。任何Hibernate映射对象都要有这个注释  
//     @Id   声明此属性为主键。该属性值可以通过应该自身创建  
//实体类  
@Entity  
@Table(name = "provinces")  
public class Provinces {  
    @Id  
  private Integer id;  
  
  @Column(name = "provinceid")  
    private Integer provinceid;  
  
  @Column(name="province")  
    private String province;  
  
  //需要声明无参数的构造函数  
  public Provinces(){}  
  
    public Provinces(Integer id, Integer provinceid, String province) {  
        this.id = id;  
 this.provinceid = provinceid;  
 this.province = province;  
  }  
  
    public Integer getId() {  
        return id;  
  }  
  
    public void setId(Integer id) {  
        this.id = id;  
  }  
  
    public Integer getProvinceid() {  
        return provinceid;  
  }  
  
    public void setProvinceid(Integer provinceid) {  
        this.provinceid = provinceid;  
  }  
  
    public String getProvince() {  
        return province;  
  }  
  
    public void setProvince(String province) {  
        this.province = province;  
  }  
}
package com.spring.inter.controller;  
  
import com.spring.inter.bean.Provinces;  
import com.spring.inter.repository.ProvincesRepository;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.web.bind.annotation.GetMapping;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.ResponseBody;  
import org.springframework.web.bind.annotation.RestController;  
  
import java.util.ArrayList;  
import java.util.List;  
  
@RestController  
  
public class ProvincesController {  
    @Autowired(required = false)  
    private ProvincesRepository provincesRepository;  
  
//    @RequestMapping("queryAll")  
  @RequestMapping("/provinces")  
    public List<Provinces> queryAll(){  
        List<Provinces> list = new ArrayList<Provinces>();  
  list = provincesRepository.findAll();  
 return list;  
  }  
  
}
package com.spring.inter.repository;  
  
  
import com.spring.inter.bean.Provinces;  
import com.spring.inter.bean.LoginBean;  
import org.springframework.data.jpa.repository.Query;  
import org.springframework.data.repository.CrudRepository;  
import org.springframework.data.jpa.repository.JpaRepository;  
import org.springframework.data.jpa.repository.Query;  
import org.springframework.data.repository.query.Param;  
  
import java.util.List;  
  
public interface ProvincesRepository extends JpaRepository<Provinces, Integer>{  
    /\*  
 \* 查询全部 \* \*/  @Query("select t from Provinces t")  
    List<Provinces> findAll();  
  
}
package com.spring.inter.starter;  
  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
import org.springframework.context.annotation.ComponentScan;  
  
/\*\*  
 \* Spring boot 入口启动程序,sprint boot内置了tomcat \* @author Administrator  
 \*/  
@ComponentScan(basePackages\= {"com.spring.inter.controller"})//扫描组件  
@SpringBootApplication  
public class Application {  
    public static void main(String\[\] args) {  
        SpringApplication.run(Application.class,args);  
  }  
}

代码github地址

https://github.com/wohuifude1...

501.png

阅读 7.2k
1 个回答

很明显它告诉你了,错误在ProvincesController的第24行,而24行的代码是provincesRepository.findAll()
因此,错误一定是provincesRepository是null,那就看看provincesRepository为什么是null.
那就看看provincesRepository是怎么创建的,发现你是在上面autowired的,那就去看看provincesRepository这个类的定义。
原来这个类是一个repository,但是为什么这个repository没有annotation呢?
因此,你需要把@Repository给加上,像下面的代码,那么provincesRepository就不会为null了。

@Repository
public interface ProvincesRepository extends JpaRepository<Provinces, Integer>{  
    /\*  
 \* 查询全部 \* \*/  @Query("select t from Provinces t")  
    List<Provinces> findAll();  
  
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题