开发者

java exception handling

I need to retrieve data from db, if no data found in dB i need to fire a popup window in java. im giving the code here which i write to handle but unable to handle it.

String SectorCode = employerProfile.getSectorCode().g开发者_如何学JAVAetSectorTypeId();
String IndustrialCode = employerProfile.getIndustrialCode().getIndustryTypeId();
try{
    if(SectorCode==null || IndustrialCode==null){
        JOptionPane.showMessageDialog(null, "Record not found" );
    }
}catch(Exception ex){
    ex.printStackTrace();
}

Please suggest me the solution... Thanks in advance


One nasty way of doing it...(you mentioned that you are getting null pointer exception)

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" );
    }
}


If the if block is not executed means an exception might be occuring in the method calls in the first 2 lines. Check if the lines employerProfile.getSectorCode().getSectorTypeId(); and employerProfile.getIndustrialCode().getIndustryTypeId(); are executed properly without any exceptions.


Depending on the database you use and/or how it is set up you may need to check for empty strings, too:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜