Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 202791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:19:15+00:00 2026-05-11T17:19:15+00:00

My problem is that I have to set a variable in a try statement

  • 0

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

Later on I need to use that variable but it is now out of scope, or so I believe. I initialise the variable outside the try statement and set it to null, I thought that it might then be accessible outside, but I still get a NullPointerException.

The code is below, with lots of it taken out to make reading easier – I know it’s bad code, but I am new to Servlets and just wanted to see it running with all the moving parts doing what they are supposed to.

I have created another class that calls createDocs(…) and passes in the required parameters, and it works fine. So that is making me curious as to why when I call rs.getString("name") I get the NullPointerException, as this is exactly what I do from the other class (run from a main method for convenience) and it works as expected.

The variable in question is the ResultSet variable “rs” –

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();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:19:16+00:00Added an answer on May 11, 2026 at 5:19 pm

    Your problem is that if this statement:

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

    throws an exception then the value of rs is still null. So what I would do is move the loop inside the same try-catch block. Alternatively you can check whether it’s still null before trying to use it.

    Setting the value to null outside the try-catch block isn’t bad code though. That’s just what you have to do if you want the rs variable outside the try block (and that includes inside one of the catch clauses). Your rs while loop should probably just be within the same try block.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 161k
  • Answers 161k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You initialise vStatus to OK, then you immediately assign a… May 12, 2026 at 11:47 am
  • Editorial Team
    Editorial Team added an answer Do you have access to "history.js", "history.css", and "historyFRame.html" on… May 12, 2026 at 11:47 am
  • Editorial Team
    Editorial Team added an answer You could try inheritance ... the three controllers can all… May 12, 2026 at 11:47 am

Related Questions

So, I'm trying to stub a database connector in order to write tests for
I am new to threads and in need of help. I have a data
I have a method that creates a new node in a tree - either
Let's suppose I have a struct like this: struct my_struct { int a; int

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.