Results 1 to 9 of 9
Thread: Problem with user defined class
- 04-16-2012, 08:57 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 5
- Rep Power
- 0
Problem with user defined class
Hi there having real trouble with this and don't really know where else to go, a point in the right direction is what i need.
I have an assignment i have to creat my own class that defines a Toy; here are the constructors
So one defines a toy that is not borrowed and the other one that is out and shows the hireDate and the members ID.Java Code:public Toy(String toyID, String desc, int minAge, double dailyRate, char status, String memberID, String hireDate){ c_toyID = toyID; c_desc = desc; c_minAge = minAge; c_dailyRate = dailyRate; c_status = status; c_memberID = memberID; c_hireDate = hireDate;} public Toy(String toyID, String desc, int minAge, double dailyRate, char status){ c_toyID = toyID; c_desc = desc; c_minAge = minAge; c_dailyRate = dailyRate; c_status = status;}
Now i also have a method to prin the values
and when i define the Toy[] i add them like this, contained in my mainJava Code:public void print(){ System.out.println(c_toyID + " " + c_desc + " " + c_minAge + "yo " + c_dailyRate + " " + c_status + " " + c_memberID + " " + c_hireDate); }
now when i print any of the records i get a null field in the output, i understand why but how can i distinguished between the two type of objects, even though they have the same name.Java Code:public static Toy[] toys = new Toy[9]; public static void main(String[] args) { Scanner console = new Scanner(System.in); toys[0] = new Toy("Toy022","Train set",2,0.3,'A'); toys[1] = new Toy("Toy093","Snake Rattle",0,1,'U',"MEM012","23/02/2012"); toys[2] = new Toy("Toy837","Home Science Kit",5,0.5,'A'); toys[3] = new Toy("Toy112","Lego Technic Car",9,0.5,'A'); toys[4] = new Toy("Toy009","Metal Tricycle",3,0.3,'U', "MEM034","16/10/2011"); toys[5] = new Toy("Toy981","Fabric Lion Puzzla",0,0.2,'A');
Also i have a method to hire a toy, now i get a nullexception error when ever i try to add the memberID and hireDate to the Toy object. Can i some how convert the object to the other tried the statement but doesn't work.
thanks for the help and i hope my newbism is to lame.Java Code:toys[i] = new Toy(toys[i].getToyID(), toys[i].getDesc(), toys[i].getMinAge(), toys[i].getDailyRate(), "U", memID, currentDate);
- 04-16-2012, 01:06 PM #2
Re: Problem with user defined class
Can you post the code that does the printing and its output. Where is the null value shown? Are you trying to print for the size of an array rather than for the number of objects in the array?when i print any of the records i get a null field in the output
What other types of of objects are there besides the Toy objects?how can i distinguished between the two type of objects
That would probably be because there is no element in the array at the index's location. You need to keep track of how may Toy objects you put into the array. If you put 2 objects into the array, the slot at index = 2 could be empty. Remember that array indexes start at 0 and go to the array length-1.i get a nullexception errorIf you don't understand my response, don't ignore it, ask a question.
- 04-16-2012, 02:18 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 5
- Rep Power
- 0
Re: Problem with user defined class
Toy022 Train set 2yo 0.3 A null null
Toy009 Metal Tricycle 3yo 0.3 U MEM034 16/10/2011
Toy981 Fabric Lion Puzzla 0yo 0.2 A null null
this is the output of the print() method with toys that have a daily rate of between 0.1 and 0.3
There are only toys either available for hire and return
Below i have posted the hireToy() and borrowToy() method contained in a separate class TestToy also its output
Java Code:public static void hireToy(){ Calendar today = Calendar.getInstance(); String currentDate = today.get(Calendar.DAY_OF_MONTH) + "/" + (today.get (Calendar.MONTH)+1) + "/" + today.get(Calendar.YEAR); System.out.println(currentDate); Scanner console = new Scanner(System.in); System.out.println("To hire a toy, enter a toy ID: "); String toyID = console.nextLine(); System.out.println("Enter the member ID: "); String memID = console.nextLine(); System.out.println("Enter the child's date of birth (dd/mm/yyyy): "); String date = console.nextLine(); for(int i =0;i<9;i++){ if(toys[i].getToyID()== (toyID)){ if(toys[i].borrowToy(memID,date,currentDate) == true){ //toys[i] = new Toy(toys[i].getToyID(), toys[i].getDesc(), toys[i].getMinAge(), toys[i].getDailyRate(), // "U", memID, currentDate); toys[i].setMemId(memID); System.out.println("Your hire was sucesfull"); }else{System.out.println("The child is too young to Hire this film"); } }else{ System.out.println("No record found"); } } }To hire a toy, enter a toy ID:Java Code:public boolean borrowToy(String memberID, String childDOB,String hireDate){ boolean temp; String[] date1 = childDOB.split("/"); String[] date = hireDate.split("/"); Calendar today = Calendar.getInstance(); today.set((Integer.parseInt(date[2])),(Integer.parseInt(date[1])-1), Integer.parseInt(date[0])); Calendar c_childDOB = Calendar.getInstance(); c_childDOB.set((Integer.parseInt(date1[2])),(Integer.parseInt (date1[1])-1),Integer.parseInt(date1[0])); int childDOY = c_childDOB.get(Calendar.DAY_OF_YEAR); int todayDOY = today.get(Calendar.DAY_OF_YEAR); int childY = c_childDOB.get(Calendar.YEAR); int todayY = today.get(Calendar.YEAR); if(childY <= todayY){ if(childDOY <= todayDOY){ return temp = true;}else{ return temp = false; } }else{ return temp = false; } }
TOY022
Enter the member ID:
MEM025
Enter the child's date of birth (dd/mm/yyyy):
25/03/1988
No record found
No record found
No record found
No record found
No record found
No record found
Exception in thread "main" java.lang.NullPointerException
at TestToy.hireToy(TestToy.java:59)
at TestToy.main(TestToy.java:37)
Java Result: 1
- 04-16-2012, 02:21 PM #4
Member
- Join Date
- Apr 2012
- Posts
- 5
- Rep Power
- 0
Re: Problem with user defined class
line 15 in the first code snippet corresponds to 59 where the error is
- 04-16-2012, 02:29 PM #5
Re: Problem with user defined class
What variables have the null values that are shown above?Toy022 Train set 2yo 0.3 A null null
Can you give those variables default values in the constructor when you create an instance of the class?
What variable on line 59 has the null value? When you find the variable, backtrack in the code to see why that variable does not have a valid non null value.Exception in thread "main" java.lang.NullPointerException
at TestToy.hireToy(TestToy.java:59)
Why does this for statement use a hardcoded value: 9?for(int i =0;i<9;i++){
It should use a variable that contains the number of real/non-null elements in the array.If you don't understand my response, don't ignore it, ask a question.
- 04-17-2012, 08:06 AM #6
Member
- Join Date
- Apr 2012
- Posts
- 5
- Rep Power
- 0
Re: Problem with user defined class
here the variable is defined
no i cant give them dummy values within the these values, i have to use two separate constructors one for a toy hired the other for a toy that is available.Java Code:toys[0] = new Toy("Toy022","Train set",2,0.3,'A');
im trying to convert add the memberID and Hire date value to change it to the other kind of toy but when i try and set these values there are null.
this is the type of toy the record is
and i want to change it to this type of object and replace the original in the toys arrayJava Code:public Toy(String toyID, String desc, int minAge, double dailyRate, char status){ c_toyID = toyID; c_desc = desc; c_minAge = minAge; c_dailyRate = dailyRate; c_status = status;}
no reason for the nine, I've changed it to (toys.length-1) so that it goes to the end of the array.Java Code:public Toy(String toyID, String desc, int minAge, double dailyRate, char status, String memberID, String hireDate){ c_toyID = toyID; c_desc = desc; c_minAge = minAge; c_dailyRate = dailyRate; c_status = status; c_memberID = memberID; c_hireDate = hireDate;}
- 04-17-2012, 12:40 PM #7
Re: Problem with user defined class
What are the names of the variables that have null values? Why can't they be given default values in the constructor?when i try and set these values there are null.If you don't understand my response, don't ignore it, ask a question.
- 04-17-2012, 05:48 PM #8
Member
- Join Date
- Apr 2012
- Posts
- 5
- Rep Power
- 0
Re: Problem with user defined class
itas an assignment for uni i cant change this anf the veriables are memberId and hireDate look in the print() method
- 04-17-2012, 05:58 PM #9
Similar Threads
-
Problem about uesr defined class: want explanation
By abhinav435 in forum New To JavaReplies: 3Last Post: 03-23-2012, 07:32 PM -
how to write user defined class as Immutable?
By srinivasmallabathula in forum New To JavaReplies: 3Last Post: 07-04-2011, 11:50 PM -
Problem with user defined class in ArrayList
By anders73 in forum New To JavaReplies: 4Last Post: 04-26-2011, 03:59 PM -
Problem accessing a constant defined in another class
By subith86 in forum New To JavaReplies: 6Last Post: 01-26-2011, 07:49 PM -
Facing problem in compiling user defined package
By UJJAL DHAR in forum New To JavaReplies: 2Last Post: 05-24-2010, 11:40 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks