Results 1 to 9 of 9
Thread: Problem with user defined class
- 04-16-2012, 09: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
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
Java Code:public void print(){ System.out.println(c_toyID + " " + c_desc + " " + c_minAge + "yo " + c_dailyRate + " " + c_status + " " + c_memberID + " " + c_hireDate); }
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.
Java Code:toys[i] = new Toy(toys[i].getToyID(), toys[i].getDesc(), toys[i].getMinAge(), toys[i].getDailyRate(), "U", memID, currentDate);
- 04-16-2012, 02:06 PM #2
Re: Problem with user defined class
when i print any of the records i get a null field in the output
how can i distinguished between the two type of objects
i get a nullexception errorIf you don't understand my response, don't ignore it, ask a question.
- 04-16-2012, 03: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"); } } }
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, 03: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, 03:29 PM #5
Re: Problem with user defined class
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?
Exception in thread "main" java.lang.NullPointerException
at TestToy.hireToy(TestToy.java:59)
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, 09:06 AM #6
Member
- Join Date
- Apr 2012
- Posts
- 5
- Rep Power
- 0
Re: Problem with user defined class
here the variable is defined
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
Java 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;}
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, 01:40 PM #7
Re: Problem with user defined class
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, 06: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, 06:58 PM #9
Similar Threads
-
Problem about uesr defined class: want explanation
By abhinav435 in forum New To JavaReplies: 3Last Post: 03-23-2012, 08:32 PM -
how to write user defined class as Immutable?
By srinivasmallabathula in forum New To JavaReplies: 3Last Post: 07-05-2011, 12:50 AM -
Problem with user defined class in ArrayList
By anders73 in forum New To JavaReplies: 4Last Post: 04-26-2011, 04:59 PM -
Problem accessing a constant defined in another class
By subith86 in forum New To JavaReplies: 6Last Post: 01-26-2011, 08:49 PM -
Facing problem in compiling user defined package
By UJJAL DHAR in forum New To JavaReplies: 2Last Post: 05-24-2010, 12:40 PM
Bookmarks