?
Solved

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

Posted on 2003-03-15
9
Medium Priority
?
219 Views
Last Modified: 2010-03-31
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
Comment
Question by:redds2001
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8144344
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
 

Author Comment

by:redds2001
ID: 8144430
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
 

Author Comment

by:redds2001
ID: 8144486
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 86

Expert Comment

by:CEHJ
ID: 8144595
What i'm saying is change

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

to

>>rs2 = selectStatement.executeQuery(selectStatementStr);

in browseRequests
0
 
LVL 92

Expert Comment

by:objects
ID: 8144740
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
 
LVL 2

Expert Comment

by:functionpointer
ID: 8145882
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
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 8148019
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
 

Author Comment

by:redds2001
ID: 8148495
Thanks objects
i think thatz the problem... "closing the connection"
i used a single method now
redd
0
 
LVL 92

Expert Comment

by:objects
ID: 8148511
:-)
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question