我对java很陌生,所以如果我没有注意到我犯的一些错误,我很抱歉。正如标题所说,当我构建一个在 Netbeans 7.2.1 上完美运行的项目,然后双击 jar 文件时,什么也没有发生。在网上查看我读到的内容,为了更好地发现错误,建议转到命令提示符并写入:
java -jar C:\Users\conserva\Documents\NetBeansProjects\EasyText\dist\EasyText.jar
它给了我这个:
C:\>java -jar C:\Users\conserva\Documents\NetBeansProjects\EasyText\dist\EasyText.jar
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at easytext.MainWindow.<init>(MainWindow.java:25)
at easytext.MainWindow$4.run(MainWindow.java:220)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
这是第 25 行的代码部分:
public MainWindow() {
initComponents();
selectfolder.removeAllItems();
String tempordir = ("src/easytext/textfiles/");
String[] arr = new java.io.File(tempordir).list();
/*line 25 */ int len = arr.length;
//System.out.println("arr.lenght è: "+arr.length);
for (int i = 0; i<len; ++i){
selectfolder.addItem(arr[i]);}
selecttext.removeAllItems();
tempordir = ("src/easytext/textfiles/"+selectfolder.getSelectedItem().toString());
arr = new java.io.File(tempordir).list();
for (int i = 0; i<len; ++i){
selecttext.addItem(arr[i]);}
GetText("src/easytext/textfiles/"+selectfolder.getSelectedItem().toString()+"/"+selecttext.getSelectedItem().toString());
}
所以看起来 nullpointerecxeption 被抛出,因为我指的是 len 数组的长度。
我该如何解决这个问题?在此先感谢,如果我犯了一些愚蠢的错误或看不清楚,再次抱歉。我真的花了一些时间没有找到解决方案。