gpt4 book ai didi

java - XMLEncoder 拒绝写入文件

转载 作者:行者123 更新时间:2023-12-01 19:42:58 25 4
gpt4 key购买 nike

我对 Java 编程/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 ""
继续...
java.lang.Exception: XMLEncoder: 丢弃语句 ArrayList.add(Person);
继续...

但是我的 Person 类是公共(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);
}
}

还有我的人课。

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 - XMLEncoder 拒绝写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59156101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com