所以我对如何解决这个问题感到茫然。
我正在使用Eclipse作为我的IDE并通过它导出一个可运行的jar。除了现在我有一个ComboBox并用数组加载它(FX.Collections-thing)之前,所有的东西都有效。我在我开发的Windows 7计算机上运行它,然后将其移动到我测试的Windows 10计算机上,以确保运行正常,但在这种情况下不是这样。
OutOfBoundsException通常很容易处理,但是我对如何处理这个异常感到茫然,因为它在一台计算机上工作(没有运行时异常)而在另一台计算机上有这个异常:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$159(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.elementData(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at com.sun.javafx.collections.ObservableListWrapper.get(ObservableListWrapper.java:89)
at my.pages.giftcertmaker.MainGiftCertPage.start(MainGiftCertPage.java:52)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$166(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$179(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$152(WinApplication.java:177)
... 1 more
Exception running application my.pages.giftcertmaker.MainGiftCertPage
编辑:原谅我没有尽职尽责,下面是51行和52行
ArrayList<Integer> certNumbersFound = workbook.getCertNumbers();
int lastNumber = certNumbersFound.get(certNumbersFound.size()-1);
这引出了getCertNumbers()中的问题
public ArrayList<Integer> getCertNumbers()
{
ArrayList<Integer> numbersUsed = new ArrayList<Integer>();
/*
* Code reads from an excel file column of doubles and converts
* the doubles to ints and adds them to numbersUsed with a for-loop
*/
return numbersUsed;
我尝试了4种不同的Java版本(1.8.0_181,_192,_201,_202)。我已经尝试在代码的不同部分更改从excel文件中读取的double类型。我已经尝试从,和更改ArrayList的类型。我已经改变了加载代码的位置。它始终属于这一部分:
certNumbersFound.get(certNumbersFound.size()-1)
我一直认为这没关系,但更好的方法是什么?或者我只是不走运?我还在main中启动(args)方法之前对System.out.print进行了System.out.println-ed,并在放入ArrayList的get方法之前将certNumbersFound.size() - 1放入其自己的对象中。
并且所有库以前都有用,但是添加这个ComboBox和ArrayList(而不是FX.Collections-thing)会破坏它。
我真是傻眼了。
如果指数超出范围,ArrayList.get
会抛出IndexOutOfBoundsException
。在你的情况下可能小于零。
为了避免这种情况,请在代码中添加检查:
ArrayList<Integer> certNumbersFound = workbook.getCertNumbers();
if (certNumbersFound.size() >= 1) {
int lastNumber = certNumbersFound.get(certNumbersFound.size()-1);
//more code
}
else {
//handle situation according to your needs
//e.g. throw exception, log something or write to err:
System.err.println("Invalid size: " + certNumbersFound.size());
}
从外部源读取数据时(如本例中的Excel文件),引入安全检查始终是个好主意。
更好的想法是在getCertNumbers
中放置异常处理(或:期望意外的处理代码),这是您读取(可能不可靠)外部源的方法。此上下文中的外部源意味着:不受Java编译器控制。
感谢JB Nizet帮助我完成这个工作。
我检查了文件是否不存在,将使用适当的模板创建一个新文件作为冗余。但!这并不意味着模板中有任何值可以加载ArrayList。
它在我的Windows 7计算机(开发计算机)上运行的唯一原因是因为它有一个测试文件,模板中已有数字,因此它从未像我在Windows 10(测试计算机)上那样从头开始运行。
我必须添加一个方法或if语句说:
if(certNumbersFound != null && certNumbersFound.size() > 0)
{
//Write code that can use the ArrayList certNumbersFound
//because there's values in the file
}
else
{
//Write code that doesn't use the ArrayList certNumbersFound
//because there's no values in the file.
}
我觉得很蠢。感谢大家。我很抱歉浪费你的时间。