使用JFileChooser中的文件路径字符串,设置执行操作的目录

时间:2019-02-27 00:44:41

标签: java swing nullpointerexception jfilechooser jxl

我正在尝试使用JFileChooser获取电子表格的系统路径。然后,下面的代码将修改电子表格,并输出一个新的,经过修改的工作表。

我认为我可以使用toString()来存储目录,但是我想它实际上不是在存储字符串吗?我得到的错误是java.lang.NullPointerException。它最初发生在我的第24行,这是我将目录设置为fc1.path的地方。

今天,我对此进行了大量研究,但运气不佳。该代码可以在不使用文件选择器的情况下正常运行,因此fc1.path可以解决问题。

以下是主程序功能的代码。.我从中提取的FileChooser4.java进一步向下。

App(.java):

    public class App {

    private static final int[] URL_COLUMNS = {0,4,9}; // Columns A, E, J
    public static void main(final String[] args) throws Exception {

        FileChooser4 fc1 = new FileChooser4();

        Workbook originalWorkbook = Workbook.getWorkbook(new File(fc1.path));
        WritableWorkbook workbook = Workbook.createWorkbook(new File("C:/Users/Shadow/Desktop/updated.xls"), originalWorkbook);
        originalWorkbook.close();
        WritableSheet sheet = workbook.getSheet(0);
        Cell cell;

        for (int i = 0; i < URL_COLUMNS.length; i++) {
            int currentRow = 1;
            while (!(cell = sheet.getCell(URL_COLUMNS[i], currentRow)).getType().equals(CellType.EMPTY)) {

                String url = cell.getContents();
                System.out.println("Checking URL: " + url);
                if (url.contains("scrapingsite1.com")) {
                    String Price = ScrapingSite1(url);
                    System.out.println("Scraping Site1's Price: " + Price);
                    // save price into the next column
                    Label cellWithPrice = new Label(URL_COLUMNS[i] + 1, currentRow, Price);
                    sheet.addCell(cellWithPrice);
                }
                currentRow++;
            }
        }

        workbook.write();
        workbook.close();
    }

    private static String ScrapingSite1(String url) throws IOException {
        Document doc = null;
        for (int i = 1; i <= 6; i++) {
            try {
                doc = Jsoup.connect(url).userAgent("Mozilla/5.0").timeout(6000).validateTLSCertificates(false).get();
                break;
            } catch (IOException e) {
                System.out.println("Jsoup issue occurred " + i + " time(s).");
            }
        }
        if (doc == null) {
            return null;
        } else {
            return doc.select("p.price").text();
        }
    }
}

FileChooser4(.java):

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileSystemView;

public class FileChooser4 {

    public String path;

    public void main(String[] args) {

        JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory().getPath());
        jfc.setDialogTitle("Select a sheet");
        jfc.setAcceptAllFileFilterUsed(false);
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Choose a '.xls' file...", "xls");
        jfc.addChoosableFileFilter(filter);

        int returnValue = jfc.showOpenDialog(null);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            path = jfc.getSelectedFile().getAbsolutePath();
            System.out.println(path + " selected.");
        }
    }

}

0 个答案:

没有答案