Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-01-2008, 08:24 AM
Member
 
Join Date: Jul 2008
Posts: 24
bri1547 is on a distinguished road
Trouble w/ Bubble Sort
I am writing an inventory program that uses an array of objects. I need to bubble sort the array based on the product name then display the results. When I added the bubble sort I got a NullPointerException error in run time. I also have to display the total value of all the inventory. I have tried everything I can find in my java book and just can't seem to get it. Can anyone point me in the right direction? Here is my code:

Code:
public class Product { public String itemNumber; // declare variable for item number public String productName; // declare variable for product name public int stockUnits; // declare variable for number of units in stock public double unitPrice; // declare variable for unit price of each product // Four argument constructor public Product( String item, String name, int units, double price ) { itemNumber = item; productName = name; stockUnits = units; unitPrice = price; } // End four argument constructor public void setItemNumber( String itemNumber ) // Set item number { this.itemNumber = itemNumber; } // end set item number public String getItemNumber() // return item number { return itemNumber; } // end return item number public void setProductName( String productName ) // set product name { this.productName = productName; } // end set product name public String getProductName() // return product name { return productName; } // end return product name public void setStockUnits( int stockUnits ) // set units in stock { this.stockUnits = stockUnits; } // end set units in stock public int getStockUnits() // return units in stock { return stockUnits; } // end return units in stock public void setUnitPrice( double unitPrice ) // set unit price { this.unitPrice = unitPrice; } // end set unit price public double getUnitPrice() // return unit price { return unitPrice; } // end return unit price public double getTotalValue() // calculate total value { return stockUnits * unitPrice; } // end method to calculate total value } // end class product public class InventoryPart2 { public static void main( String args[] ) // begin main method { Product Inventory[] = new Product[100]; // array for 100 inventory items Inventory[0] = new Product( "001", "Phone Case", 25, 7.99 ); // product 1 data Inventory[1] = new Product( "002", "Dream Catcher", 75, 9.99 ); // product 2 data Inventory[2] = new Product( "003", "T-Shirt", 100, 19.99 ); // product 3 data int flag = 0; Product temp; while (flag == 0) { flag = 1; for( int x = 0; x < Inventory.length - 1; ++x) { if( Inventory[x].getProductName().compareTo( Inventory[x+1].getProductName() ) > 0 ) { temp = Inventory[x]; Inventory[x] = Inventory[x+1]; Inventory[x+1] = temp; flag = 0; } } } // Display product data System.out.println( "Item# Item In-Stock Price Total Value\n" ); System.out.printf( "%s ", Inventory[0].getItemNumber() ); System.out.printf( "%s ", Inventory[0].getProductName() ); System.out.printf( "%d ", Inventory[0].getStockUnits() ); System.out.printf( "%.2f ", Inventory[0].getUnitPrice() ); System.out.printf( "%.2f\n", Inventory[0].getTotalValue() ); System.out.printf( "%s ", Inventory[1].getItemNumber() ); System.out.printf( "%s ", Inventory[1].getProductName() ); System.out.printf( "%d ", Inventory[1].getStockUnits() ); System.out.printf( "%.2f ", Inventory[1].getUnitPrice() ); System.out.printf( "%.2f\n", Inventory[1].getTotalValue() ); System.out.printf( "%s ", Inventory[2].getItemNumber() ); System.out.printf( "%s ", Inventory[2].getProductName() ); System.out.printf( "%d ", Inventory[2].getStockUnits() ); System.out.printf( "%.2f ", Inventory[2].getUnitPrice() ); System.out.printf( "%.2f\n", Inventory[2].getTotalValue() ); } // end method main } // end class InventoryPart2
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-01-2008, 11:34 AM
Niveditha's Avatar
Senior Member
 
Join Date: May 2008
Posts: 299
Niveditha is on a distinguished road
Send a message via Skype™ to Niveditha
Hi,
You have not initialized the variable
Quote:
Product temp;
i feel it should be
Quote:
Product temp= new Product();
for which you have to add a default constructor in Product class with no arguments..
__________________
To finish sooner, take your own time....
Nivedithaaaa
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-01-2008, 04:23 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
Norm is on a distinguished road
Where do you get the NPE? You MUST post the full text of error messages.
Look at the line number referenced in the error message and think how any of the objects referenced in that statement could be null.

Inventory is defined to hold 100 elements, but your code only shows that 3 have been filled. Your code expects all 100 to be filled. It they are not filled, they will be null.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-01-2008, 06:25 PM
Member
 
Join Date: Jul 2008
Posts: 24
bri1547 is on a distinguished road
Norm, eventually the inventory will be controlled by user input. Will I need to set up a counter of some sort to define the length of the array? Also, when I try to print the inventory by calling the get methods from class product I get a compile time error "cannot find symbol." I took the lines straight from a textbook which does not expound on the lines used to print. Here is the code and error message:

Code:
public class InventoryPart2 { public static void main( String args[] ) // begin main method { Product Inventory[] = new Product[3]; // array for 3 inventory items Inventory[0] = new Product( "001", "Phone Case", 25, 7.99 ); // product 1 data Inventory[1] = new Product( "002", "Dream Catcher", 75, 9.99 ); // product 2 data Inventory[2] = new Product( "003", "T-Shirt", 100, 19.99 ); // product 3 data int flag = 0; Product temp; while (flag == 0) { flag = 1; for( int x = 0; x < Inventory.length - 1; ++x) { if( Inventory[x].getProductName().compareTo( Inventory[x+1].getProductName() ) > 0 ) { temp = Inventory[x]; Inventory[x] = Inventory[x+1]; Inventory[x+1] = temp; flag = 0; } } } // Display product data System.out.println( "Item# Item In-Stock Price Total Value\n" ); for ( int counter = 0; counter < Inventory.length; counter ++ ) System.out.printf( "%s%3s%12d%8.2f%6.2f", Inventory[counter].getItemNumber(), Inventory[counter].getProductName, Inventory[counter].getStockUnits, Inventory[counter].getUnitPrice, Inventory[counter].getTotalValue ); } // end method main } // end class InventoryPart2 public class Product { public String itemNumber; // declare variable for item number public String productName; // declare variable for product name public int stockUnits; // declare variable for number of units in stock public double unitPrice; // declare variable for unit price of each product // Four argument constructor public Product( String item, String name, int units, double price ) { itemNumber = item; productName = name; stockUnits = units; unitPrice = price; } // End four argument constructor public void setItemNumber( String itemNumber ) // Set item number { this.itemNumber = itemNumber; } // end set item number public String getItemNumber() // return item number { return itemNumber; } // end return item number public void setProductName( String productName ) // set product name { this.productName = productName; } // end set product name public String getProductName() // return product name { return productName; } // end return product name public void setStockUnits( int stockUnits ) // set units in stock { this.stockUnits = stockUnits; } // end set units in stock public int getStockUnits() // return units in stock { return stockUnits; } // end return units in stock public void setUnitPrice( double unitPrice ) // set unit price { this.unitPrice = unitPrice; } // end set unit price public double getUnitPrice() // return unit price { return unitPrice; } // end return unit price public double getTotalValue() // calculate total value { return stockUnits * unitPrice; } // end method to calculate total value } // end class product InventoryPart2.java:37: cannot find symbol symbol : variable getProductName location: class Product Inventory[counter].getProductName, Inventory[counte r].getStockUnits, ^ InventoryPart2.java:37: cannot find symbol symbol : variable getStockUnits location: class Product Inventory[counter].getProductName, Inventory[counte r].getStockUnits, ^ InventoryPart2.java:38: cannot find symbol symbol : variable getUnitPrice location: class Product Inventory[counter].getUnitPrice, Inventory[counter] .getTotalValue ); ^ InventoryPart2.java:38: cannot find symbol symbol : variable getTotalValue location: class Product Inventory[counter].getUnitPrice, Inventory[counter] .getTotalValue ); ^ 4 errors
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-01-2008, 06:41 PM
Member
 
Join Date: Jul 2008
Posts: 24
bri1547 is on a distinguished road
Norm, I figure out my problem...I forgot the parentheses! Wow...dumb! Thanks for not being one of those uptight types who thinks everyone should make the same mistakes they did. I really appreciate your help. Especially since you don't give away the answer, you lead me to it and make me find it on my own. Your help is invaluable!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
having some trouble Unknown1369 New To Java 13 07-22-2008 01:52 AM
How to sort a list using Bubble sort algorithm Java Tip Algorithms 3 04-29-2008 10:04 PM
Bubble Sort in Java Java Tip Algorithms 0 04-15-2008 09:42 PM
trouble with program jimJohnson New To Java 1 04-03-2008 11:29 AM
need help with bubble sort lowpro New To Java 3 12-17-2007 07:27 PM


All times are GMT +3. The time now is 01:31 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org