list = new ArrayList<TTopic>();
list = service.findAll();for (int i = 0; i < list.size(); i++)
{  ttopic = list.get(i);  
int j = ttopic.getExpert().getExpertid();
System.out.println("j="+ttopic.getExpert().getExpertid());
}
topic表和expert表是多对一的关系,如果topic表中的外键expertid为空的话,程序就会报空指针错误,但是topic表中的expertid确实有可能为空,我如何让程序继续运行呀。

解决方案 »

  1.   

    哪里报空指针?
    int j = ttopic.getExpert().getExpertid();这里吗?
    判断一下不就行了吗。int型是必须有值的,怎么可以付个空呢。
      

  2.   


    for (int i = 0; i < list.size(); i++)
    { ttopic = list.get(i);   
      if(ttopic.getExpert()!=null)
      {    
        int j = ttopic.getExpert().getExpertid();
         System.out.println("j="+ttopic.getExpert().getExpertid());
      }
    }