tags:

views:

377

answers:

3

Hi!

I'm purposely targeting Java OS 1.4.2

I'm trying to use an iterator combined with apache's POI to read an excel spreadsheet.

The code runs perfectly in java 1.5, but in the 1.4.2 version I get the error listed in the question subject.

the code is:

Iterator<HSSFRow> myIter = null;

*Updated - Removed the null declaration, and immediately set it to the collection. Still getting the same error! "Iterator cannot be resolved to a type" (Iterator is an abstract type). This error occurs before I try to get the values from the iterator!

Iterator itRows = hsSheet.rowIterator();

    • I also have imported the HSSFRow variable

it breaks on that line of code, which is clearly at the beginning of the application. I dont understand what needs to be done to correct this issue. Please let me know if you have any insight!

A: 

You can't use generics (i.e. parameterized types) in Java versions prior to 1.5. Generics where introduced in 1.5 and even the syntax SomeClass<T> is not supported in 1.4.2. That's why you get the error.

aioobe
A: 

Are you sure it's not Iterator<Something> myIter = null; because that seems to be what the error is saying; you're using generics and trying to compile for 1.4, but generics were only added in 1.5.

edit: You can use the key ` (left of 1 key) to delimit code blocks; You need to delete the generics and just use Iterator myIter = null;

Andrei Fierbinteanu
+3  A: 

Java with version below 1.5 doesn't handle generic types ( aka types parametrized by other types: List ).

To run your code in java 1.4 you need to loose the generic type parameter and do the casts yourself.

Iterator myIterator = // initialize it
HSSFROW row = (HSSFROW)myIterator.next();

A more complete example:

List collection = new ArrayList();

collection.add("a");
collection.add("b");
collection.add("c");

Iterator myIterator = collection.iterator();

while ( myIterator.hasNext() ) {
    String value = (String) myIterator.next();
    System.out.println("value: " + value);
}

and the output for this is:

value: a
value: b
value: c
Toader Mihai Claudiu
ah, I tried this. When I initialize it (to null) then set it later, I still get the same error
Wooooo
this doesnt work :(
Wooooo
I posted a more complete example in the answer. It doesn't use HSSFROW but it works with a collection of strings.
Toader Mihai Claudiu
still seeing the same issue - updated the code in the question...
Wooooo
that code works :) the output is taken from an actual run of the code. You must be doing something else difference.
Toader Mihai Claudiu