Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Unable to access getter method in an object class I made

    Hello,

    I cannot seem to be able to use a .getKey in my code. I am trying to access it as below:

    Java Code:
    public static void MakeChoiceAgain(int choice, CreateObject[] array)
    {
        String key=array.getKey();
        ...
    }
    And my CreateObject class looks like this:

    Java Code:
    import java.util.StringTokenizer;
    
    public class CreateObject
    {
    	/*****************
    	 * member fields *
    	 *****************/
    	
    	private String key, brand, model, inKey, inBrand, inModel, inWeight, inPrice;
    	private int weight;
    	private double price;
    	private String[] tokens;
    	
    	private String csvRow;
    	//csvRow=ConsoleInput.readString("Enter something:");
    	
    	/*************************
    	 * Alternate Constructor *
    	 *************************/
    	
    	//public CreateObject(String inKey, String inBrand, String inModel, String inWeight, String inPrice)
    	public CreateObject(String inCsvRow)
    	{
    		
    		tokens=inCsvRow.split(",");
    		inKey=tokens[0];
    		inBrand=tokens[1];
    		inModel=tokens[2];
    		inWeight=tokens[3];
    		inPrice=tokens[4];
    		 
    		key=inKey;
    		brand=inBrand;
    		model=inModel;
    		weight = Integer.parseInt(inWeight);
    		setWeight(inWeight);
    		price = Double.parseDouble(inPrice);
    		setPrice(inPrice);
    	}
    	
    	/********************
    	 * Copy Constructor *
    	 ********************/
    	
    	public CreateObject(CreateObject inCreateObject)
    	{
    		key=inCreateObject.getKey();
    		brand=inCreateObject.getBrand();
    		model=inCreateObject.getModel();
    		weight=inCreateObject.getWeight();
    		price=inCreateObject.getPrice();
    	}
    	
    	/**********
    	 * getKey *
    	 **********/
    	
    	public String getKey()
    	{
    		return key;
    	}
    	
    	/************
    	 * getBrand *
    	 ************/
    	
    	public String getBrand()
    	{
    		return brand;
    	}
    	
    	/************
    	 * getModel *
    	 ************/
    	
    	public String getModel()
    	{
    		return model;
    	}
    	
    	/*************
    	 * getWeight *
    	 *************/
    	
    	public int getWeight()
    	{
    		//System.out.println("getWeight weight:"+weight);
    		return weight;
    	}
    	
    	/*************
    	 * setWeight *
    	 *************/
    	
    	public void setWeight(String inWeight)
    	{
    		if (weight<0) 
    		{
    			throw new IllegalArgumentException("Weight must be positive");
    		}
    		//System.out.println("setWeight weight:"+weight);
    	}
    	
    	/************
    	 * getPrice *
    	 ************/
    	
    	public double getPrice()
    	{
    		//System.out.println("getPrice price:"+price);
    		return price;
    	}
    	
    	/************
    	 * setPrice *
    	 ************/
    	
    	public void setPrice(String inPrice)
    	{
    		if (price<0) 
    		{
    			throw new IllegalArgumentException("Price must be positive");
    		}
    		//System.out.println("setPrice price:"+price);
    	}
    	
    	/*************
    	 * To String *
    	 *************/
    	public String toString()
    	{
    		String fieldString;
    		fieldString=new String("\nKey: "+key+"\nBrand: "+brand+"\nModel: "+model+"\nWeight: "+weight+"kg\nPrice: $"+price);
    		return fieldString;
    	}
    }
    The error I'm getting is:

    cannot find symbol
    symbol : method getKey()
    location: class CreateObject[]
    String key=array.getKey();
    Can anyone please tell me what I'm doing wrong?

    Cheers

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Unable to access getter method in an object class I made

    You have an array of CreateObject so you need to tell the index of CreateObject that you are trying to access.

  3. #3
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Re: Unable to access getter method in an object class I made

    I don't really understand what you mean by that?

  4. #4
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Re: Unable to access getter method in an object class I made

    Do you mean something like this?:

    Java Code:
    public static void MakeChoiceAgain(int choice, CreateObject[] array)
    	{
    		for (int i=0; i<array.length; i++) 
    		{
    		String key=array[i].getKey();
                    ...
                    }
             }
    Because that stopped giving me the previous error..
    Last edited by EnSlavingBlair; 10-01-2011 at 07:19 AM.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: Unable to access getter method in an object class I made

    Quote Originally Posted by EnSlavingBlair View Post
    I don't really understand what you mean by that?
    It's like this: a house has a doorbell but a block of houses doesn't have a doorbell. It's the same with your code: your class object has a getKey() method but an array of your objects doesn't have that method. As a matter of fact, an array doesn't have methods at all.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Unable to access getter method in an object class I made

    Quote Originally Posted by EnSlavingBlair View Post
    Do you mean something like this?:

    Java Code:
    public static void MakeChoiceAgain(int choice, CreateObject[] array)
    	{
    		for (int i=0; i<array.length; i++) 
    		{
    		String key=array[i].getKey();
                    ...
                    }
             }
    Because that stopped giving me the previous error..
    Yes that is what I mean. Glad you understand it. :)

  7. #7
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Re: Unable to access getter method in an object class I made

    Thanks for the help :)

Similar Threads

  1. Replies: 3
    Last Post: 08-25-2011, 04:46 PM
  2. Replies: 2
    Last Post: 06-16-2011, 09:39 AM
  3. getter and setter method help please!
    By merdzins in forum New To Java
    Replies: 2
    Last Post: 12-06-2010, 05:06 AM
  4. why i cant access a method in a sub class?
    By javanew in forum New To Java
    Replies: 7
    Last Post: 05-03-2010, 06:29 PM
  5. problem in getter method
    By freddieMaize in forum Advanced Java
    Replies: 5
    Last Post: 05-26-2008, 07:02 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •