senzacionale
senzacionale

Reputation: 20926

reference to IndexOutOfBoundsException is ambiguous

ERROR:

reference to IndexOutOfBoundsException is ambiguous, both class com.sun.star.lang.IndexOutOfBoundsException in com.sun.star.lang and class java.lang.IndexOutOfBoundsException in java.lang match

CODE:

public void insertIntoCell(int CellX, int CellY, String theValue, 
                             XSpreadsheet TT1, 
                             String flag) throws IndexOutOfBoundsException {

    XCell oCell = null;
    oCell = TT1.getCellByPosition(CellX, CellY);

    if (flag.equals("V")) {
      oCell.setValue((new Float(theValue)).floatValue());
    } else {
      if (theValue!=null && theValue.length()>0 && theValue.length()!=0) { 
          oCell.setFormula("'"+(String)theValue.toString());
      } else {
          oCell.setFormula((String)theValue.toString());
      }
    }
  }

Upvotes: 0

Views: 539

Answers (4)

Stephen C
Stephen C

Reputation: 719346

@BalusC and @polygenelubricants answers are spot on.

I just want to point out that this illustrates the problems that can arise when someone defines a class with the same name as a class in java.lang or (to a lesser extent) one of the other widely used classes. Other examples that you are likely to run into in the long term are java.util.Date versus java.sql.Date and java.util.List versus java.awt.List.

Upvotes: 0

BalusC
BalusC

Reputation: 1109362

The class IndexOutOfBoundsException has appeared more than once in the (implicit) imports. You'll need to reorganize the imports to be more specific (i.e. don't use import com.sun.star.lang.* but import com.sun.star.lang.SomeClassName, if you're using an IDE like Eclipse, it can do that automatically for you), or to use the full qualified class name instead. I.e. including the package, e.g.

throws java.lang.IndexOutOfBoundsException

instead of

throws IndexOutOfBoundsException

That said, imports on com.sun.* or sun.* are considered bad practice since those are undocumented Sun classes which are subject for changes and would not work on non-Sun JVM's. Even Sun itself recommends to not import/use those classes in your own code.

Upvotes: 4

helpermethod
helpermethod

Reputation: 62224

IndexOutOfBoundsException is ambiguous because there are two classes named IndexOutOfBoundsException within two different packages (com.sun.star.lang and java.lang). You need to tell the compiler which one you mean by prefixing IndexOutOfBoundsException with the correct package name.

Upvotes: 1

polygenelubricants
polygenelubricants

Reputation: 383926

Fully qualify the exception type.

public void insertIntoCell.. throws com.sun.star.lang.IndexOutOfBoundsException {
}

I'm presuming here that you do not intend to throw java.lang.IndexOutOfBoundsException, which is an unchecked RuntimeException, but I could be wrong.

You can also use a single-type import declaration instead:

import com.sun.star.lang.IndexOutOfBoundsException;
//...

public void insertIntoCell.... throws IndexOutOfBoundsException {
}

But this can potentially cause a lot more confusion down the pipe.

Upvotes: 5

Related Questions