-1

I got HttpStatus 500 with nested exception and java.lang.NullPointerException
im making a multicurrency converter i used to debug when it get here the error starts currencypojo.setAmount(BigDecimal.valueOf(Integer.parseInt((request.getParameter("txtAmount")))));

MultiCurrencyController.java

package com.multicurrency;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class MultiCurrencyController extends AbstractController{

    private MultiCurrencyPOJO currencypojo;
    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        currencypojo.setAmount(BigDecimal.valueOf(Integer.parseInt((request.getParameter("txtAmount")))));
        currencypojo.setCurrency(request.getParameter("currency"));
        return new ModelAndView ("home","CurrencyInfo",currencypojo);
    }

    public void multiCurrencyPOJO(MultiCurrencyPOJO currencypojo){
        this.currencypojo=currencypojo;
    }
}

MultiCurrencyPOJO.java

package com.multicurrency;

import java.math.BigDecimal;



public class MultiCurrencyPOJO {
    private String currency;
    private BigDecimal amount;

    public String getCurrency() {
        return currency;
    }
    public void setCurrency(String currency) {
        this.currency = currency;
    }
    public BigDecimal getAmount() {
        return amount;
    }
    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }

    public static BigDecimal getExchangeRate(String currency) {
        return null;

    }
}
pretzels04
  • 33
  • 2
  • 7
  • Your controller is flawed you are storing state in a singleton controller. What happens when 100 users issue a request to the `MultiCurrencyController`? Never keep state. – M. Deinum Jun 18 '14 at 08:59

2 Answers2

0

It is likely that the parameter txtAmount is not set on the HTTP request. In order to have more clarity on exactly where the error occurred, I would recommend splitting this into a separate line from the rest of the type conversion and explicitly checking that it's set.

For example:

String txtAmount = request.getParameter("txtAmount");
if (txtAmount == null) {
    throw new IllegalArgumentException("Expected HTTP request parameter txtAmount not present");
}

currencypojo.setAmount(BigDecimal.valueOf(Integer.parseInt((txtAmount));

(Also, I'd question the need for a BigDecimal in the first place - particularly since you're converting it from an Integer, but that's not the subject of your question.)

nullPainter
  • 2,053
  • 16
  • 34
0

Where is you Object for MultiCurrencyPOJO

private MultiCurrencyPOJO currencypojo;

Make sure when you have a instance of this object and its intialized before you do operation on them.

By default private objetcs are intialized to null, so any operation in null will end of NPE

Jayaram
  • 1,647
  • 17
  • 30
  • I already used this in my code before the @overide private MultiCurrencyPOJO currencypojo; and used this for instanciation public void multiCurrencyPOJO(MultiCurrencyPOJO currencypojo){ this.currencypojo=currencypojo; } – pretzels04 Jun 18 '14 at 06:38
  • Good point. That could very well also be null, unless there's something else explicitly setting it via `multiCurrencyPOJO`(); – nullPainter Jun 18 '14 at 06:39
  • Regardless, it's never a bad idea to add sanity checks in your code. Check that currencypojo is also not null. Good to be defensive and get a sensible error message rather than a NPE on a random line. – nullPainter Jun 18 '14 at 06:46
  • the error was been resolve when I change the instanciation to MultiCurrencyPOJO currencypojo=new MultiCurrencyPOJO(); – pretzels04 Jun 18 '14 at 06:47
  • That suggests the `multiCurrencyPOJO(MultiCurrencyPOJO currencypojo)` method wasn't being called. Try using a debugger to figure out what's going on, or perhaps start with a slightly simpler Java application which doesn't involve having to get up to speed with Spring MVC also. – nullPainter Jun 18 '14 at 06:50