Пишу программу на java с использованием javafx, хочу сделать так, чтобы данные, которые я ввожу и получаю после работы программы, сохранялись и выводились на экран при перезапуске программы, пытался сделать это при помощи сериализации, но, по какой-то причине, программа начала выдавать ошибку:
Подскажите, пожалуйста, как сделать так, чтобы она работала как надо
package sample;
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("sample.fxml"));
primaryStage.setTitle("Трудочасы 3.0");
primaryStage.setScene(new Scene(root, 600, 400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
package sample;
import java.io.*;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javax.script.ScriptException;
import javafx.scene.control.*;
public class Controller {
@FXML
private TextField dni;
@FXML
private TextField num;
@FXML
private TextField minRes;
@FXML
private TextField crRes;
@FXML
private TextField maxRes;
public static void main(String[] args) { //Проблема, скорее всего, где-то здесь
Controller controller = new Controller();
try {
FileOutputStream file = new FileOutputStream("Trood.ser");
ObjectOutputStream so = new ObjectOutputStream(file);
so.writeObject(controller);
so.close();
}
catch (FileNotFoundException ex){
System.out.println("Невозможно создать файл");
} catch (IOException e) {
e.printStackTrace();
}
controller=null;
try {
ObjectInputStream is = new ObjectInputStream(new FileInputStream("Trood.ser"));
Controller one = (Controller) is.readObject();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@FXML
private void process(ActionEvent event) throws ScriptException {
formula(2,1,2,1,8);
formula(1,0.5,1,2,8);
formula(1,0.5,0.5,3,7);
}
@FXML
private void formula(double a, double b, double c, int o,double l){
double d = Double.parseDouble(dni.getText()); //Число рабояих дней
double s = l;//Время на сон
double e = 1;//Время на еду
double k = a;//Не входящее не в одну категорию
double y = b;//утрение+вечерние мероприятия
double n = 0.83;//Неизбежные мероприятия
double r = c;//Общий резерв
double Vr = (24-s-e-k-y-n-r-0.83)*d;//Время на работу без учёта отдыха
//0.83 - доля времени на общение, распределённая на день, так как общение обычно происходит 5 часов, то при разделе на 6 дней оно будет равно 0.83
double pod=25;//время одного подхода
double kolpod=4;//Колличество подходов
double p = Double.parseDouble(num.getText());//Номер системы нагрузки
if (p==2){
pod = 30;
kolpod =5;
}
if (p==3){
pod = 35;
kolpod =6;
}
if (p==4){
pod = 40;
kolpod =7;
}
if (p==5){
pod = 45;
kolpod =8;
}
if (p==6){
pod = 50;
kolpod =9;
}
if (p==7){
pod = 55;
kolpod =10;
}
double Srp=pod*kolpod; //время работы в цикле
double So = 5*kolpod+20; //время отдыха в цикле
double Vobsh = Srp+So; // время одного цикла с учётом работы и отдыха
double Rrv = Vr/Vobsh*Srp; //Реальное времяф работы
if (o==1)
minRes.setText(String.valueOf(Rrv));
else if (o==2)
crRes.setText(String.valueOf(Rrv));
else
maxRes.setText(String.valueOf(Rrv));
}
}
Графическая часть:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<HBox prefHeight="31.0" prefWidth="600.0">
<VBox.margin>
<Insets top="40.0" />
</VBox.margin>
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Введите число рабочих дней:">
<HBox.margin>
<Insets left="30.0" />
</HBox.margin></Text>
<TextField fx:id="dni">
<HBox.margin>
<Insets left="10.0" />
</HBox.margin>
</TextField>
</children>
</HBox>
<HBox prefHeight="49.0" prefWidth="600.0">
<children>
<Button mnemonicParsing="false" onAction="#process" prefHeight="46.0" prefWidth="121.0" text="Расчитать">
<HBox.margin>
<Insets left="400.0" />
</HBox.margin>
</Button>
</children>
</HBox>
<HBox prefHeight="8.0" prefWidth="600.0">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Введите номер системы:">
<HBox.margin>
<Insets left="30.0" />
</HBox.margin></Text>
<TextField fx:id="num">
<HBox.margin>
<Insets left="37.0" />
</HBox.margin>
</TextField>
</children>
<VBox.margin>
<Insets />
</VBox.margin>
</HBox>
<HBox prefHeight="24.0" prefWidth="600.0">
<VBox.margin>
<Insets top="70.0" />
</VBox.margin>
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Минимальное рабочее время:">
<HBox.margin>
<Insets left="200.0" />
</HBox.margin>
</Text>
<TextField fx:id="minRes">
<HBox.margin>
<Insets left="20.0" />
</HBox.margin>
</TextField>
</children>
</HBox>
<HBox prefHeight="24.0" prefWidth="600.0">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Среднее рабочее время:">
<HBox.margin>
<Insets left="200.0" />
</HBox.margin>
</Text>
<TextField fx:id="crRes">
<HBox.margin>
<Insets left="51.0" />
</HBox.margin>
</TextField>
</children>
<VBox.margin>
<Insets top="20.0" />
</VBox.margin>
</HBox>
<HBox prefHeight="27.0" prefWidth="600.0">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Максимальное рабочее время:" wrappingWidth="174.2890625">
<HBox.margin>
<Insets left="200.0" />
</HBox.margin>
</Text>
<TextField fx:id="maxRes">
<HBox.margin>
<Insets left="12.0" />
</HBox.margin>
</TextField>
</children>
<VBox.margin>
<Insets top="20.0" />
</VBox.margin>
</HBox>
</children>
</VBox>