1

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
Wilson
  • 95
  • 6
  • 4
    What error(s) are you getting? – James_D Oct 15 '15 at 22:56
  • I just added the whole list of errors that I'm getting. I'm really at a loss. I mean logically it checks out (I wrote a command line app with almost identical code) but IntelliJ doesn't like it at all. Is it something with the Event Handler? – Wilson Oct 15 '15 at 23:23
  • I'll also add that as soon as I remove the line `mInventory.addMotorcycle(motorcycle);` the code executes without a problem. – Wilson Oct 15 '15 at 23:30
  • You're getting a `NullPointerException` when you run the code, so I don't know why you think it has anything to do with your IDE. I assume `mInventory.addMotocycle(...)` is line 26? – James_D Oct 15 '15 at 23:31
  • Well, I am new to this so it gets a little confusing. That's correct. It's line 26. What could cause a NullPointerException if am passing in the object created using the newMotorcycle() method? – Wilson Oct 15 '15 at 23:39
  • Where do you call the usercreatebike ? – Yassin Hajaj Oct 15 '15 at 23:43
  • 1
    The error indicates that `mInventory` is `null`, which doesn't make much sense given your code. Make sure all the files are saved and do a clean build of the project (no idea how you do that in IntelliJ but it should be obvious enough). – James_D Oct 15 '15 at 23:43
  • I guess if you post the FXML that's the only thing from stopping others from testing your code. And in IntelliJ, from the Build menu, try "Rebuild Project" – James_D Oct 15 '15 at 23:51
  • @YassinHajaj userCreateAction method takes an ActionEvent. It only executes when the user hits the submit button after filling in the required TextFields for Motorcycle. It should add the bike to the inventory. – Wilson Oct 15 '15 at 23:55
  • Please change the access modifier of your List and try to Check if it is null before adding the object, sysout the BOOLEAN for example – Yassin Hajaj Oct 16 '15 at 00:00
  • Well, I downloaded IntelliJ, copied the code into a new project, and ran it, and it ran just fine... Try adding the line `System.out.println(mInventory);` immediately before the line that is generating the error, and see what it does. – James_D Oct 16 '15 at 00:04
  • Ok. I added the `System.out.println(mInventory);` and it returns null because it hasn't been populated yet. – Wilson Oct 16 '15 at 00:17
  • It shouldn't return `null` just because it's not populated. `null` means it's not initialized, which it is by the line `private Inventory mInventory = new Inventory();`. Are you certain the code you have posted here is an exact copy of the code you are running? – James_D Oct 16 '15 at 00:20
  • @James_D I just posted all of the code into a new project and it executed without problems. I have no idea what the issue was. I cleaned the code, rebuilt the project, etc. I'll just go with it! Thank you for your help. – Wilson Oct 16 '15 at 00:24

1 Answers1

0

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

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.

JSamir
  • 1,057
  • 1
  • 10
  • 20