- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以有人请让我知道我在这里做错了什么。我的目标是将摩托车添加到 mInventory(我已创建并从单独的包导入)。但是,我的代码中仍然出现错误。我参加了 Treehouse 的 Java 类(class),现在我正尝试使用 IntelliJ,但我不禁觉得他们真的只是把我们扔到那里,而没有真正解释 IntelliJ 的工作原理。有人给我一些建议吗???
这是 Main.java:
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("/fxml/sample.fxml"));
primaryStage.setTitle("Store Motorcycle");
primaryStage.setScene(new Scene(root, 500, 400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
这是我的 Controller 类:
package sample;
import javafx.event.ActionEvent;
import sample.com.gardnerwebideas.Inventory;
import sample.com.gardnerwebideas.Motorcycle;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import java.util.ArrayList;
public class Controller {
@FXML private TextField bikeYear;
@FXML private TextField bikeMake;
@FXML private TextField bikeModel;
@FXML private TextField bikeColor;
@FXML private TextField bikePrice;
private Inventory mInventory = new Inventory();
public void userCreateBike(ActionEvent actionEvent) {
Motorcycle motorcycle = newMotorcycle();
System.out.printf("Adding: %s", motorcycle);
mInventory.addMotorcycle(motorcycle);
}
public Motorcycle newMotorcycle() {
String year = bikeYear.getText();
String make = bikeMake.getText();
String model = bikeModel.getText();
String color = bikeColor.getText();
String price = bikePrice.getText();
return new Motorcycle(year, make, model, color, price);
}
}
这是我的库存类:
package sample.com.gardnerwebideas;
import java.util.ArrayList;
import java.util.List;
public class Inventory {
private List<Motorcycle> mInventory;
public Inventory() {
mInventory = new ArrayList<>();
}
public void addMotorcycle(Motorcycle motorcycle) {
mInventory.add(motorcycle);
}
public int getInventoryCount() {
return mInventory.size();
}
@Override
public String toString() {
return "Inventory{" +
"mInventory=" + mInventory +
'}';
}
}
最后是摩托车类:
package sample.com.gardnerwebideas;
public class Motorcycle {
private String mYear;
private String mMake;
private String mModel;
private String mColor;
private String mPrice;
public Motorcycle(String year, String make, String model, String color, String price) {
mYear = year;
mMake = make;
mModel = model;
mColor = color;
mPrice = price;
}
public String getYear() {
return mYear;
}
public String getMake() {
return mMake;
}
public String getModel() {
return mModel;
}
public String getColor() {
return mColor;
}
public String getPrice() {
return mPrice;
}
@Override
public String toString() {
return "Motorcycle:" +
"Year: " + mYear + "\n" +
"Make: " + mMake + "\n" +
"Model: " + mModel + "\n" +
"Color: " + mColor + "\n" +
"Price: " + mPrice + "\n";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Motorcycle that = (Motorcycle) o;
if (mYear != null ? !mYear.equals(that.mYear) : that.mYear != null) return false;
if (mMake != null ? !mMake.equals(that.mMake) : that.mMake != null) return false;
if (mModel != null ? !mModel.equals(that.mModel) : that.mModel != null) return false;
if (mColor != null ? !mColor.equals(that.mColor) : that.mColor != null) return false;
return !(mPrice != null ? !mPrice.equals(that.mPrice) : that.mPrice != null);
}
@Override
public int hashCode() {
int result = mYear != null ? mYear.hashCode() : 0;
result = 31 * result + (mMake != null ? mMake.hashCode() : 0);
result = 31 * result + (mModel != null ? mModel.hashCode() : 0);
result = 31 * result + (mColor != null ? mColor.hashCode() : 0);
result = 31 * result + (mPrice != null ? mPrice.hashCode() : 0);
return result;
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.text.Text?>
<?import javafx.scene.control.TextField?>
<GridPane alignment="CENTER"
stylesheets="/css/style.css"
xmlns="http://javafx.com/javafx/8"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="sample.Controller">
<children>
<Text styleClass="title"
text="Add a motorcycle:"
GridPane.columnSpan="2"
GridPane.halignment="CENTER"
GridPane.rowIndex="0" />
<Text styleClass="textTitle"
text="Year:"
GridPane.columnIndex="0"
GridPane.rowIndex="1" />
<TextField fx:id="bikeYear"
GridPane.columnIndex="1"
GridPane.rowIndex="1" />
<Text styleClass="textTitle"
text="Make:"
GridPane.columnIndex="0"
GridPane.rowIndex="2" />
<TextField fx:id="bikeMake"
alignment="TOP_LEFT"
GridPane.columnIndex="1"
GridPane.rowIndex="2" />
<Text styleClass="textTitle"
text="Model:"
GridPane.columnIndex="0"
GridPane.rowIndex="3" />
<TextField fx:id="bikeModel"
GridPane.columnIndex="1"
GridPane.rowIndex="3" />
<Text styleClass="textTitle"
text="Color:"
GridPane.columnIndex="0"
GridPane.rowIndex="4" />
<TextField fx:id="bikeColor"
GridPane.columnIndex="1"
GridPane.rowIndex="4" />
<Text styleClass="textTitle"
text="Price:"
GridPane.columnIndex="0"
GridPane.rowIndex="5" />
<TextField fx:id="bikePrice"
GridPane.columnIndex="1"
GridPane.rowIndex="5" />
<Button onAction="#userCreateBike"
styleClass="button"
text="Submit"
GridPane.columnSpan="2"
GridPane.halignment="CENTER"
GridPane.rowIndex="6" />
</children>
</GridPane>
如果有任何不清楚的地方,让我重复一遍...导致我出现问题的是以下行:mInventory.addMotorcycle(摩托车);
除此之外,我的代码可以工作。它从我的 TextFields 获取信息,甚至将其打印到控制台......但它不会将新的摩托车添加到 mInventory。
哦...这是我收到的错误消息...
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
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:497)
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:497)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 45 more
Caused by: java.lang.NullPointerException
at sample.Controller.userCreateBike(Controller.java:26)
... 55 more
最佳答案
您的 Controller 没有构造函数,也没有 initialize()
方法。
首先在现在所在的位置定义成员private Inventory mInventory;
,然后在构造函数或initialize()
方法中创建新对象(mInventory = new Inventory();
)。
关于java - 无法使用包中的方法将对象存储在 List<Object> 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33159848/
我想使用 R 预定义这样的列表 DATA<-list( list(list(),list(),list()), list(list(),list(),list()), list(list(),l
如何将一个列表添加到另一个列表,返回一个列表的列表? foo :: [a] -> [a] -> [[a]] 例如,我想要的结果是: foo [1,2] [3,4] 将是 [[1,2], [3,4]]。
我还没有在这里找到类似问题的解决方案,所以我会寻求你的帮助。 有 2 个列表,其中之一是列表列表: categories = ['APPLE', 'ORANGE', 'BANANA'] test_re
这个问题不同于Converting list of lists / nested lists to list of lists without nesting (这会产生一组非常具体的响应,但无法解决
原始列表转换为 List正好。为什么原始列表的列表不能转换为 List 的列表? { // works List raw = null; List wild = raw; } {
在下面的代码中,get()被调用并将其结果分配给类型为 List> 的变量. get()返回 List>并在类型参数为 T 的实例上调用设置为 ? ,所以它应该适合。 import java.util
原始列表转换为 List正好。为什么原始列表的列表不能转换为 List 的列表? { // works List raw = null; List wild = raw; } {
在insufficiently-polymorphic 作者说: def foo[A](fst: List[A], snd: List[A]): List[A] There are fewer way
我有下面的代码有效。 class ListManipulate(val list: List, val blockCount: Int) { val result: MutableList>
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 5 年前。 Improve this ques
在 scala (2.9) 中转换列表列表的最佳方法是什么? 我有一个 list : List[List[A]] 我想转换成 List[A] 如何递归地实现这一点?或者还有其他更好的办法吗? 最佳答案
我编写了这个函数来确定给定元素是否存储在元组列表的列表中,但目前它只搜索第一个列表。我将如何搜索其余列表? fun findItem (name : command, ((x,y)::firstlis
我创建了一个类名 objectA,它有 4 个变量:约会时间;字符串文本;变量 1,变量 2 我需要创建一个 ObjectA() 列表。然后首先按时间对它们进行分组,其次按 var1,然后按 var2
我有一套说法 char={'J','A'} 和列表的列表 content = [[1,'J', 2], [2, 'K', 3], [2, 'A', 3], [3,'A', 9], [5, 'J', 9
我有以下列表 List >>> titles = new ArrayList >>> ();我想访问它的元素,但我不知道该怎么做.. 该列表有 1 个元素,它又包含 3 个元素,这 3 个元素中的
转换 List[List[Long]] 的最佳方法是什么?到 List[List[Int]]在斯卡拉? 例如,给定以下类型列表 List[List[Long]] val l: List[List[Lo
我有一个来自 Filereader (String) 的 List-List,如何将其转换为 List-List (Double):我必须返回一个包含 line-Array 的第一个 Values 的
我收集了List> 。我需要将其转换为List> 。这是我尝试过的, List> dataOne = GetDataOne(); var dataTwo = dataOne.Select(x => x
这个问题在这里已经有了答案: Cannot convert from List to List> (3 个答案) 关闭 7 年前。 我没有得到这段代码以任何方式编译: List a = new Ar
这个问题在这里已经有了答案: Cannot convert from List to List> (3 个答案) 关闭 7 年前。 我没有得到这段代码以任何方式编译: List a = new Ar
我是一名优秀的程序员,十分优秀!