2011-03-25 58 views
1

我想從JFileChooser中選擇歌曲列表後,將音頻信息顯示給jList。這是我得到的東西,但它拋出了一個NullPointerException如何在jList中顯示音頻信息

boolean openFile() throws FileNotFoundException{ 
    JFileChooser jfc = new JFileChooser(); 
    jfc.setMultiSelectionEnabled(true); 
    jfc.setDialogTitle("Open File"); 
    jfc.setFileSelectionMode(JFileChooser.FILES_ONLY); 
    jfc.setCurrentDirectory(new File (".")); 
    jfc.setFileFilter(audiofilter); 
    int result = jfc.showOpenDialog(this); 
    if(result == JFileChooser.CANCEL_OPTION){ 
     return true; 
    }else if(result == JFileChooser.APPROVE_OPTION){ 
     File[] file = jfc.getSelectedFiles();//fFile = jfc.getSelectedFile(); 
     String file_string = fFile.getAbsolutePath(); 
     for(int i=0;i<file.length;i++){ 
      if(file[i].isFile()){ 
       String[] filesInDirectory = file[i].list(); 
       for(int ii=0;ii<filesInDirectory.length;ii++){ 
        list.addElement(filesInDirectory[ii]); 
       } 
      }else{ 
       list.addElement(file[i]); 
      } 
     } 

     if(file_string != null){ 
      fTextArea.setText(file_string); 
     }else{ 
      return false; 
     } 
     this.jList1.setModel(list); 
    } 
    return true; 
} 

錯誤說:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
     at SoundsTrip.SoundBytePlaying.openFile(SoundBytePlaying.java:267) 
     at SoundsTrip.SoundBytePlaying.jButton1MouseClicked(SoundBytePlaying.java:210) 
     at SoundsTrip.SoundBytePlaying.access$000(SoundBytePlaying.java:43) 
     at SoundsTrip.SoundBytePlaying$2.mouseClicked(SoundBytePlaying.java:135) 
     at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:253) 
     at java.awt.Component.processMouseEvent(Component.java:6270) 
     at javax.swing.JComponent.processMouseEvent(JComponent.java:3267) 
     at java.awt.Component.processEvent(Component.java:6032) 
     at java.awt.Container.processEvent(Container.java:2041) 
     at java.awt.Component.dispatchEventImpl(Component.java:4630) 
     at java.awt.Container.dispatchEventImpl(Container.java:2099) 
     at java.awt.Component.dispatchEvent(Component.java:4460) 
     at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577) 
     at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4247) 
     at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168) 
     at java.awt.Container.dispatchEventImpl(Container.java:2085) 
     at java.awt.Window.dispatchEventImpl(Window.java:2478) 
     at java.awt.Component.dispatchEvent(Component.java:4460) 
     at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) 
     at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) 
     at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) 
     at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) 
     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) 
     at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) 
     at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) 

這是怎麼造成的,我該如何解決呢?

+0

沒有必要抱怨。這裏每個人都可以得到同樣的幫只需以聰明的方式提出問題。我清理了一下這個問題。 – BalusC 2011-03-25 02:45:13

+0

謝謝...下次注意... :) – 2011-03-25 02:49:29

+1

和267行是...? – draganstankovic 2011-03-25 02:51:40

回答

1

閱讀異常堆棧跟蹤的第一行。

at SoundsTrip.SoundBytePlaying.openFile(SoundBytePlaying.java:267) 

它被扔在SoundsTrip.SoundBytePlaying類267線,該openFile()方法內。在源代碼中引用它。它會包含類似

someObject.doSomething(); 

一個NullPointerException意味着someObjectnull。然後使用句點運算符.嘗試訪問/調用它將引發此異常。

基本上有兩種方法來解決這個問題:

  1. 初始化它事先所以,它不是null

    someObject = new SomeObject(); 
    someObject.doSomething(); 
    
  2. 事先檢查,以便它可以當它是null被繞過。

    if (someObject != null) { 
        someObject.doSomething(); 
    } 
    

選擇哪一個要看的唯一功能需求。


無關的具體問題,Java naming conventions狀態包名應優選地在所有小寫寫入。

+0

謝謝你的好意先生。我會嘗試... :) – 2011-03-25 03:00:25

1

看看這一行:

if(file[i].isFile()){ 
     String[] filesInDirectory = file[i].list(); 
... 

將其更改爲:

if(!file[i].isFile()){ 
    String[] filesInDirectory = file[i].list(); 

可以填充filesInDirectory只有這個文件[i]是一個目錄(這意味着它不是一個文件)。

+0

哇,謝謝你,先生,它的工作。我希望我已經可以投票並將其投票作爲答案。但我不能因爲我的聲望低於15作爲允許我的要求。反正謝謝先生draganstankovic .... :) – 2011-03-25 03:29:21

+0

沒問題。這位'先生'聽起來很'強大',不習慣。也許我可以試着說服我的妻子以這種方式給我打電話,如果我活下來,我會告訴你':)祝你好運! – draganstankovic 2011-03-25 03:56:13