Skip to main content

Bug at Arraylist.size()

8 replies [Last post]
ruya
Offline
Joined: 2007-05-10
Points: 0

Hi all,

Has anyone experienced problems with the ArrayList. I think I have found some kind of bug, but I am not sure it is a Java problem. I am using:

Intel Centrino Dothan
Kubuntu 6.10
java version "1.6.0"
Java(TM) SE Runtime Environment (build 1.6.0-b105)
Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)
Eclipse SDK Version: 3.2.1

The problem:
I have a NullPointerException, on a cycle like this:
for( Object a : objectList ) a.doSomething();
objectList is a ArrayList. I used the eclipse debugger to found the problem: it shows an ArrayList with 10 empty slots, but with the size set to 1. How is this possible?

Thank you all in advance,
Rui

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
_simu_
Offline
Joined: 2005-01-28
Points: 0

Why the correction? I think the first one is correct.

In the overwritten addAll with 2 arguments I call the original addAll with 2 arguments. The way you posted, you are calling the wrong original => other functionality.


> Anyway, just a small copy-paste correction :)

> (...)

> @Override

> public boolean addAll(int index,

> Collection c) {

> (...)

> return super.addAll( c);

> }

> u for helping,

> Rui

regards.

formatting

Message was edited by: _simu_

_simu_
Offline
Joined: 2005-01-28
Points: 0

> ...
> debugger to found the problem: it shows an ArrayList
> with 10 empty slots, but with the size set to 1. How
> is this possible?
> ...

Hi,
an ArrayList "permits all elements, including [b]null[/b]".
It's simple to create an ArrayList with size = 1 and all elements = [b]null[/b]

[code] ArrayList

list = new ArrayList(); list.add(null); System.out.println(list.size()); // just for setting breakpoint[/code] Check your code, probably there is an [b]add[/b] or [b]addAll[/b] with [b]null[/b] argument. Should be easy using the eclipse's Debug. Or override your ArrayList to check for an [b]null[/b] addition: [code] ArrayList list = new ArrayList() { @Override public boolean add(Object e) { if (e == null) throw new NullPointerException(); return super.add(e); } @Override public void add(int index, Object e) { if (e == null) throw new NullPointerException(); super.add(index, e); } @Override public boolean addAll(Collection c) { for (Object e : c) { if (e == null) throw new NullPointerException(); } return super.addAll(c); } @Override public boolean addAll(int index, Collection c) { for (Object e : c) { if (e == null) throw new NullPointerException(); } return super.addAll(index, c); } };[/code] []]]
ruya
Offline
Joined: 2007-05-10
Points: 0

You're totally right and hopefully I won't return with another question like this :). I am not very good at debugging...
Anyway, just a small copy-paste correction :)
(...)
@Override
public boolean addAll(int index, Collection c) {
(...)
return super.addAll( c);
}

Thank you for helping,
Rui

btek
Offline
Joined: 2007-05-10
Points: 0

Actually, if you just override the add() method, it should be sufficient in providing you with the necessary check. No need for the duplication.

@Override
public boolean add(Object e) {
if (e == null)
throw new NullPointerException();
return super.add(e);
}

_simu_
Offline
Joined: 2005-01-28
Points: 0


> Actually, if you just override the add() method, it

> should be sufficient in providing you with the

> necessary check. No need for the duplication.


duplication? they are different methods with different signature.

What if he calls list.add(0, null) or one of the addAll methods? Your code will NOT intercept these calls!

And since I do not know which one is used by his code, nor how compicated that code is, I opted for a bit more typing...

[]]

formatting

Message was edited by: _simu_

btek
Offline
Joined: 2007-05-10
Points: 0

u r right, my bad... was not thinking properly

btek
Offline
Joined: 2007-05-10
Points: 0

> The problem:
> I have a NullPointerException, on a cycle like this:
> for( Object a : objectList ) a.doSomething();
> objectList is a ArrayList

. I used the eclipse > debugger to found the problem: it shows an ArrayList > with 10 empty slots, but with the size set to 1. How > is this possible? Would you be able to provide a code chunk or better yet a class with a main() that showcases the bug? In other words, try and reproduce it and share it here.
ruya
Offline
Joined: 2007-05-10
Points: 0

I tried to make some dummy classes to reproduce it but I was not able to... It adds and removes without problem. However, in the real application, the problem persists.
What can I do? I have the code online at google code (dummy classes also) and I can point the code chuncks involved. Also, I have screenshots from the debugger ( which don't really help, only show an empty arraylist with size attribute set to 1 ).

Thank you,
Rui