Results 1 to 4 of 4
  1. #1
    pnoty03 is offline Member
    Join Date
    Jan 2012
    Posts
    1
    Rep Power
    0

    Default Exception in thread "main" java.lang.NullPointerException

    Hello all I need a little help here I am getting this error(Exception in thread "main" java.lang.NullPointerException) on line 12 and not sure why, here is my code
    Java Code:
    import javax.swing.JOptionPane;
    
    public class CoffeeDriver
    {
    	public static String names;
    	public static double prices;
    
    	public static void main (String[] args) 
    		{
    			Item [] names = new Item [5];
    			Item [] prices = new Item [5];
    			names[0].setName("Coffee");
    			names[1].setName("Water");
    			names[2].setName("Milk");
    			names[3].setName("Bagel");
    			names[4].setName("Donut");
    			prices[0].setPrice(1.00);
    			prices[1].setPrice(2.00);
    			prices[2].setPrice(1.50);
    			prices[3].setPrice(1.25);
    			prices[4].setPrice(0.75);
    
    
    
    
    			String Sort = JOptionPane.showInputDialog(null,"Would you like the menu items sorted by name or price? (n/p)", "Welcome to Wings Coffee Shop",1);
    
    			if (Sort == "n")
    				{
    					sortName(names);
    				}	
    else
    
    	sortPrice(prices);
    
    
    
    	}
    public static void sortName( Item [] array)
    {
    
    
    
    
    }
    
    public static void sortPrice (Item [] array)
    {
    int a, b;
    Item temp;
    int highSubscript = array.length -1;
    for(a = 0; a < array.length; ++a)
    	{
    	for(b = 0; b < highSubscript; ++b)
    		{
    		if(array[b].getPrice() > array[b + 1].getPrice())
    			{
    				temp = array[b];
    				array[b] = array[b + 1];
    				array[b + 1] = temp;
    			}
    		}
    	}
    
    }
    
    
    
    
    
    
    
    }

  2. #2
    Join Date
    May 2011
    Location
    Seattle, WA
    Posts
    35
    Rep Power
    0

    Default Re: Exception in thread "main" java.lang.NullPointerException

    I think I may have the solution.

    You seem to initializing everything in your main method, which is something I suggest not doing.
    Instead make an init() method to do all of that.

    Java Code:
    public void init(){
    //Initialization 
    }
    Also you create the object arrays outside of the methods like this:

    Java Code:
    public class example{
    Item[] names;
    Item[] prices;
    //ect...
    Then, inside of your init() initialize the objects:

    Java Code:
    public void init(){
    names = new Item [5];
    prices = new Item [5];
    
    for(int i = 0; i < names.length; i++)
           	 names[i] = new Item();
            
    for(int i = 0; i < prices.length; i++)
              prices[i] = new Item();
    
    names[0].setName("Coffee");
    names[1].setName("Water");
    names[2].setName("Milk");
    //ect...
    }
    This is my first time answering a question, but I'm fairly confident this should help.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,422
    Rep Power
    20

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Declaring the size of an object array doesn't automatically populate the array with objects of the declared Type.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Exactly as Darryl said .... were are u populating names ?. You will have to do something like this.

    Java Code:
    Item [] names = new Item [5];
    Item item = new Item();        
    item.setName("Coffee");
    names[0] = item;
    warm regards
    Vinod M
    _______________________________________________
    give me beans .........

Similar Threads

  1. Replies: 5
    Last Post: 12-21-2011, 07:44 PM
  2. Replies: 5
    Last Post: 10-11-2011, 01:16 AM
  3. Replies: 6
    Last Post: 07-16-2009, 04:30 PM
  4. Replies: 1
    Last Post: 07-16-2009, 11:35 AM
  5. Replies: 1
    Last Post: 07-16-2007, 07:32 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
  •