2014-10-07 27 views
0

我試圖從某人的計算機加載文件,並將其作爲圖標放入標籤中。當我嘗試運行它時,我得到一個NullPointer錯誤。當我到達setIcon代碼時它會中斷使用JFileChooser將圖像加載到JLabel圖標

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       TheChooser frame = new TheChooser(); 
       frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the frame. 
*/ 
public TheChooser() { 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 450, 300); 
    contentPane = new JPanel(); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    setContentPane(contentPane); 
    contentPane.setLayout(null); 

    JButton searchButton = new JButton("Search Picture"); 
    searchButton.addMouseListener(new MouseAdapter() { 
     @Override 
     public void mouseClicked(MouseEvent arg0) { 
      FileFilter filter = new FileNameExtensionFilter("JPEG file", "jpg", "jpeg"); 
      fc.setFileFilter(filter); 
      int response = fc.showOpenDialog(null); 
     try{ 
      if (response == JFileChooser.APPROVE_OPTION) { 
       String pathName = fc.getSelectedFile().getPath(); 
       JOptionPane.showMessageDialog(null, pathName); 
       ImageIcon icon = new ImageIcon(pathName); 
       picPanel.setIcon(icon); 
      } else { 
       JOptionPane.showMessageDialog(null, "Feel Free to Look Later"); 
      } 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     } 
    }); 
    searchButton.setBounds(141, 11, 139, 23); 
    contentPane.add(searchButton); 

    JLabel picPanel = new JLabel(""); 
    picPanel.setIcon(null); 
    picPanel.setBounds(10, 58, 414, 192); 
    contentPane.add(picPanel); 
} 

這是怎麼回事?

我試圖做的修復之下下來,但我仍然得到錯誤

  java.lang.NullPointerException 
       at chooser.TheChooser$2.mouseClicked(TheChooser.java:70) 
       at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source) 
       at java.awt.Component.processMouseEvent(Unknown Source) 
       at javax.swing.JComponent.processMouseEvent(Unknown Source) 
       at java.awt.Component.processEvent(Unknown Source) 
       at java.awt.Container.processEvent(Unknown Source) 
       at java.awt.Component.dispatchEventImpl(Unknown Source) 
       at java.awt.Container.dispatchEventImpl(Unknown Source) 
       at java.awt.Component.dispatchEvent(Unknown Source) 
       at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
       at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
       at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
       at java.awt.Container.dispatchEventImpl(Unknown Source) 
       at java.awt.Window.dispatchEventImpl(Unknown Source) 
       at java.awt.Component.dispatchEvent(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.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
       at java.awt.EventQueue$4.run(Unknown Source) 
       at java.awt.EventQueue$4.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) 
      java.lang.NullPointerException 
       at chooser.TheChooser$2.mouseClicked(TheChooser.java:70) 
       at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source) 
       at java.awt.Component.processMouseEvent(Unknown Source) 
       at javax.swing.JComponent.processMouseEvent(Unknown Source) 
       at java.awt.Component.processEvent(Unknown Source) 
       at java.awt.Container.processEvent(Unknown Source) 
       at java.awt.Component.dispatchEventImpl(Unknown Source) 
       at java.awt.Container.dispatchEventImpl(Unknown Source) 
       at java.awt.Component.dispatchEvent(Unknown Source) 
       at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) 
       at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) 
       at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) 
       at java.awt.Container.dispatchEventImpl(Unknown Source) 
       at java.awt.Window.dispatchEventImpl(Unknown Source) 
       at java.awt.Component.dispatchEvent(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.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
       at java.awt.EventQueue$4.run(Unknown Source) 
       at java.awt.EventQueue$4.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) 
+0

你是什麼人的電腦是指?你的意思是該文件位於另一臺計算機上? – Muhammad 2014-10-07 03:21:36

回答

2

我猜測的一個長長的清單,你有picPanel聲明爲類成員。 JLabel picPanel(底部)是而不是您從setIcon引用的mouseClicked。如果沒有類成員picPanel會導致編譯錯誤,因爲在嘗試訪問mouseClicked之前需要聲明本地picPanel

移動JLabel picPanel = new JLabel(「」);在添加偵聽器之前,可能會解決問題。並擺脫setIcon(null)。但是,再次,我不知道你的代碼中還有哪些代碼可能試圖處理picPanel,所以你可能想要擺脫本地聲明,因爲它是對類成員的遮蔽。所以基本上,而不是

JLabel picPanel = new JLabel(); 
searchButton.addMouseListener(new MouseAdapter() 

使用

picPanel = new JLabel(); 
searchButton.addMouseListener(new MouseAdapter() 

這裏有修復,在一個完整的程序

public class TheChooser extends JFrame { 

    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        TheChooser frame = new TheChooser(); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the frame. 
    */ 
    private JPanel contentPane; 
    private JFileChooser fc = new JFileChooser(); 
    private JLabel picPanel; 

    public TheChooser() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 450, 300); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 
     contentPane.setLayout(null); 
     //JLabel picPanel = new JLabel(""); 

     picPanel = new JLabel(); 
     JButton searchButton = new JButton("Search Picture"); 
     searchButton.addMouseListener(new MouseAdapter() { 
      @Override 
      public void mouseClicked(MouseEvent arg0) { 
       FileFilter filter = new FileNameExtensionFilter("JPEG file", "jpg", "jpeg"); 
       fc.setFileFilter(filter); 
       int response = fc.showOpenDialog(null); 
       try { 
        if (response == JFileChooser.APPROVE_OPTION) { 
         String pathName = fc.getSelectedFile().getPath(); 
         JOptionPane.showMessageDialog(null, pathName); 
         ImageIcon icon = new ImageIcon(pathName); 
         picPanel.setIcon(icon); 
        } else { 
         JOptionPane.showMessageDialog(null, "Feel Free to Look Later"); 
        } 
       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     }); 
     searchButton.setBounds(141, 11, 139, 23); 
     contentPane.add(searchButton); 
     picPanel.setBounds(10, 58, 414, 192); 
     contentPane.add(picPanel); 
    } 
} 
+1

謝謝你和1+ – 2014-10-07 03:13:41