-2

I have an generic array class that stores other objects. When I try to get the object values from the generic array it prints the insert objects classpath. How can I access the value?

public class Shop implements Initializable {
    @FXML private TextField name;
    @FXML private TextField quantity;
    @FXML private TextArea list;

    MyArrayList<Item> array = new MyArrayList<>();

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

    @FXML
    private void addItem() {

        int q = Integer.parseInt(quantity.getText());
        String n = name.getText();

        Item item = new Item(n,q);

        MyArrayList.getInstance().add(item);

        System.out.println(MyArrayList.getInstance().getItem(0));
        //Outputs sample.Item@1674184b instead of the value Banana.

    }
mattesj
  • 549
  • 2
  • 8
  • 20
  • `System.out.println(MyArrayList.getInstance().getItem(0).getName());`. If there is no `getName()` method, add one. – NAMS Mar 15 '17 at 21:35
  • It says "Can't resolve method". I have a getName() method in the Item class but I can't access it through the MyArrayList class. – mattesj Mar 15 '17 at 21:38
  • `System.out.println(MyArrayList.getInstance().getItem(0).getClass().getN‌​ame());` – bcr666 Mar 15 '17 at 21:40
  • 1
    Objects don't have classpaths, programs do. What do you mean by the "objects [sic] classpath"? – Lew Bloch Mar 15 '17 at 23:05
  • If there is a `getName()` method in `Item` and `MyArrayList.getInstance().getItem(0).getName()` is not compiling then there is something wrong with the return types of your methods. Probably you need to post at least the API of your `MyArrayList` class... – James_D Mar 16 '17 at 03:26

1 Answers1

1

So your are putting an Integer and a String into the Item? MyArrayList.getInstance().getItem(0).getName() or MyArrayList.getInstance().getItem(0).getQuantity() should print out the values u put it.

If you dont have the get methods create them, or if the fields are public you could access them directly.

U might want to overwrite the toString() method if u want to get all the values from the class listed like.

 @Overwrite
public String toString(){
    return name + "," + quantity;
}

And call

System.out.println(MyArrayList.getInstance().getItem(0).toString());
leurer
  • 441
  • 3
  • 10