-2

I try to use the method "getAbsolutePath()" but I always get the same error. Here is how I try to use it :

class OpenFrequenciesL implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            JFileChooser fileChooser = new JFileChooser();
            final FileNameExtensionFilter filter = new FileNameExtensionFilter("Text", "txt");
            fileChooser.setFileFilter(filter);
            int fileChooserResult = fileChooser.showOpenDialog(MyGUI.this);
            if (fileChooserResult == JFileChooser.APPROVE_OPTION) {
                filename.setText(fileChooser.getSelectedFile().getName());
                dir.setText(readLineByLineJava8(fileChooser.getAbsolutePath()));
            } if (fileChooserResult == JFileChooser.CANCEL_OPTION) {
                filename.setText("You pressed cancel");
                dir.setText("");
                }
            }
        }

I might need to import something but honestly I'm not even sure what I'm doing wrong at this point. Here's all that I imported :

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JFileChooser;
import javax.swing.text.JTextComponent;
import java.util.*;
import java.io.*;
import java.nio.file.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
import java.io.File;
Adrien Aacha
  • 109
  • 2
  • 8
  • 1
    What made you think that `fileChooser` even has a `getAbsolutePath()` method? *Hint:* `fileChooser` is not a `File` object. – Andreas Nov 29 '17 at 18:26
  • Well I don't know cut me some slack I'm new at this – Adrien Aacha Nov 29 '17 at 18:28
  • My question was me pondering how you knew a method named `getAbsolutePath()` even exists, anywhere. But, you should just check **the documentation**, i.e. the javadoc for the type of the object on which you're calling the method, which in this case is the `JFileChooser` class: https://docs.oracle.com/javase/9/docs/api/javax/swing/JFileChooser.html. Then you'd **know** which methods are available and would learn for yourself that it has no such method. http://idownvotedbecau.se/noresearch/ – Andreas Nov 29 '17 at 18:36

2 Answers2

1

The JFileChooser class does not have a getAbsolutePath() method.

What is the dir variable?

It looks like you want perhaps...

fileChooser.getCurrentDirectory().getAbsolutePath()
Drew Wills
  • 8,408
  • 4
  • 29
  • 40
0

This is because you are trying to call JFileChooser's method getAbsolutePath(), which does not exist. Instead you should replace the dir.setText(... line with:

dir.setText(readLineByLineJava8(fileChooser.getSelectedFile().getAbsolutePath()));

This is because JFileChooser does not have have a getAbsolutePath() method, so you need to get a File object for the file it has selected (returned by getSelectedFile()) and call getAbsolutePath() on that to get the filepath.

I hope this helps!

Atto Allas
  • 610
  • 5
  • 16