• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

Problem accessing a result set in one method which was obtained in other method

Here is the problem..
The method below browseRequests() is called in a jsp page. The result set from the query in this method could not be accessed in another method named getRequests() and it is giving me a null pointer exception when i try to access the method getRequests() from the jsp page. some one please help me out.

Thanks
Redds


  public void browseRequests() throws SQLException {
    Connection connection = null;
    String selectStatementStr = "some statement"

    try {
      connection = DriverManager.getConnection(protocol, "scott", "tiger");
      Statement selectStatement;
      selectStatement =
connection.createStatement();
      ResultSet rs2 = selectStatement.executeQuery(selectStatementStr);
}
    finally {
      if (connection != null) {
        try {
          connection.close();
        } catch (SQLException sqle) {}
      }
    }
  }


 public void getRequests()
 {
    try
    {
      if(rs2.next())
      {
        setSequence(""+rs2.getInt(1));
        setFirstName(rs2.getString(2));
        setLastName(rs2.getString(3));
        setEmail(rs2.getString(4));
        setOs(rs2.getString(5));
        setProblem(rs2.getString(6));
        setSoftware(rs2.getString(7));
        setStatus(rs2.getString(8));
        moreRequests=true;
      }
      else
     {
        moreRequests=false;
      }
    }
    catch(SQLException e)
    {}
 }
0
redds2001
Asked:
redds2001
  • 3
  • 3
  • 2
  • +1
1 Solution
 
CEHJCommented:
Note that this

>>ResultSet rs2 = selectStatement.executeQuery(selectStatementStr);

is a local variable whilst this

>>if(rs2.next())

is an instance variable

They are different and since one has not been initialized, it's null

You need


rs2 = selectStatement.executeQuery(selectStatementStr);


0
 
redds2001Author Commented:
Thanks for the comment

But the statement should be executed only once since getRequests is called from a loop in a jsp page to print the all the records in the result set. I cannot put

rs2 = selectStatement.executeQuery(selectStatementStr);

in the getRequest() method..

redd

0
 
redds2001Author Commented:
Thanks for the comment

But the statement should be executed only once since getRequests is called from a loop in a jsp page to print the all the records in the result set. I cannot put

rs2 = selectStatement.executeQuery(selectStatementStr);

in the getRequest() method..

redd

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
CEHJCommented:
What i'm saying is change

>>ResultSet rs2 = selectStatement.executeQuery(selectStatementStr);

to

>>rs2 = selectStatement.executeQuery(selectStatementStr);

in browseRequests
0
 
objectsCommented:
You could have browseRequests return the ResultSet it creates, and then pass that to getRequests():

 public ResultSet browseRequests() throws SQLException {
   Connection connection = null;
   String selectStatementStr = "some statement"
   ResultSet rs2 = null;

   try {
     connection = DriverManager.getConnection(protocol, "scott", "tiger");
     Statement selectStatement;
     selectStatement =
connection.createStatement();
     rs2 = selectStatement.executeQuery(selectStatementStr);
}
   finally {
     if (connection != null) {
       try {
         connection.close();
       } catch (SQLException sqle) {}
     }
   }
   return rs2;
 }


public void getRequests(ResultSet rs2)
{
   try
   {
     if(rs2.next())
     {
       setSequence(""+rs2.getInt(1));
       setFirstName(rs2.getString(2));
       setLastName(rs2.getString(3));
       setEmail(rs2.getString(4));
       setOs(rs2.getString(5));
       setProblem(rs2.getString(6));
       setSoftware(rs2.getString(7));
       setStatus(rs2.getString(8));
       moreRequests=true;
     }
     else
    {
       moreRequests=false;
     }
   }
   catch(SQLException e)
   {}
}
0
 
functionpointerCommented:
I dont think using rs as a member scope variable is a bad idea ( if you get what CEHJ is saying ).

But, you should write your methods so there not so dependent on the order in which they are called. If you make it simple, the next person to touch this jsp won't screw it up, neither will you. Personally, I try to idiot proof my code as much as possible, so that not even I could screw it up. ;-)

Like,

getRequests()
{
  if ( rs == null )
     browseRequests();
  // continue...
}

You can now freely call getRequests() all over the page, whenever, whereever you feel like it.
You dont have to worry about the problem of calling getRequests() prior to calling browseRequests(), or wondering where you get the ResultSet argument for getRequests( ResultSet rs ) when you look at this code 6 months from now.

Another thought: if it looks like an init() method, smells like an init() method...   you get my point.

Good luck. :)
0
 
objectsCommented:
You also shouldn't be closing your connection before you finish processing.
Which raises another problem in that you need to handle closing the connection (and the statement, and result set once you are done).

It would be a lot easier to handle the entire query in one method, unless you have some reason why this cannot be done. Otherwise you will need to make your connection, statement and result set member vars.
0
 
redds2001Author Commented:
Thanks objects
i think thatz the problem... "closing the connection"
i used a single method now
redd
0
 
objectsCommented:
:-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now