Results 1 to 5 of 5
Thread: Why does this print null?
- 10-24-2012, 08:47 AM #1
Member
- Join Date
- Aug 2012
- Posts
- 16
- Rep Power
- 0
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; } }
The print gives me null.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()); } }
- 10-24-2012, 09:08 AM #2
Member
- Join Date
- Sep 2012
- Posts
- 70
- Rep Power
- 0
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.
- 10-24-2012, 09:13 AM #3
Member
- Join Date
- Aug 2012
- Posts
- 16
- Rep Power
- 0
Re: Why does this print null?
Thank you very much.
- 10-24-2012, 09:16 AM #4
Member
- Join Date
- Aug 2012
- Posts
- 35
- Rep Power
- 0
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
- 10-25-2012, 07:09 AM #5
Member
- Join Date
- Sep 2012
- Posts
- 70
- Rep Power
- 0
Similar Threads
-
Javax Print Attribute for Selection Print Range
By rsawatzky in forum AWT / SwingReplies: 0Last Post: 04-26-2012, 12:14 AM -
Is it better to set array elements to null before setting the arrayreference to null?
By kreyszig in forum Advanced JavaReplies: 6Last Post: 10-18-2010, 10:40 AM -
cant print 2d array(giving null values)
By fneeks in forum New To JavaReplies: 1Last Post: 04-07-2010, 08:48 PM -
JOptionPane.showMessageDialog(null,"Etc Etc"); - What does null actually do?
By markious in forum New To JavaReplies: 2Last Post: 03-19-2010, 05:30 PM -
Print the text file and print preview them
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:04 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks