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 3300582
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:40:31+00:00 2026-05-17T20:40:31+00:00

I have the following code: static ArrayList<PreparedStatement> alPrepStmts = new ArrayList<PreparedStatement>(); static PreparedStatement m_stmtOne;

  • 0

I have the following code:

static ArrayList<PreparedStatement> alPrepStmts = new ArrayList<PreparedStatement>();

static PreparedStatement m_stmtOne;
static PreparedStatement m_stmtTwo;
static PreparedStatement m_stmtThree;
...
static PreparedStatement m_stmtOneHundred;

private static void init() {
    alPrepStmts.add(m_stmtOne);
    alPrepStmts.add(m_stmtTwo);
    alPrepStmts.add(m_stmtThree);
    ...
    alPrepStmts.add(m_stmtOneHundred);
}

private static void work() {
    if(m_stmtOne == null) {
        // assign m_stmtOne
    }
    // use m_stmtOne
}

private static void close() throws SQLException {
    for(PreparedStatement ps : alPrepStmts) {
        if(ps != null) {
            ps.close();
            ps = null;
        }
    }
}

init() gets called once. work() and close() may get called several times. The problem is that after calling close(), the PreparedStatements are not set to null. The next time I call work(), m_stmtOne is not null, but is closed. I supposed I can check if m_stmtOne is open, but I am wondering how can I assign null to the members of the container.

I also tried using iterators and it does not work either. The if conditional below is never true.

private static void ClosePreparedStatements() throws SQLException {
    for(Iterator<PreparedStatement> it = alPrepStmts.iterator(); it.hasNext();) {
        PreparedStatement ps = (PreparedStatement)it.next();
        if(ps != null) {
            ps.close();
            ps = null;
        }
    }
}

I know I can close and assign every statement manually, but I’m just wondering what is the best way to assign null to elements in a container.

  • 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-17T20:40:32+00:00Added an answer on May 17, 2026 at 8:40 pm

    Your assignment ps = null assigns only the local variable, it does not modify the original container. You should use the remove method of java.util.Iterator (in your second code snipped instead of ps = null) to remove an element from the container. Note this removes the element from the container, not replace it with null in the container.

    Even if you remove the element from the container, it will not change the value of, e.g. m_stmtOne, which it sounds like you want to have happen also. You will have to do that some other way.

    It also seems init() just adds 100 nulls to your list of prepared statements. Assigning a non-null value to m_stmtOne later will not change the list. In other words, m_stmtOne gets added to the list by value, not by reference.

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

Sidebar

Related Questions

I have the following code: private static void WriteStartupInfo() { Settings settings = Settings.Default;
Let us have the following code: public class TestGenerics { static <T> void mix(ArrayList<T>
I have following code private static ObservableCollection<myColor> myItems = new ObservableCollection<myColor>(); myItems.Add(new myColor(red)); When
I have the following code: public class Test { public static void Main() {
Assume I have the following code: public static class Foo { public static void
In one of my class I have the following code: private static final Color
I have the following code: class Program { static void Main(string[] args) { string
I have the following code: public static T ParameterFetchValue<T>(string parameterKey) { Parameter result =
Say I have the following code snippet in c# static const bool DO_PERFORMANCE_CODE =
I have the following code: $bind = new COM(LDAP://CN=GroupName,OU=Groups,OU=Division,DC=company,DC=local); When I execute it from

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.