Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By KevinWorkman

Thread: Help With Program (ArrayLists)

  1. #1
    Kreek is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default Help With Program (ArrayLists)

    Hello everyone! I am writing a program that will basically take an item's ID number, along with the quantity, and then store that in an ArrayList. Here is the first class:

    Java Code:
    public class BinItem
    {
    	private String mySKU;
    	private int myQuantity;
    	
    	public BinItem(String sku, int quantity)
    	{
    		mySKU = sku;
    		myQuantity = quantity;
    	}
    	
    	public String getSKU() 
    	{ 
    		return mySKU;
    	}
    	
    	public int getQuantity()
    	{
    		return myQuantity;
    	}
    
    	public String toString()
    	{
    		return "SKU " + mySKU + ": " + myQuantity;
    	}
    }
    As you can see, it takes the item's SKU (ID #), along with the quantity, and then has get methods to be accessed from another class. Here is my second class:

    Java Code:
    import java.util.*;
    public class Bin
    {
    	private String myName;
    	private ArrayList<BinItem> myContents;
    	
    	public Bin(String name)
    	{
    		myName = name;
    	}
    	
    	public String getName()
    	{
    		return myName;
    	}
    	
    	public ArrayList<BinItem> getContents()
    	{
    		return myContents;
    	}
    
    	public void add(BinItem b)
    	{
    		myContents.add(b);
    	}
    	
    	public String toString()
    	{
    		String s = "Bin " + myName + ":\n";
    		for ( BinItem b : myContents )
    			s += b + "\n";
    		return s;
    	}
    	
    	public static void main(String [] args)
    	{
    		ArrayList<Bin> warehouse = new ArrayList<Bin>();
    		Bin a = new Bin("A");
    		Bin b = new Bin("B");
    		warehouse.add(a);
    		warehouse.add(b);
    		a.add(new BinItem("1234-0", 500));
    		a.add(new BinItem("1234-1", 25));
    		a.add(new BinItem("1234-2", 7720));
    		b.add(new BinItem("1234-3", 1000));
    		for(Bin bn : warehouse) 
    		System.out.println(bn);
    	}
    }
    This class has a string that holds the name of the Bin object, and an ArrayList that holds an object or instance of the BinItem class. However, whenever I try to add a BinItem to the MyContents ArrayList, such as in the add method, I get a null pointer exception. Anybody know why I'm getting this?

    Thanks!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default Re: Help With Program (ArrayLists)

    You should post the exact text of your full error message.

    But I can take a wild guess: when do you initialize myContents?
    Kreek likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,785
    Rep Power
    5

    Default Re: Help With Program (ArrayLists)

    It doesn't look to me like you have assigned any ArrayList to myContents. So it defaults to null.

    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Kreek is offline Member
    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Default Re: Help With Program (ArrayLists)

    Ok I see! That was it! I hadn't initialized myContents, so it was defaulting to null. I completely overlooked this. Thanks for pointing it out guys!

Similar Threads

  1. Help with arraylists!
    By MrSquishyBoots in forum New To Java
    Replies: 3
    Last Post: 01-24-2013, 09:52 AM
  2. I don't get ArrayLists
    By Gio!? in forum New To Java
    Replies: 10
    Last Post: 05-03-2012, 12:50 PM
  3. Working with ArrayLists, program locks up at runtime
    By JaredXIII in forum New To Java
    Replies: 4
    Last Post: 04-06-2012, 02:56 AM
  4. Converting Program from ArrayLists to Arrays
    By Java-Guy in forum New To Java
    Replies: 3
    Last Post: 01-26-2011, 09:02 AM

Posting Permissions

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