2

Good Morning, I want to show the file location when the button "Locate" is pressed. Also I have full file path in JTextField, when I tried, I get an 'NullPointerException' exception, please give me directions, thanks

So far have tried:

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);
    }
}
Java.beginner
  • 871
  • 2
  • 19
  • 37
  • 3
    At what line exception occur? – talex Dec 19 '14 at 10:21
  • Thanks @talex, I got an error at ** desktop.open(pdfFilepath);**, thanks – Java.beginner Dec 19 '14 at 10:23
  • 1
    Please add a runable example. – Jens Dec 19 '14 at 10:24
  • 3
    "Instances of this class may or may not denote an actual file-system object such as a file or a directory."—[File](https://docs.oracle.com/javase/8/docs/api/java/io/File.html). – trashgod Dec 19 '14 at 10:38
  • 1
    So if the exception is as *desktop.open(pdfFilepath)* then the problem is 'desktop' is not initialized. Try 'Desktop.getDesktop().open(pdfFilepath)' and see what happens. – dic19 Dec 19 '14 at 10:47
  • Thank you so much @dic19, it worked! superb! but it opens the file so I did change it to **Desktop.getDesktop().open(pdfFilepath.getParentFile());**, now it open's the file location in the 'Windows Explorer', Thank you so much for your help and time, if you post the answer will accept it, – Java.beginner Dec 19 '14 at 10:51
  • 1
    @Java.beginner: Please edit your question to include your [complete example](http://stackoverflow.com/help/mcve), seen [here](http://pastebin.com/Qt2AzpBi); I defer to dic19 to answer. – trashgod Dec 19 '14 at 10:53
  • 2
    BTW - Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Dec 19 '14 at 11:07
  • 2
    It's ok, I wrote my comment from my phone and it's no so easy to write an answer though. @trahsgod You are welcome Java.beginner - Please accept the answer you already have. – dic19 Dec 19 '14 at 11:13

1 Answers1

3

If you get NullPointerException at desktop.open(pdfFilePath), have you checked whether the object desktop itself has been properly initialized? Assuming it is java.awt.Desktop you are using, verify if you have checked both 1. whether Desktop is supported on your platform and 2. whether OPEN Action is supported

You can do this as follows, before the call to 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) {
        ...
    }
}

If you have initialized it properly as above and still get NullPointerException, the the file is null (as per docs). But since pdfFilepath.exists() returns true in your case, check the desktop initialization as shown above.

AmeetC
  • 280
  • 1
  • 7