Results 1 to 7 of 7
- 10-01-2011, 04:55 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 14
- Rep Power
- 0
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:
And my CreateObject class looks like this:Java Code:public static void MakeChoiceAgain(int choice, CreateObject[] array) { String key=array.getKey(); ... }
The error I'm getting is: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; } }
Can anyone please tell me what I'm doing wrong?cannot find symbol
symbol : method getKey()
location: class CreateObject[]
String key=array.getKey();
Cheers
- 10-01-2011, 05:01 AM #2
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
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.
- 10-01-2011, 07:04 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 14
- Rep Power
- 0
Re: Unable to access getter method in an object class I made
I don't really understand what you mean by that?
- 10-01-2011, 07:11 AM #4
Member
- Join Date
- Sep 2011
- Posts
- 14
- Rep Power
- 0
Re: Unable to access getter method in an object class I made
Do you mean something like this?:
Because that stopped giving me the previous error..Java Code:public static void MakeChoiceAgain(int choice, CreateObject[] array) { for (int i=0; i<array.length; i++) { String key=array[i].getKey(); ... } }Last edited by EnSlavingBlair; 10-01-2011 at 07:19 AM.
- 10-01-2011, 07:12 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Re: Unable to access getter method in an object class I made
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-01-2011, 10:44 AM #6
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
- 10-01-2011, 10:52 AM #7
Member
- Join Date
- Sep 2011
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
Conceptual question: access to an object created in another class
By TaxpayersMoney in forum New To JavaReplies: 3Last Post: 08-25-2011, 04:46 PM -
How do you access a defined object from another class?!?!
By Baldie in forum AWT / SwingReplies: 2Last Post: 06-16-2011, 09:39 AM -
getter and setter method help please!
By merdzins in forum New To JavaReplies: 2Last Post: 12-06-2010, 05:06 AM -
why i cant access a method in a sub class?
By javanew in forum New To JavaReplies: 7Last Post: 05-03-2010, 06:29 PM -
problem in getter method
By freddieMaize in forum Advanced JavaReplies: 5Last Post: 05-26-2008, 07:02 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks