Doc Holiday
Doc Holiday

Reputation: 10254

java.lang.NullPointerException ERROR

I'm getting this null error on this line but I'm doing an if check and setting an empty string if null. Am I doing this wrong?

java.lang.NullPointerException
at form.setSam((teamBean.getHead().getNone().getCode() == null) ? "" : teamBean.getHead().getNone().getSamCode().toString());   //SAM

Code:

public int show(Action action) throws Exception
{
    HttpServletRequest request = action.getRequest();

    String[] params;

    if (!isEmpty(params[0]))
    {

    String teamNumber= params[0];

    TeamBean teamBean = DAO.getTeamDAO().getTeamByNumber(Long.parseLong(teamNumber));

    Fake form = new fakeForm();

    form.setMoor(teamBean.getHeader().getMoor()); 
    form.setDoor(Double.toString(teamBean .getDoors()));    
    form.setURC(Double.toString(teamBean.getURCS()));   
    form.setUMC(Double.toString(teamBean.getUMCSt()));  
    form.setWeek(Long.toString(teamBean.getHead().getWeek().getnow())); //WEEK
   ERROR HERE -->>   form.setSam((teamBean.getHead().getNone().getCode() == null) ? "" : teamBean.getHead().getNone().getSamCode().toString()); //SAM

Upvotes: 0

Views: 263

Answers (2)

JB Nizet
JB Nizet

Reputation: 691765

You get it because teamBean.getHead().getNone() is null. And since you're calling getCode() on this null value, you get a NullPointerException.

Note that

form.setSam((teamBean.getHead().getNone().getCode() == null) ? "" : "");

could be rewritten as

form.setSac("");

(except you wouldn't have the NullPointerException)

Upvotes: 2

Mark Byers
Mark Byers

Reputation: 838266

For clarity, this is the expression that gives you the NullPointerException:

teamBean.getHead().getNone().getCode()

You aren't checking if getNone returns null.

Upvotes: 3

Related Questions