2010-12-15 173 views
-2

我需要从数据库检索数据,如果没有在dB中找到数据我需要在java中激发一个弹出窗口。即时通讯给我在这里写的代码来处理,但无法处理它。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(); 
} 

请给我建议的解决方案... 在此先感谢

+0

那么,什么是真正的问题?它抛出异常吗?没有看到对话框? – 2010-12-15 06:32:34

+0

是它的抛出异常...但用户需要弹出窗口,当没有文件存在dB .. – charan 2010-12-15 06:36:23

+0

@ charan ...当这个调用抛出异常...可能是incase没有找到记录.. ?? M我...? – water 2010-12-15 06:38:16

回答

1

一个这样做的肮脏的方式...(你提到,你得到空指针异常)

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

取决于您使用和/或它是如何设置您可能需要检查空字符串在数据库上,太:

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

嗨sjngm ...它不工作。我没有得到任何弹出... – charan 2010-12-15 06:37:55

1

如果if块未执行意味着可能在前两行的方法调用中发生异常。检查行employerProfile.getSectorCode().getSectorTypeId();employerProfile.getIndustrialCode().getIndustryTypeId();是否正确执行,没有任何例外。

+0

没有柴坦尼亚。在第2行,没有例外。但如果阻止则无法执行。在java控制台最后显示空指针异常 – charan 2010-12-15 06:53:49

+0

你是对的。如果块不会被执行,除非有异常,因为如果块在try-catch块内。 – 2010-12-20 10:38:12