Issue
Im quite new to Java programming/javafx and are trying to create a program that takes input of first name, last name, age. Prints it to a observablelist -> tableview in javafx. Then I got an arraylist that takes the observablelist and should print it to XML. But getting the error message 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 ...
But my Person class are public and im able to output everything in console.
This is the main
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);
}
}
And my person 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;
}
}
Solution
Your constructors are not public, so XMLEncoder is not able to access them. Change them to:
public Person()
public Person(String firstName, String lastName, String age)
Answered By - VGR