0

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
2
  • What's the point of (jobPlanBean.getHeader().getNsn().getStoresAccountCode() == null) ? "" : "".... You always set it to "", might as well just do setSac("") -- and yes @JB Nizet, I hadn't seen the "ERROR HERE", deleted my first comment when I saw it ;) Commented Oct 2, 2011 at 17:07
  • sorry....I wrote it that way for testing purposes....this is how its supposed to look like this: Commented Oct 2, 2011 at 17:12

2 Answers 2

3

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

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

You aren't checking if getNone returns null.

Sign up to request clarification or add additional context in comments.

Comments

2

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.