简体   繁体   中英

Java: weird issue with iterator

I am having a weird problem with an iterator I don't fully understand. The iterator is being used in a loggedPreparedStatement but every now and then (about 20 of the 200) throws this error:

SQLException in executeQuery, errorcode: 0, SQL state: 07001, message: No value specified for parameter 1
while executing statement: com.mysql.jdbc.PreparedStatement@11fffa73: SELECT ac.id as i FROM ac LEFT JOIN pa ON pa.id = ac.adspace_id WHERE  pa.adspace_id IN (** NOT SPECIFIED **) 

This is how I create my iterator:

private Iterator< Integer > adspacesId = null;

public ArrayList<Integer> getIds(int inAdspaceId) {
        if (inAdspaceId <= 0) return new ArrayList<Integer>();
        ArrayList<Integer> tempList = new ArrayList<Integer>();
        tempList.add(new Integer(inAdspaceId));
        adspacesId = tempList.listIterator();
        num = tempList.size();
        activeOnly = false;

        if (adspacesId != null && adspacesId.hasNext() && num > 0) {
            executeQuery();
        }
        return result;
    }

So I add an int to a temp arrayList and then turn that into a Iterator, then use that iterator in my query like so:

LoggedPreparedStatement statement = new LoggedPreparedStatement(theQuery.toString());
statement.setInt(1, adspacesId);

So my first guess would be, no entries in my iterator, but how does it pass the check of adspacesId.hasNext() then?

The value of num is 1, so there is an entry in the templist and I would assume in my iterator.

Little help is welcome :)

ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);

Iterator<Integer> it = list.listIterator();

System.out.println("First Element : " + it.next());

I guess you have to call next() to get the elements even the first one. Which i don't see you have done here. And if its a PreparedStatement , I don't see where you have added the parameter before executing the executeQuery() , And i suggest you use the ListIterator interface instead just Iterator here..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM