Results 1 to 5 of 5
  1. #1
    bri1547 is offline Member
    Join Date
    Jul 2008
    Posts
    24
    Rep Power
    0

    Default 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:

    Java 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

  2. #2
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    7

    Default

    Hi,
    You have not initialized the variable
    Product temp;
    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

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,408
    Rep Power
    25

    Default

    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.

  4. #4
    bri1547 is offline Member
    Join Date
    Jul 2008
    Posts
    24
    Rep Power
    0

    Default

    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:

    Java 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

  5. #5
    bri1547 is offline Member
    Join Date
    Jul 2008
    Posts
    24
    Rep Power
    0

    Default

    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!

Similar Threads

  1. having some trouble
    By Unknown1369 in forum New To Java
    Replies: 13
    Last Post: 07-21-2008, 11:52 PM
  2. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM
  3. Bubble Sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 07:42 PM
  4. trouble with program
    By jimJohnson in forum New To Java
    Replies: 1
    Last Post: 04-03-2008, 09:29 AM
  5. need help with bubble sort
    By lowpro in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 05:27 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
  •