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???
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);
}
}
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);
}
}
Here's my Inventory class:
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 +
'}';
}
}
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;
}
}
The 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>
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... 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