如何在JavaFX 8中使用Univocity分析器?

时间:2015-11-04 14:29:00

标签: parsing csv javafx

如何在JavaFX 8中使用Univocity解析器?

正在使用IntelliJ。 IntelliJ在编码上下文中没有给出警告,但是在运行代码时会出现错误。

这是代码和堆栈跟踪(缩写):

Main.java:

package parse;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
    Parent root = FXMLLoader.load(getClass().getResource("parse.fxml"));
    primaryStage.setTitle("Parse Test");
    primaryStage.setScene(new Scene(root, 300, 275));
    primaryStage.show();
}


public static void main(String[] args) {
    launch(args);
}
}

Controller.java:

package parse;

import com.univocity.parsers.common.processor.RowListProcessor;
import com.univocity.parsers.csv.CsvParser;
import com.univocity.parsers.csv.CsvParserSettings;
import javafx.fxml.FXML;
import javafx.scene.control.TextArea;

import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.util.List;

public class Controller {

@FXML
private TextArea textArea;


public Controller() {
}

@FXML
private void initialize() {
}

public void print(String message) {

    textArea.appendText(message);
}

public Reader getReader(String relativePath) {
    try {
        return new InputStreamReader(this.getClass().getResourceAsStream(relativePath), "Windows-1252");
    } catch (UnsupportedEncodingException e) {
        throw new IllegalStateException("Unable to read input", e);
    }
}


@FXML
public void columnSelection() {

    print("this is the parsing method" + System.getProperty("line.separator"));

    RowListProcessor rowProcessor = new RowListProcessor();
    CsvParserSettings parserSettings = new CsvParserSettings();

    parserSettings.setRowProcessor(rowProcessor);
    parserSettings.setHeaderExtractionEnabled(true);
    parserSettings.setLineSeparatorDetectionEnabled(true);
    parserSettings.setSkipEmptyLines(true);
    parserSettings.getFormat().setDelimiter(',');

    // Here we select only the columns "Price", "Year" and "Make".
    // The parser just skips the other fields
    parserSettings.selectFields("AUTHOR", "ISBN");

    CsvParser parser = new CsvParser(parserSettings);
    parser.parse(getReader("list3.csv"));

    List<String[]> rows = rowProcessor.getRows();

    String[] strings = rows.get(0);

    print(strings[0]);


}


}

parse.fxml:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="279.0" prefWidth="322.0" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1" fx:controller="parse.Controller">
   <center>
      <Button mnemonicParsing="false" onAction="#columnSelection" text="Parse" BorderPane.alignment="CENTER" />
   </center>
   <top>
      <TextArea fx:id="textArea" prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
   </top>
</BorderPane>

堆栈追踪:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1770)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1653)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)

Caused by: java.lang.NullPointerException
    at java.io.Reader.<init>(Reader.java:78)
    at java.io.InputStreamReader.<init>(InputStreamReader.java:97)
    at parse.Controller.getReader(Controller.java:34)
    at parse.Controller.columnSelection(Controller.java:60)
    ... 56 more

0 个答案:

没有答案