Results 1 to 7 of 7
  1. #1
    hayden06f4i is offline Senior Member
    Join Date
    Oct 2010
    Posts
    119
    Rep Power
    0

    Default String output showing "null"

    Hey guys, beginner here....(Again) here is the latest code i have (2 .java files) and my output it showing "null" for the Part # and part Description when it runs, im new and i don't know what im not doing correctly.

    Java Code:
    // Ex3.13 Invoice.java (JHtP page104)
    //invoice a hardware store might use to represent an invoice
    //for an item sold at the store
    
    public class Invoice
    {
    	private String partNumber; // part number for invoice
    	private String partDescription; //part description for invoice
    	private int quantity; //number of parts being purchased
    	private double pricePerItem; // price per item
    	private double invoiceAmount; // total of quantity * priceperitem
    
    	// 4-arg constructor
    	public Invoice (String partNumber, String partDescription,
    		int count, double price, double invoiceAmount)
    	{
    		setpartNumber ( partNumber );
    		setpartDescription ( partDescription );
    		
    		if (count > 0) //determine whether count is positive
    		quantity = count; // valid count assigned to quantity
    		
    		if (price > 0.0) // determine whether price is positive
    		pricePerItem = price; // valid price assign to pricePerItem
    		
    		setquantity ( quantity );
    		setpricePerItem ( price );
    		setinvoiceAmount ( invoiceAmount );
    	} // end 4-arg constructor
    	
    	// set Part number:
    	public void setpartNumber ( String partNumber)
    	{
    		partNumber = partNumber; //accept the passed part value-assgn to  instance v partNumber
    	}// end method setpartNumber
    	
    	// get Part number:
    	public String getpartNumber ()
    	{
    		return partNumber;
    	} //end method getpartNumber
    	
    	// set partDescription:
    	public void setpartDescription ( String partDescription )
    	{
    		partDescription = partDescription; // accept the passed part value-assgn to  instance v partDesc
    	}// end method setpartDescription
    	
    	//get partDescription:
    	public String getpartDescription ()
    	{
    		return partDescription;
    	}// end method getpartDescription
    	
    	// set quantity:
    	public void setquantity ( int count)
    	{
    		quantity = count; 	
    	}// end method set quantity
    	
    	// get quantity:
    	public int getquantity ()
    	{
    		return quantity;
    	} //end method get quantity
    	
    	// set pricePerItem
    	public void setpricePerItem ( double price )
    	{
    		pricePerItem = price;
    	}// end method set pricePerItem
    	
    	//get pricePerItem
    	public double getpricePerItem ()
    	{
    		return pricePerItem;
    	} // end method get pricePerItem
    	
    	//method to setInvoiceAmount
    	public void setinvoiceAmount ( double invoiceAmount )
    	{
    		invoiceAmount = quantity * pricePerItem;
    	}//end method set invoiceAmount
    	
    	
    	// method to getinvoiceAmount (multiplies quantity * price per item) 
    	public double getinvoiceAmount ()
    	{
    		return quantity * pricePerItem;
    	}// end method getinvoiceAmount
    } // end public class Invoice
    program 2
    Java Code:
    // Ex3.13 InvoiceTest.java (JHtP page104)
    //
    
    public class InvoiceTest
    {
    	public static void main ( String args [] )
    	{
    		//create instance of Invoice passing 4 argument values
    		Invoice invoice1 = new Invoice ( "1234" , "Hammer" , 5 , 14.99, 2 * 14.99 );
    		
    		//display invoice1
    		System.out.println ("Original invoice information");
    		
    		System.out.printf ( "Part Number: %s\n", 
    		invoice1.getpartNumber () );
    		
    		System.out.printf ( "Part Description: %s\n",
    		invoice1.getpartDescription () );
    		
    		System.out.printf ( "Quantity: %s\n",
    		invoice1.getquantity () );
    		
    		System.out.printf ( "Price Per Item: %s\n",
    		invoice1.getpricePerItem () );
    		
    		System.out.printf ( "Total Invoice Amount: %s\n",
    		invoice1.getinvoiceAmount () );
    	} //end main method
    }//end public class InvoiceTest
    the output is showing this:


    c:\SimplyJava\Invoice>java InvoiceTest
    Original invoice information
    Part Number: null
    Part Description: null
    Quantity: 5
    Price Per Item: 14.99
    Total Invoice Amount: 74.95

    c:\SimplyJava\Invoice>

  2. #2
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    Well firstly don't make a new thread, you're other question (which is the same) is only 4 theads below and posted less than an hour ago!

    be patient

    Secondly with all the printF statements and methods and params and substitutions you should know about strings. if not you may need to read one of these

    http://tinyurl.com/39xzz7u
    Last edited by maknib; 11-05-2010 at 03:16 AM.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,680
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by hayden06f4i View Post
    Java Code:
    public class Invoice
    {
    	private String partNumber; // part number for invoice
    	...
    	// set Part number:
    	public void setpartNumber ( String partNumber)
    	{
    		partNumber = partNumber; //accept the passed part value-assgn to  instance v partNumber
    	}// end method setpartNumber
    This is wrong (the same mistake occurs in the other setter methods); your parameter shadows your member variable (they have the same name) so all your method does is set the parameter to its own value; change your method(s) to this:

    Java Code:
    	public void setpartNumber ( String partNumber)
    	{
    		this.partNumber = partNumber;
    	}
    kind regards,

    Jos

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,123
    Rep Power
    20

    Default

    Oh great.
    I could have saved myself a post!
    Why ask the question in the other thread and then go and post a new thread??

    Bah!

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,680
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    Oh great.
    I could have saved myself a post!
    Why ask the question in the other thread and then go and post a new thread??

    Bah!
    My fault; I should've noticed that this is the double post (but I didn't have my espression yet so I'm innocent ;-)

    kind regards,

    Jos

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,123
    Rep Power
    20

    Default

    You?
    Innocent?

    Hah!
    ;)

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,680
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    You?
    Innocent?

    Hah!
    ;)
    Yes, me; as a newborn baby. Except for the eggrolls and chocolate icecream incident or the painted balls in the dishwasher, but I didn't do that, honest.

    kind regards,

    Jos ;-)

Similar Threads

  1. string comparison with "=" and ".equal"
    By guavajuice in forum New To Java
    Replies: 9
    Last Post: 04-22-2010, 10:01 PM
  2. Showing/Hiding groups with "triangle flippities"
    By Kendall in forum AWT / Swing
    Replies: 2
    Last Post: 03-30-2010, 06:07 PM
  3. Error showing "Object Required"
    By kishan in forum Advanced Java
    Replies: 2
    Last Post: 09-26-2009, 03:23 PM
  4. Replies: 0
    Last Post: 04-02-2009, 08:02 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 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
  •