无法将对象存储在List< Object>中从包的方法? [英] Cannot store Object in List<Object> with method from package?

查看:89
本文介绍了无法将对象存储在List< Object>中从包的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以有人请让我知道我在这里做错了什么.我的目标是将Motorcycle添加到mInventory(我已经从单独的程序包中创建并导入了它).但是,我继续在我的代码中出现错误.我从Treehouse上了Java课程,现在我正尝试着去使用IntelliJ弄脏我的手,但是我忍不住觉得他们真的把我们扔了出去,而没有真正解释IntelliJ的工作原理.有人给我一些建议吗?

So someone please let me know what I am doing wrong here. My goal is to add the Motorcycle to mInventory (which I have created and imported from a separate package). However, I continue getting errors in my code. I took the Java course from Treehouse and now I'm trying to get out there and get my hands dirty using IntelliJ, but I can't help but feel like they really just threw us out there without really explaining how IntelliJ works. Anyone got some advice for me???

这是Main.java:

Here's 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类:

Here's my Controller class:

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 +
                '}';
    }
}

最后是Motorcycle类:

And finally the Motorcycle class:

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(motorcycle); 除此之外,我的代码还可以工作.它从我的TextFields中获取信息,甚至可以将其打印到控制台...但是它不会将新的Motorcycle添加到mInventory.

If any of that is unclear let me repeat it... what is causing me issues is the line: mInventory.addMotorcycle(motorcycle); Apart from that, my code works. It gets info from my TextFields and will even print it out to the console... but it won't add the new Motorcycle to mInventory.

OH ...这是我收到的错误消息...

OH... and here are the error messages I'm getting...

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

推荐答案

您的控制器没有构造函数,也没有initialize()方法.

Your controller does not have a constructor, nor the initialize() Method.

首先在现在的位置定义成员private Inventory mInventory;,然后在构造函数或initialize()方法中创建新对象(mInventory = new Inventory();).

First define the member private Inventory mInventory; at the place where it is now, then create the new object (mInventory = new Inventory();) in the constructor or initialize() Method.

这篇关于无法将对象存储在List&lt; Object&gt;中从包的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆