|
|
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.
|
|

08-01-2008, 08:24 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 24
|
|
|
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:
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
|
|

08-01-2008, 11:34 AM
|
 |
Senior Member
|
|
Join Date: May 2008
Posts: 299
|
|
Hi,
You have not initialized the variable
i feel it should be
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
|
|

08-01-2008, 04:23 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
|
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.
|
|

08-01-2008, 06:25 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 24
|
|
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:
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
|
|

08-01-2008, 06:41 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 24
|
|
|
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!
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|