首页 > 解决方案 > 带有返回语句的奇怪警告

问题描述

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 块中,则会出现错误“在最后一个花括号之前缺少返回语句”

标签: java

解决方案


您的 ResultSet 未初始化,因此如果 try catch 中有异常,则可能未定义。初始化为null更正确,更清晰。


推荐阅读