【问题标题】:NullPointerException on CreateMaturityDate() [duplicate]CreateMaturityDate() 上的 NullPointerException [重复]
【发布时间】:2021-12-02 21:22:34
【问题描述】:

我正在调用 createInvestment 方法,它里面是 calculateMaturityDate 方法。当它到达服务类的 calculateMaturityDate 时,我不断收到 Nullpointerexception。

服务类


@Service
public class InvestmentService {
    
    private static int nextAccountNumber = 2005070006;
    
    @Autowired  
    private InvestmentRepository investmentRepository;
    


      public Investment CreateInvestment(Investment investment)
       {
        investment.setAccountNumber(nextAccountNumber());
        investment.setStatus(InvestmentStatus.pending);
        investment.setStartDate(LocalDate.now());
        investment.setMaturityDate(calculateMaturityDate());
        investment.setCurrentInterest(calculateCurrentInterest());
        investment.setMaturityInterest(calculateInterestAtMaturity());
        return investmentRepository.save(investment);
       }
    
     public LocalDate calculateMaturityDate() {
      Investment investment = new Investment();
      {
           if(investment.getTenure().equalsIgnoreCase("365"))
            { investment.setMaturityDate(investment.getStartDate().plusDays(365));
            } else if (investment.getTenure().equalsIgnoreCase("120")) 
            { investment.setMaturityDate(investment.getStartDate().plusDays(120));}
           else if (investment.getTenure().equalsIgnoreCase("90")) 
            {investment.setMaturityDate(investment.getStartDate().plusDays(90));}
           else if (investment.getTenure().equalsIgnoreCase("30")) 
            {investment.setMaturityDate(investment.getStartDate().plusDays(30));}
         }
       return investment.getMaturityDate();}

这是在 CreateInvestment 方法中创建其实例的模型类

public class Investment 

   {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column(name = "AccountNumber", updatable = false, nullable = false)
    private int accountNumber;
    private String category;
    private BigDecimal principal;
    //private BigDecimal netInvestmentAmount;
    private BigDecimal currentInterest;
    private BigDecimal maturityInterest;
    private String tenure;
    private String marketer;
    private String investmentPackage;
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate startDate;
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate maturityDate;
    private int rate;
    private final double wht = 10/100;
    private String whtStatus;
    private final double  preLiquidationCharges = 25/100;
    @Enumerated(EnumType.STRING)
    private InvestmentStatus status;
    
    @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
    @ManyToOne(targetEntity = Customer.class, 
    cascade = CascadeType.ALL, fetch = FetchType.LAZY )
    @JoinColumn(name="customer_id") 
    private Customer customer ;

这是堆栈跟踪

2021-10-14 06:18:30.764 ERROR 11592 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause

java.lang.NullPointerException: null
    at com.bethsaida.org.service.InvestmentService.calculateMaturityDate(InvestmentService.java:52) ~[classes/:na]
    at com.bethsaida.org.service.InvestmentService.CreateInvestment(InvestmentService.java:43) ~[classes/:na]
    at com.bethsaida.org.controllers.InvestmentController.CreateInvestment(InvestmentController.java:64) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197) ~[spring-web-5.3.9.jar:5.3.9]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141) ~[spring-web-5.3.9.jar:5.3.9]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) ~[spring-webmvc-5.3.9.jar:5.3.9]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.3.9.jar:5.3.9]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.9.jar:5.3.9]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.9.jar:5.3.9]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1064) ~[spring-webmvc-5.3.9.jar:5.3.9]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) ~[spring-webmvc-5.3.9.jar:5.3.9]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.9.jar:5.3.9]
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.3.9.jar:5.3.9]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:681) ~[tomcat-embed-core-9.0.50.jar:4.0.FR]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.9.jar:5.3.9]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) ~[tomcat-embed-core-9.0.50.jar:4.0.FR]

【问题讨论】:

  • 哪个变量为空?
  • 没有CreateMaturityDate() - 你指的是哪种方法?发布堆栈跟踪怎么样(在询问异常时应该始终这样做)。
  • 首先stacktrace在哪里?其次,您不应该向我们询问 NPE,因为没有什么比您使用调试器识别它更容易的了
  • 顺便说一句,为什么calculateMaturityDate() 创建一个Investment 然后被丢弃?这看起来像一个尴尬的设计。您有几个 if 块来检查 investment.getTenure() 的值 - 您是否希望为使用 new Investment() 创建的普通投资返回不同的值?
  • 我已经编辑并添加了堆栈跟踪。 @Thomas calculateMaturityDate() 方法是在 createInvestment 方法中调用的方法。

标签: java spring spring-boot spring-mvc nullpointerexception


【解决方案1】:

在此代码中,Investmentinvestment = new Investment(); 行将创建一个新的 Investment 对象,其任期值为 null,因为您没有初始化变量。

在if条件下if (investment.getTenure().equalsIgnoreCase("365"))等价于if (null.equalsIgnoreCase("365 "))。这导致了 nullPointerException。

另外,您可能不需要这个额外的包装括号。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-10
    • 2019-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-27
    • 2014-07-12
    • 1970-01-01
    相关资源
    最近更新 更多