我在此行上收到此null错误,但是我正在做if检查并设置一个空字符串(如果为null)。我做错了吗?

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


码:

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

最佳答案

为了清楚起见,这是为您提供NullPointerException的表达式:

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


您无需检查getNone是否返回null。

09-25 18:36