2011-03-25 5 views
1

JFileChooser에서 노래 목록을 선택한 후 오디오 정보를 jList에 표시하고 싶습니다. 여기까지 내가 얻은 것이 있는데, 그것은 NullPointerException을 던졌습니다. jList에 오디오 정보를 표시하는 방법

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) 

방법이 발생하고 어떻게 그것을 해결할 수 :

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; 
} 

오류는 말한다?

+0

도움을 청하는 필요가 없습니다. 누구나 동등하게 여기에서 도움을 얻습니다. 그냥 똑똑한 방식으로 질문하십시오. 나는 조금 질문을 청소했다. – BalusC

+0

감사합니다 ... 다음 번에 ....... –

+1

및 267 호선은 · ·? – draganstankovic

답변

1

예외 스택 추적의 첫 번째 줄을 읽습니다.

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

는 그것은 openFile() 방법 내부 클래스 SoundsTrip.SoundBytePlaying의 라인 267에 던져 됐어요. 소스 코드에서 트랙백하십시오. 그것은 NullPointerExceptionsomeObjectnull 것을 의미

someObject.doSomething(); 

같은 것을 포함 할 것이다. 마침표 연산자 .을 사용하여 액세스/호출하려고하면이 예외가 throw됩니다. 그래서

  1. 인스턴스화가 미리이 null가 아니다 :

    은 기본적으로 문제를 해결하는 방법은 두 가지가 있습니다. 이 null을 때 그것을 우회 할 수 있도록

    someObject = new SomeObject(); 
    someObject.doSomething(); 
    
  2. 은 미리 확인하십시오. 선택

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

는 유일한 기능 요구 사항에 따라 달라집니다. 구체적인 문제에 관련없는


, 패키지 이름은 바람직하게는 모두 소문자로 작성되어야한다는 Java naming conventions 상태입니다.

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

변화 그것에 :

+0

친절함에 감사드립니다. 나는 그것을 시도 할 것이다 : :) –

1

이 줄을보고 받아

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

이 파일은 [I] 디렉토리 인 경우에만 filesInDirectory를 채울 수를 (그것이 파일이 아닙니다 의미).

+0

와우, 고마워요, 일했습니다. 나는 이미 투표를하고 이것을 대답으로 투표 할 수 있었으면 좋겠다. 하지만 내 평판이 나를 허용하는 요건으로 15 세 이하이기 때문에 그럴 수 없습니다. 어쨌든 당신 draganstankovic 각하 감사드립니다 .... :) –

+0

괜찮습니다. 이 '선생님'은 소리가 나지 않아서 '힘이 센'이라고 생각하지 않았습니다.어쩌면 나는 내 아내가 나를이 방법으로 부르라고 설득하려고 노력할 수있다. 만약 내가 살아남는다면 나는 나중에 말할 것이다. 당신의 연구에 행운이 있기를! – draganstankovic

관련 문제