java异常处理

java异常处理,java,Java,我需要从数据库中检索数据,如果在数据库中找不到数据,我需要用java启动一个弹出窗口。我给这里的代码,我写处理,但无法处理它 String SectorCode = employerProfile.getSectorCode().getSectorTypeId(); String IndustrialCode = employerProfile.getIndustrialCode().getIndustryTypeId(); try{ if(SectorCode==null || Ind

我需要从数据库中检索数据,如果在数据库中找不到数据,我需要用java启动一个弹出窗口。我给这里的代码,我写处理,但无法处理它

String SectorCode = employerProfile.getSectorCode().getSectorTypeId();
String IndustrialCode = employerProfile.getIndustrialCode().getIndustryTypeId();
try{
    if(SectorCode==null || IndustrialCode==null){
        JOptionPane.showMessageDialog(null, "Record not found" );
    }
}catch(Exception ex){
    ex.printStackTrace();
}
请给我建议解决方案。。。
提前感谢

根据您使用的数据库和/或其设置方式,您可能还需要检查空字符串:

if(SectorCode==null || IndustrialCode==null || SectorCode.length() == 0 || IndustrialCode.length() == 0) {

如果未执行
If
块,则表示前两行中的方法调用可能发生异常。检查行
employerProfile.getSectorCode().getSectorTypeId()是否正确
employerProfile.getIndustrialCode().getIndustrialTypeId()在没有任何异常的情况下正确执行。

一种令人讨厌的方法…(您提到您遇到了空指针异常)


那么,实际问题是什么?它是否抛出异常?没有看到对话框?是,它引发异常。。。但当dB..@charan中不存在文件时,用户需要获得弹出窗口。。。当此调用引发异常时。。。可能是因为找不到记录。。。??M i…?正在引发什么类型的异常?空指针异常。。。在它抛出异常之前,我需要弹出一个窗口,因为并没有记录,并没有柴坦尼亚。前两行没有例外。但它无法执行if块。在java控制台的最后,它显示空指针异常,您是正确的。除非有异常,否则不会执行if块,因为if块在try catch块内。
String SectorCode = null;
String IndustrialCode = null;
try{
  SectorCode = employerProfile.getSectorCode().getSectorTypeId();
  IndustrialCode = employerProfile.getIndustrialCode().getIndustryTypeId();
  ...
}catch(Exception ex){
    if(SectorCode==null || IndustrialCode==null){
       JOptionPane.showMessageDialog(null, "Record not found" );
    }
}