我是Spring的新手,所以我提前致歉!如果我直接从init方法加载我的fxml文档,那么一切都会顺利进行,Spring可以使用自动装配的存储库访问我的数据库。但是,当我更改场景的那一刻,Spring不再能够定位存储库/自动装配,并且它为涉及自动装配/我的存储库的所有内容抛出了空指针异常。我相信我有所有适当的注释(@Repository用于存储库,@ Controller用于控制器,@ Entity用于模型,等等),但似乎无济于事。在下面的示例中,初次加载时一切正常,但是如果我更改窗口或返回此窗口,则只会得到空指针异常。
主类/ Spring Boot应用程序
@SpringBootApplication
public class Project3368Application extends Application {
@Autowired
private ConfigurableApplicationContext springContext;
private Parent root;
public static void main(String[] args) {
launch();
}
@Override
public void start(Stage primaryStage) throws Exception{
// Note that the root was already populated in the init function
// root is of type Parent, and is a field in the class
primaryStage.setScene(new Scene(root,800,400));
System.out.println("showing page");
primaryStage.show();
}
@Override
@Lazy // ?
public void init() throws Exception {
springContext = SpringApplication.run(Project3368Application.class);
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("newJobInfo.fxml"));
fxmlLoader.setControllerFactory(springContext::getBean);
root = fxmlLoader.load();
super.init();
}
}
控制器
@Controller
public class NewJobInfoController implements Initializable {
public AnchorPane content;
@Autowired
MaterialRepository materialRepository;
@Autowired
ColorRepository colorRepository;
@Autowired
ShapeRepository shapeRepository;
public Button homeButton;
public ComboBox materialComboBox;
public ComboBox colorComboBox;
public ComboBox shapeComboBox;
public Label materialPriceLabel;
public Label colorPriceLabel;
public Label shapePriceLabel;
public Label totalLabel;
public void materialSelected(ActionEvent actionEvent) {
String materialPrice = " ";
//System.out.println(materialComboBox.getSelectionModel().getSelectedItem().toString());
//Iterable<Material> materials = materialRepository.findByMaterial("Cotton");
//materials.forEach(Material->System.out.println(Material.getPrice()));
List<Material> materials = materialRepository.findByMaterial(materialComboBox.getSelectionModel().getSelectedItem().toString());
for (Material material : materials) {
materialPrice = material.getPrice().toString();
}
materialPriceLabel.setText(materialPrice);
double num1;
double num2;
double num3;
double total;
num1 = Double.parseDouble(materialPriceLabel.getText());
num2 = Double.parseDouble(colorPriceLabel.getText());
num3 = Double.parseDouble(shapePriceLabel.getText());
total = num1 + num2 + num3;
totalLabel.setText(Double.toString(total));
}
public void colorSelected(ActionEvent actionEvent) {
String colorPrice = " ";
List<Color> colors = colorRepository.findByColor(colorComboBox.getSelectionModel().getSelectedItem().toString());
for (Color color : colors) {
colorPrice = color.getPrice().toString();
}
colorPriceLabel.setText(colorPrice);
double num1;
double num2;
double num3;
double total;
num1 = Double.parseDouble(materialPriceLabel.getText());
num2 = Double.parseDouble(colorPriceLabel.getText());
num3 = Double.parseDouble(shapePriceLabel.getText());
total = num1 + num2 + num3;
totalLabel.setText(Double.toString(total));
}
public void shapeSelected(ActionEvent actionEvent) {
String shapePrice = " ";
List<Shape> shapes = shapeRepository.findByShape(shapeComboBox.getSelectionModel().getSelectedItem().toString());
for (Shape shape : shapes) {
shapePrice = shape.getPrice().toString();
}
shapePriceLabel.setText(shapePrice);
double num1;
double num2;
double num3;
double total;
num1 = Double.parseDouble(materialPriceLabel.getText());
num2 = Double.parseDouble(colorPriceLabel.getText());
num3 = Double.parseDouble(shapePriceLabel.getText());
total = num1 + num2 + num3;
totalLabel.setText(Double.toString(total));
}
@Override
public void initialize(URL location, ResourceBundle resources) {
materialComboBox.getItems().removeAll(materialComboBox.getItems());
materialComboBox.getItems().addAll("Burlap", "Cotton", "Velvet", "Silk");
materialComboBox.getSelectionModel().select("Burlap");
colorComboBox.getItems().removeAll(colorComboBox.getItems());
colorComboBox.getItems().addAll("White", "Red", "Green", "Gold");
colorComboBox.getSelectionModel().select("White");
shapeComboBox.getItems().removeAll(shapeComboBox.getItems());
shapeComboBox.getItems().addAll("Sock", "Bone", "Candy Cane", "Paw Print");
shapeComboBox.getSelectionModel().select("Sock");
}
public void homeButtonPushed(ActionEvent actionEvent) throws IOException {
// Actually Change Screens
Parent manageCustomerViewParent = FXMLLoader.load(getClass().getResource("kanban.fxml"));
Scene manageCustomerViewScene = new Scene(manageCustomerViewParent);
// This line gets the Stage Information
Stage window = (Stage)((Node)actionEvent.getSource()).getScene().getWindow();
window.setScene(manageCustomerViewScene);
window.show();
}
public void submitButtonPushed(ActionEvent actionEvent) throws IOException {
// Actually Change Screens
Parent manageCustomerViewParent = FXMLLoader.load(getClass().getResource("kanban.fxml"));
Scene manageCustomerViewScene = new Scene(manageCustomerViewParent);
// This line gets the Stage Information
Stage window = (Stage)((Node)actionEvent.getSource()).getScene().getWindow();
window.setScene(manageCustomerViewScene);
window.show();
}
@FXML
private void backButtonPushed(ActionEvent actionEvent) throws IOException {
Parent parent = FXMLLoader.load(getClass().getResource("newJobPage1.fxml"));
Scene parentScene = new Scene(parent);
Stage window = (Stage)((Node)actionEvent.getSource()).getScene().getWindow();
window.setScene(parentScene);
window.show();
}
}
颜色存储库
package project_3368.project_3368;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface ColorRepository extends CrudRepository <Color,
Integer> {
List<Color> findByColor(String color);
}
错误
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1787)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8865)
at javafx.controls/javafx.scene.control.skin.ComboBoxListViewSkin.lambda$new$7(ComboBoxListViewSkin.java:182)
at java.base/java.util.function.Consumer.lambda$andThen$0(Consumer.java:65)
at javafx.controls/com.sun.javafx.scene.control.LambdaMultiplePropertyChangeListenerHandler.lambda$new$1(LambdaMultiplePropertyChangeListenerHandler.java:49)
at javafx.base/javafx.beans.value.WeakChangeListener.changed(WeakChangeListener.java:86)
at javafx.base/com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:360)
at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.base/javafx.beans.property.ObjectPropertyBase.fireValueChangedEvent(ObjectPropertyBase.java:106)
at javafx.base/javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:113)
at javafx.base/javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:147)
at javafx.controls/javafx.scene.control.ComboBoxBase.setValue(ComboBoxBase.java:151)
at javafx.controls/javafx.scene.control.ComboBox.updateValue(ComboBox.java:520)
at javafx.controls/javafx.scene.control.ComboBox.access$200(ComboBox.java:194)
at javafx.controls/javafx.scene.control.ComboBox$3.changed(ComboBox.java:503)
at javafx.base/com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:360)
at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.base/javafx.beans.property.ReadOnlyObjectPropertyBase.fireValueChangedEvent(ReadOnlyObjectPropertyBase.java:74)
at javafx.base/javafx.beans.property.ReadOnlyObjectWrapper.fireValueChangedEvent(ReadOnlyObjectWrapper.java:102)
at javafx.base/javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:113)
at javafx.base/javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:147)
at javafx.controls/javafx.scene.control.SelectionModel.setSelectedItem(SelectionModel.java:105)
at javafx.controls/javafx.scene.control.ComboBox$ComboBoxSelectionModel.lambda$new$0(ComboBox.java:560)
at javafx.base/com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:136)
at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.base/javafx.beans.property.ReadOnlyIntegerPropertyBase.fireValueChangedEvent(ReadOnlyIntegerPropertyBase.java:72)
at javafx.base/javafx.beans.property.ReadOnlyIntegerWrapper.fireValueChangedEvent(ReadOnlyIntegerWrapper.java:102)
at javafx.base/javafx.beans.property.IntegerPropertyBase.markInvalid(IntegerPropertyBase.java:114)
at javafx.base/javafx.beans.property.IntegerPropertyBase.set(IntegerPropertyBase.java:148)
at javafx.controls/javafx.scene.control.SelectionModel.setSelectedIndex(SelectionModel.java:69)
at javafx.controls/javafx.scene.control.SingleSelectionModel.updateSelectedIndex(SingleSelectionModel.java:215)
at javafx.controls/javafx.scene.control.SingleSelectionModel.select(SingleSelectionModel.java:149)
at javafx.controls/javafx.scene.control.skin.ComboBoxListViewSkin.lambda$createListView$9(ComboBoxListViewSkin.java:545)
at javafx.base/com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:348)
at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
at javafx.base/javafx.beans.property.ReadOnlyIntegerPropertyBase.fireValueChangedEvent(ReadOnlyIntegerPropertyBase.java:72)
at javafx.base/javafx.beans.property.ReadOnlyIntegerWrapper.fireValueChangedEvent(ReadOnlyIntegerWrapper.java:102)
at javafx.base/javafx.beans.property.IntegerPropertyBase.markInvalid(IntegerPropertyBase.java:114)
at javafx.base/javafx.beans.property.IntegerPropertyBase.set(IntegerPropertyBase.java:148)
at javafx.controls/javafx.scene.control.SelectionModel.setSelectedIndex(SelectionModel.java:69)
at javafx.controls/javafx.scene.control.MultipleSelectionModelBase.select(MultipleSelectionModelBase.java:412)
at javafx.controls/javafx.scene.control.MultipleSelectionModelBase.clearAndSelect(MultipleSelectionModelBase.java:360)
at javafx.controls/javafx.scene.control.ListView$ListViewBitSetSelectionModel.clearAndSelect(ListView.java:1460)
at javafx.controls/com.sun.javafx.scene.control.behavior.CellBehaviorBase.simpleSelect(CellBehaviorBase.java:282)
at javafx.controls/com.sun.javafx.scene.control.behavior.CellBehaviorBase.doSelect(CellBehaviorBase.java:246)
at javafx.controls/com.sun.javafx.scene.control.behavior.CellBehaviorBase.mousePressed(CellBehaviorBase.java:176)
at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3876)
at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1300(Scene.java:3604)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1874)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2613)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
at java.base/java.lang.Thread.run(Thread.java:844)
由以下原因引起:java.lang.reflect.InvocationTargetException 在java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(本机方法) 在java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在java.base / jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.base / java.lang.reflect.Method.invoke(Method.java:564) 在com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76) 在jdk.internal.reflect.GeneratedMethodAccessor33.invoke(未知来源) 在java.base / jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.base / java.lang.reflect.Method.invoke(Method.java:564) 在javafx.base / com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:275) 在javafx.fxml / com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83) 在javafx.fxml / javafx.fxml.FXMLLoader $ MethodHandler.invoke(FXMLLoader.java:1782) ...更多91 造成原因:java.lang.NullPointerException 在project_3368.project_3368.NewJobInfoController.colorSelected(NewJobInfoController.java:85) ...还有102个