Results 1 to 9 of 9
  1. #1
    JohnPringle83 is offline Member
    Join Date
    May 2011
    Posts
    64
    Rep Power
    0

    Default RPG inventory Problem

    I am trying to create an inventory for an RPG and I was creating a method to display the contents of the bag when I came across a problem I do not know how to solve. Can anyone tell me what I am doing wrong?
    The error I get is on the word items in the enhanced for loop.
    Error:
    Can only iterate over an array or an instance of java.lang.Iterable

    Java Code:
    import java.awt.List;
    
    public class Inventory {
    	private List items;
    	
    	void addItem(String item){
    		items.add(item);
    	}
    	
    	void removeItem(String item){
    		items.remove(item);
    	}
    	
    	void displayBag(){
    		System.out.println("The items in the bag are :/n");
    		for(int x: items){
    			System.out.println(x);
    		}
    	}
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What is the exact error message(copy/paste)?

    Also, the add and remove methods add and remove strings but you are using int in the for each loop. You may also want to consider using java.util.List.
    Last edited by sunde887; 05-08-2011 at 03:34 AM.

  3. #3
    JohnPringle83 is offline Member
    Join Date
    May 2011
    Posts
    64
    Rep Power
    0

    Default This is the error

    I did include the error in the original message but here it is again and it is the actual error message.
    Java Code:
    Can only iterate over an array or an instance of java.lang.Iterable

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're using the wrong List class, java.awt.List. You want to use java.util.List. You will also want to initialize the List as either a new ArrayList or new LinkedList before using it.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    As Fubar said, you are using List (Java Platform SE 6) , this list doesn't implement iterable, which is what is used in a for each loop. A for each loop iterates through the list. You may want to look up how to generify the list as well. If you are storing strings in the list you can't print ints.

  6. #6
    JohnPringle83 is offline Member
    Join Date
    May 2011
    Posts
    64
    Rep Power
    0

    Default I now have a different problem

    I changed the List that I was using and it removes all of the errors in eclipse but when I compile and run I get this console error:

    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    	at Inventory.addItem(Inventory.java:7)
    	at Main.main(Main.java:6)
    Here is the code:

    Java Code:
    import java.util.List;
    
    public class Inventory {
    	private List<String> items;
    	
    	void addItem(String item){
    		items.add(item);
    	}
    	
    	void removeItem(String item){
    		items.remove(item);
    	}
    	
    	void displayBag(){
    		System.out.println("The items in the bag are:/n");
    		for(String x: items){
    			System.out.println(x);
    		}
    	}
    }
    Here is what I have in Main:

    Java Code:
    public class Main {
    	public static void main(String[] args){
    		Human John = new Human();
    		
    		John.bag.addItem("Sword");
    		John.bag.addItem("Shield");
    		John.bag.addItem("Weed");
    		John.bag.displayBag();
    	}
    }
    I know it's irrelevant, but here is my Human class so you can see the whole picture:

    Java Code:
    public class Human extends Race{
    	private byte walkingSpeed = 5;
    	private byte runningSpeed = 10;
    	private byte strength = 25;
    	private byte magic = 5;
    	byte gold = 100;
    	
    	Inventory bag = new Inventory();
    	
    	byte getSpeed(char x) {
    		byte speed = 0;
    		if(x=='w'){
    			speed=walkingSpeed;
    		}else if(x=='r'){
    			speed=runningSpeed;
    		}
    		return speed;
    	}
    	
    	byte getStrength() {
    		return strength;
    	}
    	
    	byte getMagic() {
    		return magic;
    	}
    
    	void increaseSpeed() {
    		walkingSpeed++;
    		runningSpeed+=2;
    	}
    
    	void increaseStrength() {
    		strength++;
    	}
    
    	void increaseMagic() {
    		magic++;
    	}
    	
    	void levelsUp(){
    		walkingSpeed++;
    		runningSpeed+=2;
    		strength++;
    		magic++;
    	}
    	
    }
    My Race class is abstract with abstract methods just so I don't forget anything when creating my races.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JohnPringle83 View Post
    I changed the List that I was using and it removes all of the errors in eclipse but when I compile and run I get this console error:

    Java Code:
    Exception in thread "main" java.lang.NullPointerException
    	at Inventory.addItem(Inventory.java:7)
    	at Main.main(Main.java:6)

    Again as I said before:

    You're using the wrong List class, java.awt.List. You want to use java.util.List. You will also want to initialize the List as either a new ArrayList or new LinkedList before using it.

  8. #8
    JohnPringle83 is offline Member
    Join Date
    May 2011
    Posts
    64
    Rep Power
    0

    Default I got it working

    The top of my Inventory Class now looks like this:

    Java Code:
    import java.util.ArrayList;
    import java.util.List;
    
    public class Inventory {
    	private List<String> items = new ArrayList<String>();

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by JohnPringle83 View Post
    The top of my Inventory Class now looks like this:

    Java Code:
    import java.util.ArrayList;
    import java.util.List;
    
    public class Inventory {
    	private List<String> items = new ArrayList<String>();
    There you go! Congrats!

Similar Threads

  1. Re: Help doing GUI in sales and inventory
    By cuddles14 in forum Eclipse
    Replies: 0
    Last Post: 03-08-2011, 01:53 AM
  2. Inventory Part 4 - Almost done!!!
    By ladykrimson in forum New To Java
    Replies: 3
    Last Post: 11-09-2010, 07:03 PM
  3. My Inventory Program
    By ladykrimson in forum New To Java
    Replies: 28
    Last Post: 11-01-2010, 10:06 PM
  4. Help with Inventory Program, Please
    By NewKidjava in forum New To Java
    Replies: 4
    Last Post: 08-21-2009, 12:09 PM
  5. How come it does not display the inventory
    By sparkyii in forum New To Java
    Replies: 1
    Last Post: 10-29-2008, 03:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •