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:
Code:
public static void MakeChoiceAgain(int choice, CreateObject[] array)
{
String key=array.getKey();
...
}
And my CreateObject class looks like this:
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:
Quote:
cannot find symbol
symbol : method getKey()
location: class CreateObject[]
String key=array.getKey();
Can anyone please tell me what I'm doing wrong?
Cheers
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.
Re: Unable to access getter method in an object class I made
I don't really understand what you mean by that?
Re: Unable to access getter method in an object class I made
Do you mean something like this?:
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..
Re: Unable to access getter method in an object class I made
Quote:
Originally Posted by
EnSlavingBlair
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
Re: Unable to access getter method in an object class I made
Quote:
Originally Posted by
EnSlavingBlair
Do you mean something like this?:
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. :)
Re: Unable to access getter method in an object class I made