How to avoid setting a variable in a try statement

My problem is that I have to set the variable in the try statement, otherwise I get a compilation error.

Later I need to use this variable, but now it is out of scope. I am initializing a variable outside of the try statement and setting it to null, I thought it might be accessible from the outside, but I still get NullPointerException

.

Below is the code and many of them are taken out to make it easier to read. I know this is bad code, but I'm new to Servlets and just wanted to see how it works with all the moving parts that do what they suggest.

I created another class that calls createDocs (...) and passes in the required parameters and it works fine. So I am getting interested to know why when I call rs.getString("name")

I get NullPointerException

, since this is exactly what I am doing from another class (used for the main method for convenience) and it works as expected.

This variable is the ResultSet "rs" variable -

public class AgReportServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public AgReportServlet() {
        super();
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ResultSet rs = null;
        try {
            rs = docs.getDocs(con, start, end, zone, locality);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        response.setContentType("text/xml");
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" +
                out.println(
                        "<table border=\"0\" cellspacing=\"0\" cellpadding=\"6\">\n");

        // I have a resultset object need to iterate through it to display the file names

        try {
            while (rs.next()) { // page through the result set

                out.println(
                        "  <tr>\n" +
                                "    <td>: " + rs.getString("name") + "</td>\n" +
                                "  </tr>\n"
                );
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        out.println(
                "</table></body>\n" +
                        "</html>"
        );

        out.flush();
        out.close();
    }
}

      

0


a source to share


4 answers


Your problem is that if this statement:

rs = docs.getDocs(con, start, end, zone, locality);

      



throws an exception, then the rs value doesn't matter null

. So what I would do is move the loop inside one try-catch block. Also, you can check if it's all down to zero before trying to use it.

Setting the value to null outside of the try-catch block is not bad code. This is what you need to do if you want the rs variable outside the try block (and that includes inside one of the catch clauses). Probably your rs while loop should be in one try block.

+5


a source


The way rs variable and first try / catch block are declared is fine.



That's right, after the first try / catch, you need to remember that rs will still be null if an error occurs on the first try / catch. So just make sure you check rs for null before trying to access it.

+4


a source


Why don't you just add

if(rs != null)

      

before

while(rs.next())

      

This should help (as far as I understand)

+2


a source


Thanks everyone for your help. First you helped me determine that there must be some kind of exception. When I looked at the Tomcat console, I could see that this is a class error for the frame for the MySQL connector. I included this in my project, but not within the project itself - I just lazily referenced it in my lib directory, so the exception eventually caused the problem, but using your suggestions like if (rs! = Null) did the trick for me get to the root cause.

I originally thought it was because the variable is out of scope. This is clearly not the case.

0


a source







All Articles