I am calling the createInvestment method and Inside it is the calculateMaturityDate Method. I keep getting a Nullpointerexception when it gets to the calculateMaturityDate of the service class .
Service Class
@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();}
This is the Model Class whose Instance is being created in the CreateInvestment Method
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 ;
This is the stack trace
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]