Object corruption when retrieving object from List

Hi,

We have a batch program. There are muliple threads running which read data from database and then processes the data.

In each thread. there is a call to fetch data from a table, create objects and populate a list and return it.

This list is then given to another method which processes each item from the list

This is a simplified version of what the code looks like.

public class Manager{

public void run(){

Reader r = new Reader();

Processor processor = new Processor();

List list = reader.getItems();

processor.processItem(list);

}

}

public class Reader{

public List getItems(){

List list = new ArrayList();

// Resultset rs = execute a query and get

// a resultset object

while(rs.next()){

Item item = new Item();

// set information from rs and set

//into item

list.add(item);

}

return list;

}

}

public class Processor {

public processItem(List list){

Item item = null;

while (!list.isEmpty()){

item = (Item)list.remove(0);

}

}

}

In the processItem method of Processor class, when I retrieve item from the List

it should return me an object of type Item. But very rarely what happens is that

the object I return is not of type Item, but null or a junk class like [C@1309b55.

Once this happens, i get a class cast exception when i am trying to cast this to

Item class.

Can anyone please tell me if you have encountered such a scenario before?

The process is running in a 2 processor environment.

It's mulithreaded, but method calls are done from within a thread.

The ArrayList is not shared across multiple threads. It's life starts

and ends within the run method of the thread.

[1860 byte] By [mukeshmja] at [2007-11-27 10:19:54]
# 1

> junk class like [C@1309b55

That would be an array of chars if I'm not mistaken.

Garbage in, garbage out.

If you're absolutely sure that there is no funny stuff going on with threads (although you should definitely make sure that's not the case), your best hope is to just add buttloads of logging to see what's going on.

-Kayaman-a at 2007-7-28 17:00:28 > top of Java-index,Java Essentials,Java Programming...
# 2

Yes, it could be the array of Chars. But i am not using an array of chars anywhere in the code, let alone the problem areas.

I have put a lot of logging in the code. But this occurs only once in 50 to 60 runs. So it's very difficult to replicate.

Could this be a JVM bug or caused due to just in time compilation?

mukeshmja at 2007-7-28 17:00:28 > top of Java-index,Java Essentials,Java Programming...
# 3

> Could this be a JVM bug or caused due to just in time

> compilation?

Theoretically possible, but highly unlikely. You're getting out what you're putting in. Look to your code for the bug.

If you're geting out a type that you don't think oyu put in, using generics will help you find out at compile time exactly what and where.

jverda at 2007-7-28 17:00:28 > top of Java-index,Java Essentials,Java Programming...
# 4

How your Item class looks like ?

manuel.leiriaa at 2007-7-28 17:00:28 > top of Java-index,Java Essentials,Java Programming...
# 5

Item class is made up of three instances of three separate classes with corresponding getters and setters.

All these 3 classes, have member variables of String, int and double.

There are getter and setter methods for all of the variables as well as overridden equals and hashcode methods

mukeshmja at 2007-7-28 17:00:28 > top of Java-index,Java Essentials,Java Programming...
# 6

> Item class is made up of three instances of three

> separate classes with corresponding getters and

> setters.

>

> All these 3 classes, have member variables of String,

> int and double.

> There are getter and setter methods for all of the

> variables as well as overridden equals and hashcode

> methods

You have to show your code, inside [code] and [/code] tags.

jverda at 2007-7-28 17:00:28 > top of Java-index,Java Essentials,Java Programming...
# 7

> Could this be a JVM bug or caused due to just in time

> compilation?

No. This is caused by a bug in your code.

cotton.ma at 2007-7-28 17:00:28 > top of Java-index,Java Essentials,Java Programming...
# 8

Just to add, this runs on IBM java not Sun java.

mukeshmja at 2007-7-28 17:00:28 > top of Java-index,Java Essentials,Java Programming...
# 9

Just want to point out that objects are not contained in another object, only references to them. The upshot being, the act of retrieveing an object from a List has no effect at all on the object being retrieved

At first guess, is your query actually doing what you think it's doing?

georgemca at 2007-7-28 17:00:28 > top of Java-index,Java Essentials,Java Programming...
# 10

> At first guess, is your query actually doing what you

> think it's doing?

Yes it is. In fact when I rerun the code, it runs with no problems. The same set of input data and other conditions.

Even if the query returned nothing, the method would have returned an empty list, not a list populated with null or a character array.

mukeshmja at 2007-7-28 17:00:28 > top of Java-index,Java Essentials,Java Programming...