开发者

reference to IndexOutOfBoundsException is ambiguous

开发者 https://www.devze.com 2022-12-30 12:14 出处:网络
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 matc

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 &&开发者_如何学Python; theValue.length()!=0) { 
          oCell.setFormula("'"+(String)theValue.toString());
      } else {
          oCell.setFormula((String)theValue.toString());
      }
    }
  }


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.


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.


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.


@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.

0

精彩评论

暂无评论...
验证码 换一张
取 消