Java JPA存储库引发空指针异常

Java JPA存储库引发空指针异常,java,spring-boot,rest,jpa,Java,Spring Boot,Rest,Jpa,我对spring boot和jpa相当陌生。我正在为我的学习目的做一个小项目 实体类 package com.jranjanacademy.currencyexchangeservice.service; import java.math.BigDecimal; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class

我对spring boot和jpa相当陌生。我正在为我的学习目的做一个小项目

实体类

package com.jranjanacademy.currencyexchangeservice.service;

import java.math.BigDecimal;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class ExchangeService {

    @Id
    private long id;

    @Column(name="currency_from")
    private String from;

    @Column(name="currency_to")
    private String to;

    private BigDecimal conversionMultiple;

    private int port;

    // == Constructors ==

    // == No arg Constructors ==
      public ExchangeService() {

      }

    // == Parametrised Constructors ==
    public ExchangeService(long id, String from, String to, BigDecimal conversionMultiple) {
        super();
        this.id = id;
        this.from = from;
        this.to = to;
        this.conversionMultiple = conversionMultiple;
    }

    /**
     * @return the id
     */
    public long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(long id) {
        this.id = id;
    }

    /**
     * @return the from
     */
    public String getFrom() {
        return from;
    }

    /**
     * @param from the from to set
     */
    public void setFrom(String from) {
        this.from = from;
    }

    /**
     * @return the to
     */
    public String getTo() {
        return to;
    }

    /**
     * @param to the to to set
     */
    public void setTo(String to) {
        this.to = to;
    }

    /**
     * @return the conversionMultiple
     */
    public BigDecimal getConversionMultiple() {
        return conversionMultiple;
    }

    /**
     * @param conversionMultiple the conversionMultiple to set
     */
    public void setConversionMultiple(BigDecimal conversionMultiple) {
        this.conversionMultiple = conversionMultiple;
    }

    /**
     * @return the port
     */
    public int getPort() {
        return port;
    }

    /**
     * @param port the port to set
     */
    public void setPort(int port) {
        this.port = port;
    }
}
控制器类:

package com.jranjanacademy.currencyexchangeservice.controller;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.core.env.Environment;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;

    import com.jranjanacademy.currencyexchangeservice.service.ExchangeService;
    import com.jranjanacademy.currencyexchangeservice.service.ExchangeServiceRepository;

    @RestController
    public class ExchangeServiceController {

        @Autowired
        private Environment environment;

        @Autowired
        private ExchangeServiceRepository exchangeServiceRepository;

        @GetMapping("/currency-exchange/from/{from}/to/{to}")
        public ExchangeService retrieveExchangeValue(@PathVariable String from, @PathVariable String to){
            ExchangeService exchangeValue =
                    exchangeServiceRepository.findByFromAndTo(from, to);
        exchangeValue.setPort(
                    Integer.parseInt(environment.getProperty("local.server.port")));
        return exchangeValue;
        }
    }
JPARepository

package com.jranjanacademy.currencyexchangeservice.service;

import org.springframework.data.jpa.repository.JpaRepository;

public interface ExchangeServiceRepository extends JpaRepository<ExchangeService, Long>{

    ExchangeService findByFromAndTo(String from, String to);
}
我尝试访问url“”时遇到以下异常

应用程序属性

spring.application.name=currency-exchange-service
server.port=8000
spring.jpa.show-sql=true
spring.h2.console.enabled=true

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

有线索吗?

我很惊讶您在运行开始时没有收到异常。
ExchangeServiceRepository
引用为空。您需要将
@Repository
注释添加到类型
ExchangeServiceRepository
。Spring可能无法自动连接这个Bean,因为它没有被注释为存储库(DAO),其核心是一个
@组件

@存储库
公共接口ExchangeServiceRepository扩展了JpaRepository{
ExchangeService findByFromAndTo(字符串从,字符串到);
}

我看不到您的主类,所以请确保调用
@EnableJpaRepositories
,以便Spring Boot可以获取您的存储库

我很惊讶您在运行时开始时没有收到异常。
ExchangeServiceRepository
引用为空。您需要将
@Repository
注释添加到类型
ExchangeServiceRepository
。Spring可能无法自动连接这个Bean,因为它没有被注释为存储库(DAO),其核心是一个
@组件

@存储库
公共接口ExchangeServiceRepository扩展了JpaRepository{
ExchangeService findByFromAndTo(字符串从,字符串到);
}
我看不到您的主类,所以请确保调用
@EnableJpaRepositories
,以便Spring Boot可以获取您的存储库

尝试使用

@Value("${local.server.port}")
int port;
而不是

environment.getProperty("local.server.port")
似乎问题就在那里。 为了反驳或确认这一点,只需删除带有
exchangeValue.setPort(Integer.parseInt(environment.getProperty(“local.server.port”))的行

尝试使用

@Value("${local.server.port}")
int port;
而不是

environment.getProperty("local.server.port")
似乎问题就在那里。
为了反驳或确认这一点,只需删除带有
exchangeValue.setPort(Integer.parseInt(environment.getProperty(“local.server.port”))的行

好的,从这个问题我可以看到两件事

  • 插入的项目是
  • 请求
    http://localhost:8000/currency-兑换/自/印度卢比/至/美元
  • 因此,我怀疑在下一行中,您一定得到了空值,因为表中不存在
    FROM:INR
    to
    to:USD

    ExchangeService exchangeValue=exchangeServiceRepository.findByFromAndTo(从,到);
    
    所以解决办法是

    • 在处理前检查
      exchangeValue
      是否为
      null
    (或)

    • 将员工返回为
      可选的
      数据,请参阅(为了获得更好的代码质量,上面的sol更简单)

    好的,从这个问题我可以看到两件事

  • 插入的项目是
  • 请求
    http://localhost:8000/currency-兑换/自/印度卢比/至/美元
  • 因此,我怀疑在下一行中,您一定得到了空值,因为表中不存在
    FROM:INR
    to
    to:USD

    ExchangeService exchangeValue=exchangeServiceRepository.findByFromAndTo(从,到);
    
    所以解决办法是

    • 在处理前检查
      exchangeValue
      是否为
      null
    (或)

    • 将员工返回为
      可选的
      数据,请参阅(为了获得更好的代码质量,上面的sol更简单)


    两个问题:数据是否已填入表中?你能证实这一点吗?另外,您的application.properties是什么样子的?您是否可以尝试将@Repository注释添加到存储库界面?是的,我可以在h2控制台中看到数据。application.properties如下所示。spring.application.name=currency exchange service server.port=8000 spring.jpa.show sql=true spring.h2.console.enabled=true spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.drivercassname=org.h2.Driver spring.datasource.username=sa spring.datasource.password=spring.jpa.database platform=org.hibernate.dial。H2Dialect@TharakaDevinda我尝试将存储库注释放到界面中,但仍然没有乐趣。我看到了同样的错误。请在控制器中设置断点,检查哪些变量为空,然后告诉我们。两个问题:数据是否已填充到表中?你能证实这一点吗?另外,您的application.properties是什么样子的?您是否可以尝试将@Repository注释添加到存储库界面?是的,我可以在h2控制台中看到数据。application.properties如下所示。spring.application.name=currency exchange service server.port=8000 spring.jpa.show sql=true spring.h2.console.enabled=true spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.drivercassname=org.h2.Driver spring.datasource.username=sa spring.datasource.password=spring.jpa.database platform=org.hibernate.dial。H2Dialect@TharakaDevinda我尝试将存储库注释放到界面中,但仍然没有乐趣。我看到了同样的错误。请在控制器中设置断点,检查哪些变量为null,然后告诉我们。我尝试在接口中添加存储库注释,但仍然没有乐趣。我看到了同样的错误我也有最初的怀疑。但他似乎不是这样。既然有JpaRepository,那真的需要吗?我很肯定@Repository不是必需的,因为我可以使用JpaRepository的defaut方法,但不确定当我尝试使用“ExchangeService findByFromAndTo(String from,String to);”时会发生什么。这非常奇怪。您是否在应用程序入口点类中调用了注释@EnableJpaRepositories?@Json您所说的应用程序入口点类是什么意思?你是说主课吗?如果是,我也尝试在主类中添加EnableJpaRepositories注释,但仍然看到相同的例外我尝试在int中添加Repository注释
    id,  | currency_from | currency_to | conversion_multiple |port
    1001 |    'USD'      |    'INR'    |     75.             |0
    1002 |    'AUD'      |    'INR'    |     42              |0
    1003 |    'EUR'      |    'INR'    |     65              |0
    1004 |    'POUND'    |    'INR'    |     51              |0
    
    From : INR, TO : USD