Results 1 to 5 of 5
  1. #1
    jean28 is offline Member
    Join Date
    Aug 2012
    Posts
    16
    Rep Power
    0

    Default Why does this print null?

    Hey guys. I'm making/testing a simple class that inputs a variable and a name and then I can use methods I make to print both of these values. I have made the class (Correctly, according to a solutions manual), yes when I try to print the name I get null. Can anyone tell me what i'm doing wrong?

    Java Code:
    public class Product 
    {
    	private String name;
    	private double value;
    	
    	public Product(String named, double cost)
    	{
    		named = name;
    		cost = value;
    	}
    	
    	public String getName()
    	{
    		return name;
    	}
    	
    	public double getPrice()
    	{
    		return value;
    	}
    	
    	public void reducePrice(int reduce)
    	{
    		value = value - reduce;
    	}
    	
    	
    
    }

    Java Code:
    public class ProductPrinter 
    {
    	public static void main(String[] args)
    	{
    		Product jean1 = new Product("Toaster", 10);
    		//Product jean2 = new Product("item2", 15);
    		
    		System.out.println(jean1.getName());
    		
    	}
    
    }
    The print gives me null.

  2. #2
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default Re: Why does this print null?

    should be

    value=cost;
    name = named;

    what ever is on the left is the one that is getting the value a signed.

  3. #3
    jean28 is offline Member
    Join Date
    Aug 2012
    Posts
    16
    Rep Power
    0

    Default Re: Why does this print null?

    Thank you very much.

  4. #4
    heman is offline Member
    Join Date
    Aug 2012
    Posts
    39
    Rep Power
    0

    Default Re: Why does this print null?

    dear jean28,
    you have defined the constructor wrongly.
    It should be,
    name=named;
    value=cost;

    becoz after the constructor is started it will get values in variable "named" and "cost"
    regards,
    heman

  5. #5
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default Re: Why does this print null?

    Quote Originally Posted by jean28 View Post
    Thank you very much.
    Your welcome.

Similar Threads

  1. Javax Print Attribute for Selection Print Range
    By rsawatzky in forum AWT / Swing
    Replies: 0
    Last Post: 04-26-2012, 12:14 AM
  2. Replies: 6
    Last Post: 10-18-2010, 10:40 AM
  3. cant print 2d array(giving null values)
    By fneeks in forum New To Java
    Replies: 1
    Last Post: 04-07-2010, 08:48 PM
  4. Replies: 2
    Last Post: 03-19-2010, 05:30 PM
  5. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 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
  •