XMLEncoder 拒绝写入文件
XMLEncoder refusing to write to file
我是 Java programming/javafx 的新手,正在尝试创建一个接受名字、姓氏和年龄输入的程序。将其打印到 javafx 中的 observablelist -> tableview。然后我得到一个 arraylist,它接受 observablelist 并将它打印到 XML。但是收到错误消息 java.lang.IllegalAccessException: Class sun.reflect.misc.Trampoline can not access a member of class sample.Person with modifiers ""
Continuing ...
java.lang.Exception: XMLEncoder: discarding statement ArrayList.add(Person);
Continuing ...
但是我的人 class 是 public 并且我能够在控制台中输出所有内容。
这是主要的
package sample;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.stage.WindowEvent;
import java.beans.XMLEncoder;
import java.io.*;
import java.util.ArrayList;
public class Main extends Application {
Person person;
ArrayList<Person> aList = new ArrayList<>();
ObservableList<Person> obList = FXCollections.observableList(aList);
@Override
public void start(Stage primaryStage) {
//Create main gridpane.
GridPane gridpane = new GridPane();
primaryStage.setScene(new Scene(gridpane, 500, 500));
primaryStage.show();
//Create textfields/labels.
Label fNameLabel = new Label("First name:");
GridPane.setConstraints(fNameLabel, 0, 0);
fNameLabel.setMinWidth(150);
TextField fNameInput = new TextField();
fNameInput.setPromptText("Input first name...");
GridPane.setConstraints(fNameInput, 1, 0);
Label lNameLabel = new Label("Last name: ");
GridPane.setConstraints(lNameLabel, 0, 1);
TextField lNameInput = new TextField();
lNameInput.setPromptText("Input last name...");
GridPane.setConstraints(lNameInput, 1, 1);
Label ageLabel = new Label("Age: ");
GridPane.setConstraints(ageLabel, 0, 2);
TextField ageInput = new TextField();
ageInput.setPromptText("Input age...");
GridPane.setConstraints(ageInput, 1, 2);
//Adds tableview.
TableView<Person> tableView = new TableView<Person>();
tableView.setItems(obList);
TableColumn<Person, String> fNameTable = new TableColumn<Person, String>("First Name");
fNameTable.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
TableColumn<Person, String> lNameTable = new TableColumn<Person, String>("Last Name");
lNameTable.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));
TableColumn<Person, String> ageTable = new TableColumn<Person, String>("Age");
ageTable.setCellValueFactory(new PropertyValueFactory<Person, String>("age"));
fNameTable.setMinWidth(200);
lNameTable.setMinWidth(200);
ageTable.setMinWidth(100);
ColumnConstraints column1 = new ColumnConstraints();
ColumnConstraints column2 = new ColumnConstraints();
ColumnConstraints column3 = new ColumnConstraints();
column1.setPercentWidth(33);
column2.setPercentWidth(33);
column3.setPercentWidth(33);
gridpane.getColumnConstraints().addAll(column1, column2, column3);
tableView.getColumns().addAll(fNameTable, lNameTable, ageTable);
// Adds the addbutton and creates a person with the chosen name/age.
Button addButton = new Button("Add");
GridPane.setConstraints(addButton, 1, 3);
addButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
obList.add(new Person(fNameInput.getText(), lNameInput.getText(), ageInput.getText()));
tableView.setItems(obList);
tableView.refresh();
fNameInput.clear();
lNameInput.clear();
ageInput.clear();
}
});
Button xmlButton = new Button("XML");
GridPane.setConstraints(xmlButton, 0, 3);
xmlButton.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent event) {
write();
}
});
//Add everthing to gridpane.
gridpane.add(tableView, 0, 4, 3, 2);
gridpane.getChildren().addAll(fNameInput, addButton, fNameLabel, lNameInput, lNameLabel, ageLabel, ageInput, xmlButton);
//What to do when user exits.
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
System.out.println("Du stängde");
System.out.println(obList.size());
write();
Platform.exit();
System.exit(0);
}
});
}
public void write() {
XMLEncoder x = null;
try {
x = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("test.xml")));
System.out.println("Printar till XML");
System.out.println(aList.size()+" alist storlek");
for(int i = 0; i < aList.size(); i++) {
System.out.println(aList.get(i).getFirstName() + aList.get(i).getLastName() + aList.get(i).getAge());
}
x.writeObject(aList);
x.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
还有我的人class。
package sample;
import java.io.Serializable;
public class Person implements Serializable {
public String firstName;
public String lastName;
public String age;
Person() {
}
Person(String firstName, String lastName, String age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
您的构造函数不是 public,因此 XMLEncoder 无法访问它们。将它们更改为:
public Person()
public Person(String firstName, String lastName, String age)
我是 Java programming/javafx 的新手,正在尝试创建一个接受名字、姓氏和年龄输入的程序。将其打印到 javafx 中的 observablelist -> tableview。然后我得到一个 arraylist,它接受 observablelist 并将它打印到 XML。但是收到错误消息 java.lang.IllegalAccessException: Class sun.reflect.misc.Trampoline can not access a member of class sample.Person with modifiers ""
Continuing ...
java.lang.Exception: XMLEncoder: discarding statement ArrayList.add(Person);
Continuing ...
但是我的人 class 是 public 并且我能够在控制台中输出所有内容。
这是主要的
package sample;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.stage.WindowEvent;
import java.beans.XMLEncoder;
import java.io.*;
import java.util.ArrayList;
public class Main extends Application {
Person person;
ArrayList<Person> aList = new ArrayList<>();
ObservableList<Person> obList = FXCollections.observableList(aList);
@Override
public void start(Stage primaryStage) {
//Create main gridpane.
GridPane gridpane = new GridPane();
primaryStage.setScene(new Scene(gridpane, 500, 500));
primaryStage.show();
//Create textfields/labels.
Label fNameLabel = new Label("First name:");
GridPane.setConstraints(fNameLabel, 0, 0);
fNameLabel.setMinWidth(150);
TextField fNameInput = new TextField();
fNameInput.setPromptText("Input first name...");
GridPane.setConstraints(fNameInput, 1, 0);
Label lNameLabel = new Label("Last name: ");
GridPane.setConstraints(lNameLabel, 0, 1);
TextField lNameInput = new TextField();
lNameInput.setPromptText("Input last name...");
GridPane.setConstraints(lNameInput, 1, 1);
Label ageLabel = new Label("Age: ");
GridPane.setConstraints(ageLabel, 0, 2);
TextField ageInput = new TextField();
ageInput.setPromptText("Input age...");
GridPane.setConstraints(ageInput, 1, 2);
//Adds tableview.
TableView<Person> tableView = new TableView<Person>();
tableView.setItems(obList);
TableColumn<Person, String> fNameTable = new TableColumn<Person, String>("First Name");
fNameTable.setCellValueFactory(new PropertyValueFactory<Person, String>("firstName"));
TableColumn<Person, String> lNameTable = new TableColumn<Person, String>("Last Name");
lNameTable.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));
TableColumn<Person, String> ageTable = new TableColumn<Person, String>("Age");
ageTable.setCellValueFactory(new PropertyValueFactory<Person, String>("age"));
fNameTable.setMinWidth(200);
lNameTable.setMinWidth(200);
ageTable.setMinWidth(100);
ColumnConstraints column1 = new ColumnConstraints();
ColumnConstraints column2 = new ColumnConstraints();
ColumnConstraints column3 = new ColumnConstraints();
column1.setPercentWidth(33);
column2.setPercentWidth(33);
column3.setPercentWidth(33);
gridpane.getColumnConstraints().addAll(column1, column2, column3);
tableView.getColumns().addAll(fNameTable, lNameTable, ageTable);
// Adds the addbutton and creates a person with the chosen name/age.
Button addButton = new Button("Add");
GridPane.setConstraints(addButton, 1, 3);
addButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
obList.add(new Person(fNameInput.getText(), lNameInput.getText(), ageInput.getText()));
tableView.setItems(obList);
tableView.refresh();
fNameInput.clear();
lNameInput.clear();
ageInput.clear();
}
});
Button xmlButton = new Button("XML");
GridPane.setConstraints(xmlButton, 0, 3);
xmlButton.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent event) {
write();
}
});
//Add everthing to gridpane.
gridpane.add(tableView, 0, 4, 3, 2);
gridpane.getChildren().addAll(fNameInput, addButton, fNameLabel, lNameInput, lNameLabel, ageLabel, ageInput, xmlButton);
//What to do when user exits.
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
System.out.println("Du stängde");
System.out.println(obList.size());
write();
Platform.exit();
System.exit(0);
}
});
}
public void write() {
XMLEncoder x = null;
try {
x = new XMLEncoder(new BufferedOutputStream(new FileOutputStream("test.xml")));
System.out.println("Printar till XML");
System.out.println(aList.size()+" alist storlek");
for(int i = 0; i < aList.size(); i++) {
System.out.println(aList.get(i).getFirstName() + aList.get(i).getLastName() + aList.get(i).getAge());
}
x.writeObject(aList);
x.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
还有我的人class。
package sample;
import java.io.Serializable;
public class Person implements Serializable {
public String firstName;
public String lastName;
public String age;
Person() {
}
Person(String firstName, String lastName, String age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
您的构造函数不是 public,因此 XMLEncoder 无法访问它们。将它们更改为:
public Person()
public Person(String firstName, String lastName, String age)