So, I have two classes. One class, called Data, and another class, called Part. (It is for an inventory tracking program). Data handles the serialization of info and processing of data for operation of the program. Part essentially just holds a constructor. I know this isn't the most ideal object oriented setup, but later program functions will make this setup highly convenient.
Anyways, the Part class has the following constructor:
Part(String partName, String Make, String PartNumber,
String altPartNumber, BigDecimal price, int quantity,
String description, boolean automotive, boolean marine,
boolean industrial)
{
}
The Data class contains this ArrayList, which will later be used in a JTable:
protected ArrayList<Part> parts;
There are several accessor methods within the Part class, among which include those to access each parameter of the Part objects stored within the ArrayList. Intended functionality is to be able to access (and later set, any tips on that would be helpful too, if you foresee any potential difficulties) a parameter of any given Part object within the parts ArrayList.
The current code I have that attempts to do this:
protected String getPartName(Part part)
{
return part.partName;
}
NetBeans whines that it can't find variable partName within class Part.
How do I fix this? / Why is it doing this?
EDIT: New problem:
I have the following switch statement:
@Override
public Object getValueAt(int row, int column)
{
Part part = getRow(row); // Gets the part in question
switch(column)
{
case 0:
return Part.getPartName(part);
case 1:
return Part.getMake();
case 2:
return Part.getPartNumber();
case 3:
return Part.getAltPartNumber();
case 4:
return Part.getPrice();
case 5:
return Part.getQuantity();
case 6:
return Part.isAutomotive();
case 7:
return Part.isMarine();
case 8:
return Part.isIndustrial();
default:
return null; // This shouldn't ever be called.
}
}
However, NetBeans is now whining that "non-static method getPartName cannot be referenced from a static context. Should I be declaring my accessor/mutator methods static?
Make sure that in your Part class you have the partName variable declared and that it is public and also set it in your constructor. At the end, you Part class should look like this:
public class Part {
public String partName;
public Part(String partName, String Make, String PartNumber,
String altPartNumber, BigDecimal price, int quantity,
String description, boolean automotive, boolean marine,
boolean industrial) {
this.partName = partName;
}
}
This makes the variable partName visible to all other classes who have an instance of Part by making is public. This also sets that variable to the value passed in the constructor.
You should do this with your other variables too if you are planning on accessing them in the same way as partName:
public class Part {
public String partName, Make, PartNumber, altPartNumber, description;
public BigDecimal price;
public int quantity;
public boolean automotive, marine, industrial;
public Part(String partName, String Make, String PartNumber,
String altPartNumber, BigDecimal price, int quantity,
String description, boolean automotive, boolean marine,
boolean industrial) {
this.partName = partName;
this.Make = Make;
this.PartNumber = PartNumber;
this.altPartNumber = altPartNumber;
this.description = description;
this.price = price;
this.quantity = quantity;
this.automotive = automotive;
this.marine = marine;
this.industrial = industrial;
}
}
Now NetBeans should compile it just fine.
Here's the answer to your new edit. (Note: next time, you should ask a new question instead of editing an old one :) ). Anyways, the solution is a simple one. In your code, you have a part variable named part (lowercase "p") and you also have a class named Part (uppercase "P"), but in your switch statement you are using statements like Part.getPartName(..., with an uppercase "P". Because of this, Java is thinking that you want to access a static method of class Part, when actually I think you want to access the instance methods of local variable part.
tl;dr:
Basically, you're mixing cases which shouldn't be done in case-sensitive languages like Java.
Simply change all the Part.whateverMethod(... to part.whateverMethod(... in your switch.
Your code should look like this:
@Override
public Object getValueAt(int row, int column)
{
Part part = getRow(row); // Gets the part in question
switch(column)
{
case 0:
return part.getPartName(part); // lowercase "part"
case 1:
return part.getMake(); // lowercase "part"
case 2:
return part.getPartNumber(); // lowercase "part"
//... do you see what I'm trying to do here?
// continue to do this for all your other "Part.whateverMethod("
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With