2014-12-19 25 views
2

早上好,我想在按下「Locate」按鈕時顯示文件location。我也有在JTextField中完整的文件路徑,當我嘗試,我得到一個「NullPointerException異常」異常,請給我方向,感謝使用文件位置在'windows explorer'中顯示文件

到目前爲止已經試過:

public class locateExp { 

    private JFrame frame; 
    private JTextField txtCusersmyFirstPdf; 
    private Desktop desktop; 

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

    /** 
    * Create the application. 
    */ 
    public locateExp() { 
      initialize(); 
    } 

    /** 
    * Initialize the contents of the frame. 
    */ 
    private void initialize() { 
      frame = new JFrame(); 
      frame.setBounds(100, 100, 489, 329); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.getContentPane().setLayout(null); 

      JButton button = new JButton("Locate"); 
      button.addActionListener(new ActionListener() { 
        public void actionPerformed(ActionEvent arg0) { 
          //full file path 
          File pdfFilepath = new File(txtCusersmyFirstPdf.getText()); 
          if (pdfFilepath.exists()){ 
        try { 
          //desktop.open(pdfFilepath.getParentFile()); 
         desktop.open(pdfFilepath); 
        } catch(IOException ex) { 

        } 
          } 
        } 
      }); 
      button.setMnemonic('t'); 
      button.setFont(new Font("Calibri", Font.BOLD, 12)); 
      button.setBorder(null); 
      button.setBackground(SystemColor.menu); 
      button.setBounds(126, 112, 44, 19); 
      frame.getContentPane().add(button); 

      JButton button_1 = new JButton("Open"); 
      button_1.setMnemonic('o'); 
      button_1.setFont(new Font("Calibri", Font.BOLD, 12)); 
      button_1.setBorder(null); 
      button_1.setBackground(SystemColor.menu); 
      button_1.setBounds(180, 112, 44, 19); 
      frame.getContentPane().add(button_1); 

      JButton button_2 = new JButton("Print"); 
      button_2.setMnemonic('p'); 
      button_2.setFont(new Font("Calibri", Font.BOLD, 12)); 
      button_2.setBorder(null); 
      button_2.setBackground(SystemColor.menu); 
      button_2.setBounds(228, 112, 44, 19); 
      frame.getContentPane().add(button_2); 

      JLabel label = new JLabel("File:"); 
      label.setFont(new Font("Calibri", Font.BOLD, 12)); 
      label.setBounds(114, 142, 44, 14); 
      frame.getContentPane().add(label); 

      JLabel lblMyFirstPdf = new JLabel("My First PDF Doc.pdf"); 
      lblMyFirstPdf.setBounds(162, 141, 269, 14); 
      frame.getContentPane().add(lblMyFirstPdf); 

      JLabel label_2 = new JLabel("Path/name:"); 
      label_2.setFont(new Font("Calibri", Font.BOLD, 12)); 
      label_2.setBounds(89, 173, 63, 14); 
      frame.getContentPane().add(label_2); 

      txtCusersmyFirstPdf = new JTextField(); 
      txtCusersmyFirstPdf.setText("C:\\Users\\My First PDF Doc.pdf"); 
      txtCusersmyFirstPdf.setColumns(10); 
      txtCusersmyFirstPdf.setBounds(162, 168, 269, 23); 
      frame.getContentPane().add(txtCusersmyFirstPdf); 

      JLabel label_3 = new JLabel("File Size:"); 
      label_3.setFont(new Font("Calibri", Font.BOLD, 12)); 
      label_3.setBounds(106, 205, 325, 14); 
      frame.getContentPane().add(label_3); 

      JLabel label_4 = new JLabel("513 k bytes"); 
      label_4.setBounds(162, 204, 269, 14); 
      frame.getContentPane().add(label_4); 
    } 
} 
+3

發生什麼樣的線路異常? – talex 2014-12-19 10:21:31

+0

謝謝@talex,我在** \t desktop.open(pdfFilepath)中遇到錯誤; **,謝謝 – 2014-12-19 10:23:47

+1

請添加一個可運行的示例。 – Jens 2014-12-19 10:24:05

回答

3

如果你得到NullPointerExceptiondesktop.open(pdfFilePath),你有沒有檢查對象desktop本身是否已被正確初始化?假設它的java.awt.Desktop您正在使用,驗證是否已選中兩個 1.是否桌面平臺上支持的和 2.無論是開放措施支持

你可以做到這一點,如下所示,前致電desktop.open(...)

if(!Desktop.isDesktopSupported()){   
    System.out.println("Desktop is not supported"); 
    return; 
} 
Desktop desktop = Desktop.getDesktop(); 
if (desktop.isSupported(Desktop.Action.OPEN) && pdfFilepath.exists()) { 
    try { 
     //desktop.open(pdfFilepath.getParentFile()); 
     desktop.open(pdfFilepath); 
    } catch(IOException ex) { 
     ... 
    } 
} 

如果您按照以上正確初始化它,仍然得到NullPointerException,該文件爲空(按文檔)。但由於pdfFilepath.exists()在您的情況下返回true,請檢查桌面初始化,如上所示。

相關問題