今天我想为我的按钮提供动作。所以,我想要对新对象(用户)进行内联,并在其上做一些事情。但是我遇到了一些错误。我不能单独处理它。
用户类
package HomePlanner;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.StringProperty;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
/**
* Created by mscib on 11.08.2016.
*/
public class User {
private ArrayList<Expenses> listOfExpenses = new ArrayList<>();
private DoubleProperty cash;
public double getCash() {
return cash.get();
}
public DoubleProperty cashProperty() {
return cash;
}
public void setCash(double cash) {
this.cash.set(cash);
}
public void addExpense(String name, String category, LocalDate date, double cost){
if(name != null && category != null && date != null && (cash.getValue()-cost)>0){
Expenses exp = new Expenses(category,name,cost,date);
listOfExpenses.add(exp);
}
}
public User(double cash){
setCash(cash);
}
}
费用等级
package HomePlanner;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.StringProperty;
import java.time.LocalDate;
import java.util.Date;
import java.util.Set;
import java.util.TreeSet;
/**
* Created by mscib on 11.08.2016.
*/
public class Expenses {
private String category;
public String getCategory() {
return category;
}
LocalDate date;
private String name;
public String getName() {
return name;
}
private DoubleProperty cost;
public double getCost() {
return cost.get();
}
public DoubleProperty costProperty() {
return cost;
}
public void setCost(double cost) {
this.cost.set(cost);
}
public Expenses(String cat, String name, double cost,LocalDate date){
this.date= date;
category = cat;
this.name=name;
setCost(cost);
}
}
控制器类
package HomePlanner;
import com.sun.javafx.css.converters.StringConverter;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextField;
import javafx.scene.text.Text;
import javafx.event.ActionEvent;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Controller {
@FXML
TextField nameField;
@FXML
TextField costField;
@FXML
DatePicker datePicker;
@FXML
ComboBox<String> categoryCombo;
@FXML
Button addButton;
@FXML
Button deleteButton;
@FXML
Button editButton;
@FXML
public void initialize(){
User newUser = new User(100.0);
categoryCombo.getItems().clear();
categoryCombo.getItems().addAll(
"Dom","Jedzenie","Odzież","Rozrywka"
);
addButton.setOnAction(event -> System.out.println(newUser.getCash()));
}
}
主类
package HomePlanner;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class Main extends Application {
Parent root;
@Override
public void start(Stage primaryStage) throws Exception{
FXMLLoader loader = new FXMLLoader(getClass().getResource("ExpensesOverview.fxml"));
Parent root = loader.load();
Controller myController = loader.getController();
myController.initialize();
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
primaryStage.sizeToScene();
}
public static void main(String[] args) {
launch(args);
}
}
错误列表:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException:
/C:/Users/mscib/IdeaProjects/HomePlanner3/out/production/HomePlanner3/HomePlanner/ExpensesOverview.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2571)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at HomePlanner.Main.start(Main.java:16)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 1 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2566)
... 12 more
Caused by: java.lang.NullPointerException
at HomePlanner.User.setCash(User.java:27)
at HomePlanner.User.<init>(User.java:38)
at HomePlanner.Controller.initialize(Controller.java:37)
... 22 more
Exception running application HomePlanner.Main
祝福, 米哈尔。