Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't resolve method add ArrayList

I have this code, and add is marked red with error: "Cannot resolve method 'add(java.util.ArrayList<com.example.InventoryItems>)'"

public class Player {
    private String name;
    private int lives;
    private int score;
    private int level;
    private Weapon weapon;
    private ArrayList<InventoryItems> inventoryItems;

    public Player(String name) {
        this.name = name;
        this.lives = 3;
        this.score = 0;
        this.level = 1;
        inventoryItems = new ArrayList<InventoryItems>();
        setDefaultWeapon();
    }

    public void setDefaultWeapon() {
        this.weapon = new Weapon("Dagger", 3, WeaponType.Wooden);
    }

    public void setDefaultInventoryItems() {
        InventoryItems rubyNecklace = new InventoryItems("Ruby Necklace", ItemType.Amulet);
        rubyNecklace.add(inventoryItems);

    }

This is InventoryItems.java

package com.example;

import java.util.ArrayList;

/**
 * Created by Pawel on 1/21/16.
 */

enum ItemType { Armor, Ring, Amulet, Junk, Weapon }

public class InventoryItems {
    private String name;
    private ItemType itemtype;

    public InventoryItems(String name, com.example.ItemType itemtype) {
        super();
        this.name = name;
        this.itemtype = itemtype;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public com.example.ItemType getItemtype() {
        return itemtype;
    }

    public void setItemtype(com.example.ItemType itemtype) {
        this.itemtype = itemtype;
    }

}

Should I add the add method to InventoryItems? Why isn't it just possible to add rubyNecklace to ArrayList?

like image 801
Pawel Jezierski Avatar asked Sep 17 '25 18:09

Pawel Jezierski


1 Answers

I think you got it all mixed up. Change

rubyNecklace.add(inventoryItems);

to

inventoryItems.add(rubyNecklace);

inventoryItems is the ArrayList, & rubyNecklace is the item to be added.

like image 126
Mohammed Aouf Zouag Avatar answered Sep 19 '25 08:09

Mohammed Aouf Zouag