Ошибка выполнения Java Eclipse с использованием PDF Box Начинается с java.lang.reflect.InvocationTargetException - PullRequest
0 голосов
/ 02 ноября 2018

У меня есть Java-программа, которая подсчитывает вхождения слов в текст, и вы получаете эту строку текста из PDF-файла. Я использую PDFBox, чтобы получить строку текста из PDF. Не было ошибки при запуске всей моей программы до того, как я добавил pdfBox, и мой класс графического интерфейса работал с ним нормально. Когда я добавил это PDF Box, эта ошибка появилась. Я пытался пройтись по моей программе в каждой строке, но понятия не имею, что вызывает эту ошибку во время выполнения. Помощь будет оценена!

package lab8;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

public class WordFrequency {

Map<String, Integer> wordFrequencyMap;

public WordFrequency() {
    // TODO Auto-generated constructor stub
}

public Map<String,Integer> getWords() throws IOException {
    wordFrequencyMap = new TreeMap<>();

    Scanner scanner = new Scanner(System.in);
    boolean existFile = false;
    String filePath;
    File filePDF;

    //ask user for file location
    do {
        System.out.print("\nFile PDF address location: ");
        filePath = scanner.next();
        filePDF = new File(filePath);

        //If file exists and it's in the directory, make it true;
        if(filePDF.exists() && !filePDF.isDirectory()) existFile = true;

    } while(!existFile);

    //close Scanner
    scanner.close();

    PDDocument document = PDDocument.load(filePDF);
    PDFTextStripper pdfStripper = new PDFTextStripper();

    /*Get all the text from the pdf and put it in a string*/
    String pdfText = pdfStripper.getText(document);
    System.out.println(pdfText);

    /*Split the string and break it apart if there's any spaces in between. Put the words in an list*/
    List<String> wordList = Arrays.asList(pdfText.split("\\s+"));

    for(int i = 0; i < wordList.size(); i++) {
        String word = cleanWord(wordList.get(i));
        System.out.println("Word: " + word);

        /*Checks to see if the word is empty*/
        if(word != "") {
            Integer count = wordFrequencyMap.get(word);

            /*If there was that word has no count, put 1; otherwise, increment count*/
            if(count == null) count = 1;
            else count += 1;

            wordFrequencyMap.put(word, count);
        }
    }

    System.out.println(wordFrequencyMap);
    return wordFrequencyMap;

}

/*Clean the word with anything but letters and return the new word all lowercased*/
public static String cleanWord(String word) {
    String newWord = "";

    for(int i = 0; i < word.length(); i++) {
        char temp = word.charAt(i);
        if(Character.isLetter(temp))
            newWord += temp;
    }
    return newWord.toLowerCase();
}   
}

Вот моя ошибка:

    Exception in Application start method
    java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.base/java.lang.reflect.Method.invoke(Unknown Source)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.base/java.lang.reflect.Method.invoke(Unknown Source)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(Unknown Source)
    at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.io.IOException: Error: End-of-File, expected line
    at org.apache.pdfbox.pdfparser.BaseParser.readLine(BaseParser.java:1119)
    at org.apache.pdfbox.pdfparser.COSParser.parseHeader(COSParser.java:2574)
    at org.apache.pdfbox.pdfparser.COSParser.parsePDFHeader(COSParser.java:2553)
    at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:213)
    at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1028)
    at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:984)
    at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:932)
    at lab8.WordFrequency.getWords(WordFrequency.java:52)
    at lab8.WordFrequencyGUI.start(WordFrequencyGUI.java:45)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(Unknown Source)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(Unknown Source)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(Unknown Source)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(Unknown Source)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
    ... 1 more
Exception running application lab8.WordFrequencyGUI
...