Results 1 to 9 of 9
  1. #1
    moosethmucha is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default 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;}
    So one defines a toy that is not borrowed and the other one that is out and shows the hireDate and the members ID.
    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);
            
        }
    and when i define the Toy[] i add them like this, contained in my main
    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');
    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.
    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);
    thanks for the help and i hope my newbism is to lame.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: Problem with user defined class

    when i print any of the records i get a null field in the output
    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?
    how can i distinguished between the two type of objects
    What other types of of objects are there besides the Toy objects?

    i get a nullexception error
    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.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    moosethmucha is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default 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;   
            }
         
            
        }
    To hire a toy, enter a toy ID:
    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

  4. #4
    moosethmucha is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: Problem with user defined class

    line 15 in the first code snippet corresponds to 59 where the error is

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: Problem with user defined class

    Toy022 Train set 2yo 0.3 A null null
    What variables have the null values that are shown above?
    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)
    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.

    for(int i =0;i<9;i++){
    Why does this for statement use a hardcoded value: 9?
    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.

  6. #6
    moosethmucha is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default Re: Problem with user defined class

    here the variable is defined
    Java Code:
    toys[0] = new Toy("Toy022","Train set",2,0.3,'A');
    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.

    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;}
    and i want to change it to this type of object and replace the original in the toys 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;}
    no reason for the nine, I've changed it to (toys.length-1) so that it goes to the end of the array.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: Problem with user defined class

    when i try and set these values there are null.
    What are the names of the variables that have null values? Why can't they be given default values in the constructor?
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    moosethmucha is offline Member
    Join Date
    Apr 2012
    Posts
    5
    Rep Power
    0

    Default 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

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: Problem with user defined class

    Setting default values in the constructor is a normal way to keep variables from having null values.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Problem about uesr defined class: want explanation
    By abhinav435 in forum New To Java
    Replies: 3
    Last Post: 03-23-2012, 07:32 PM
  2. how to write user defined class as Immutable?
    By srinivasmallabathula in forum New To Java
    Replies: 3
    Last Post: 07-04-2011, 11:50 PM
  3. Problem with user defined class in ArrayList
    By anders73 in forum New To Java
    Replies: 4
    Last Post: 04-26-2011, 03:59 PM
  4. Replies: 6
    Last Post: 01-26-2011, 07:49 PM
  5. Facing problem in compiling user defined package
    By UJJAL DHAR in forum New To Java
    Replies: 2
    Last Post: 05-24-2010, 11:40 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
  •