DataNucleus JIRA is now in read-only mode. Raise any new issues in GitHub against the plugin that it applies to. DataNucleus JIRA will remain for the foreseeable future but will eventually be discontinued
Issue Details (XML | Word | Printable)

Key: NUCCORE-496
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Andy Jefferson
Reporter: Sudipta
Votes: 0
Watchers: 1

If you were logged in you would be able to see more operations.
DataNucleus Core

NullPointer Exception from listIterator() when calling set() on the iterator

Created: 23/Mar/10 08:33 PM   Updated: 04/Oct/12 05:02 PM   Resolved: 31/Mar/10 08:12 PM
Component/s: Persistence
Affects Version/s: 2.0.0.release, 2.0.1, 2.0.2
Fix Version/s: 2.0.3, 2.1.0.m1

File Attachments: 1. Zip Archive (10 kB)

Forum Thread URL:,6016
Datastore: PostgreSQL

 Description  « Hide
I am getting an ArrayIndexOutOfBoundsException in org.datanucleus.sco.backed.LinkedList.

Following is the structure of my persistent classes:
public class ClassRuleSet implements Comparable {
private String name;
private Map<String, ClassRuleList> rulesMap;

public ClassRuleSet(String name) { = name;
rulesMap = new HashMap<String, ClassRuleList>();

public int compareTo(Object obj) {
if ((obj == null) || ! (obj instanceof ClassRuleSet)) {
return 1;
ClassRuleSet ruleset = (ClassRuleSet)obj;
return name.compareTo(;

void putRule(ClassRule rule) {
String deviceTypeGuid = rule.getDeviceTypeGuid();
if (deviceTypeGuid == null) {
String m = "no deviceTypeGuid for rule, cannot put rule in ruleset";
throw new IllegalArgumentException(m);
ClassRuleList rules = rulesMap.get(deviceTypeGuid);
if (rules == null) {
rules = new ClassRuleList();
rulesMap.put(deviceTypeGuid, rules);

public class ClassRuleList {
private List<ClassRule> rules;

ClassRuleList() {
rules = new LinkedList<ClassRule>();

void add(ClassRule rule) {

void sort() {

Collections.sort(rules); //This is the place where it is throwing ArrayIndexBound exception

public class ClassRule
implements Cloneable, Comparable {

private String deviceTypeGuid;
private String name;
private ClassRuleSet ruleset;

public ClassRule(String name) { = name;

public int compareTo(Object obj) {
if ((obj == null) || !(obj instanceof ClassRule)) {
return 1;
ClassRule rule = (ClassRule)obj;
return name.compareTo(;

public void setRuleSet(ClassRuleSet ruleset) {
if (this.ruleset != null) {
if (this.ruleset.equals(ruleset)) {
if (deviceTypeGuid != null) {
this.ruleset = ruleset;
if ((ruleset != null) && (deviceTypeGuid != null)) {

The main program:

public void addRule() throws Exception {
PersistenceManager pm = pmf.getPersistenceManager();
ClassRuleSet ruleSet = null;
Transaction tx = pm.currentTransaction();

//Create a RuleSet
Random random = new Random();
String ruleSetName = "RuleSet" + random.nextInt();
ruleSet = new ClassRuleSet(ruleSetName);

//Create a first rule and set the rule set of that rule to the one created above
ClassRule rule = new ClassRule("Rule5");

//Create 2nd rule and set the ruleset of that rule to the one created above
//The rule set in this case is retrieved using a JDO query

Query query = pm.newQuery(ClassRuleSet.class);
query.setFilter("name == \"" + ruleSetName + "\"");
Collection results = (Collection)query.execute();
Iterator iter = results.iterator();
while (iter.hasNext()) {
ruleSet = (ClassRuleSet);
rule = new ClassRule("Rule4");
rule.setRuleSet(ruleSet); //This call produces the ArrayIndexOutOfBoundsException

//Create 3rd rule and set the ruleset of that rule to the one created above
//The rule set in this case is retrieved using a JDO query
query = pm.newQuery(ClassRuleSet.class);
query.setFilter("name == \"" + ruleSetName + "\"");
results = (Collection)query.execute();
iter = results.iterator();
while (iter.hasNext()) {
ruleSet = (ClassRuleSet);
rule = new ClassRule("Rule6");


The jdo mapping is as follows:

<package name="foo">
<class name="ClassRuleSet" table="class_rule_set">
<column name="class_rule_set_id" jdbc-type="INTEGER"/>
<version strategy="version-number" column="jdo_version"/>
<field name="name" column="nme">
<index name="idx_class_rule_set_name"/>
<field name="rulesMap" table="class_rule_set_rules_map">
<join column="class_rule_set_id" delete-action="none"/>

<class name="ClassRuleList" table="class_rule_list">
<column name="class_rule_list_id" jdbc-type="INTEGER"/>
<version strategy="version-number" column="jdo_version"/>
<field name="rules" table="class_rule_list_rle">
<join column="class_rule_list_id"/>
<element column="class_rle_id"/>
<order column="seq"/>

<class name="ClassRule" table="class_rle">
<column name="class_rle_id" jdbc-type="INTEGER"/>
<version strategy="version-number" column="jdo_version"/>
<field name="name" column="nme"/>

The jdo properties:




I am getting the following exception while trying to run the main program. The exception happens while trying to set the ruleset of the 2nd rule.
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.LinkedList.entry(
at java.util.LinkedList.set(
at org.datanucleus.sco.backed.LinkedList.set(
at org.datanucleus.sco.backed.LinkedList.set(
at org.datanucleus.sco.SCOListIterator.set(
at java.util.Collections.sort(
at foo.ClassRuleList.sort(
at foo.ClassRuleList.add(
at foo.ClassRuleSet.putRule(
at foo.ClassRule.setRuleSet(
at foo.Main.addRule(
at foo.Main.main(

One thing that I noticed is that it only happens for the 2nd rule. I put a try/catch in the main program around the code for creating and setting rule set of the 2nd rule, the exception was thrown for the 2nd rule, but the third rule was added successfully.

Sudipta added a comment - 23/Mar/10 08:36 PM
Test case

Sudipta made changes - 23/Mar/10 08:36 PM
Field Original Value New Value
Attachment [ 11111 ]
Andy Jefferson added a comment - 24/Mar/10 06:23 PM
Suggest that you review the testcase definition to save time in future
1. org.datanucleus.test package
2. no idea what all that code for opening a properties file is about
   pmf = JDOHelper.getPersistenceManagerFactory("");
3. file
4. "foo.jdo" is illegal. JDO1.0.1 and later all use "package.jdo", and only would be accepted since DN also allows that non-standard location.

No comments on the actual issue since no time to run it. the log would tell you what was being called (by Collections.sort, which we don't use in any of our tests, so who knows what sequence of operations it makes use of)

Andy Jefferson made changes - 31/Mar/10 08:05 PM
Summary NullPointer Exception in org.datanucleus.sco.backed.LinkedList while sorting NullPointer Exception from listIterator() when calling set() on the iterator
Fix Version/s 2.0.3 [ 10907 ]
Fix Version/s 2.1.0.m1 [ 10900 ]
Affects Version/s 2.0.2 [ 10874 ]
Affects Version/s 2.0.1 [ 10835 ]
Affects Version/s 2.0.0.release [ 10703 ]
Andy Jefferson added a comment - 31/Mar/10 08:12 PM
SVN trunk and branches/2.0 fixes this incorrect handling of ListIterator.set()

Andy Jefferson made changes - 31/Mar/10 08:12 PM
Status Open [ 1 ] Resolved [ 5 ]
Assignee Andy Jefferson [ andy ]
Resolution Fixed [ 1 ]
Andy Jefferson made changes - 03/Apr/10 10:45 AM
Status Resolved [ 5 ] Closed [ 6 ]
Peter Dettman added a comment - 16/Apr/10 04:41 AM
I believe the set() method of SCOListIterator is still incorrect, and a few other methods too.

The javadocs for java.util.ListIterator explain:
"Note that the remove() and set(Object) methods are not defined in terms of the cursor position; they are defined to operate on the last element returned by a call to next() or previous()."

I am attaching a patch (against HEAD) including
1. A new test case in SCOListIteratorTest that focuses on add/remove/set in various combinations.
2. A re-implementation of several methods of SCOListIterator to pass this new test and the existing ones.

In the new version, the class relies on it's internal ListIterator ('iter') to enforce all the usage restrictions; all calls are forwarded to 'iter' before any side-effects are applied to the fields, or the SCO (to avoid exceptions leaving it in an unexpected state). Several methods rely on 'iter' to determine the relevant index for a call to the SCO.

Peter Dettman added a comment - 16/Apr/10 06:39 AM
Actually I'm unable to attach to this Closed issue. Will attach to forum post pending activation of my account there.

Andy Jefferson added a comment - 16/Apr/10 08:34 AM
If you have a problem with a release you don't attach something to a closed issue, cos its closed and released. Simply raise a new issue (far easier), and set the "Affects Versions" as the release(s) that it is a problem in.

Andy Jefferson made changes - 04/Oct/12 05:02 PM
Component/s Persistence [ 10200 ]
Component/s JDO [ 10201 ]