Assigning null to ArrayList items in Java

20,937

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.

Share:
20,937
foamboarder
Author by

foamboarder

Updated on July 15, 2022

Comments

  • foamboarder
    foamboarder almost 2 years

    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.

  • foamboarder
    foamboarder over 13 years
    I just want to set the class member variables to null by iterating through a list.
  • kaliatech
    kaliatech over 13 years
    There is no way to do that without using reflection and that is not what you want. Understanding your issue will require a better understanding of variable references in Java. Your code as shown in the question doesn't make much sense. Typically, you would not hold explicit member variable references in addition to holding those same objects in a list. Is there a specific reason you want to have a member variable for each prepared statement? Why not just keep them in the list, and only in the list?