java - 带有返回语句的奇怪警告
问题描述
public ResultSet getParticularEmpoyee(int empId){
PreparedStatement getParticular;
ResultSet result;
try{
getParticular= conn.prepareStatement("SELECT * FROM "+DatabaseHelper.TABLE_NAME+" WHERE EMP_ID = ? ");
getParticular.setInt(1,empId);
result = getParticular.executeQuery();
// return result;
}catch(SQLException e){
JOptionPane.showMessageDialog(null, e);
}
return result; // this gives the warning that result may not be initialized :)
}
以上是我出于某种目的的代码,我遇到了问题,我不明白为什么会这样:)这个警告是否会因为它只是一个警告而产生一些严重的后果?如果这是一个问题,那么请告诉我避免这种情况的方法!如果我将 return 语句放在 try 块中,则会出现错误“在最后一个花括号之前缺少返回语句”
解决方案
您的 ResultSet 未初始化,因此如果 try catch 中有异常,则可能未定义。初始化为null更正确,更清晰。
推荐阅读
- javascript - 从詹金斯下载 zip
- javascript - TypeError:无法读取未定义的属性“地图”(React.js)
- jsonparser - 如何解析包含“。”的json 以其关键名称
- python - SyntaxError:Ubuntu Python 3 终端上的语法无效
- c++ - 如何从 Qt QWebEngineView 获取 HTTP 状态码?
- java - 使用 JMockit 模拟类中的 Arrays.sort() 方法
- r - 如何将具有某些值的元素保留在列表中?
- angular - 刷新页面导致 404 错误-AWS-Angular 6
- assembly - ARM64 固有的 x86_64 点向量积
- sql - 从另一个表创建临时表,包括配置单元中的分区列