Reputation: 73
I have some problem code I pulled from a book that should not be saying a non-static variable or method needs to be static. The error on the variable comes without me changing anything with that code. The error in the method came after I added code to it but now even when I take the code out it still gives me the error. I have the variable error in Product.java in the getFormattedPrice method in the format currency statement. The method error is in the main method when I call writeProducts.
public class Product
{
private String code;
private String description;
private double price;
public Product(String code, String description, double price)
{
this.code = code;
this.description = description;
this.price = price ;
}
public Product()
{
this("", "", 0);
}
public void setCode(String code)
{
this.code = code;
}
public String getCode(){
return code;
}
public void setDescription(String description)
{
this.description = description;
}
public String getDescription()
{
return description;
}
public void setPrice(double price)
{
this.price = price;
}
public double getPrice()
{
return price;
}
public static String getFormattedPrice()
{
NumberFormat currency = NumberFormat.getCurrencyInstance();
return currency.format(price);
}
public String getFormattedPrice(double price)
{
NumberFormat currency = NumberFormat.getCurrencyInstance();
return currency.format(price);
}
public boolean equals(Object object)
{
if (object instanceof Product)
{
Product product2 = (Product) object;
if
(
code.equals(product2.getCode()) &&
description.equals(product2.getDescription()) &&
price == product2.getPrice()
)
return true;
}
return false;
}
public String toString()
{
return "Code: " + code + "\n" +
"Description: " + description + "\n" +
"Price: " + this.getFormattedPrice() + "\n";
}
}
main method
public class XMLTesterApp
{
private static String productsFilename = "products.xml";
public static void main(String[] args)
{
System.out.println("Products list:");
ArrayList<Product> products = readProducts();
printProducts(products);
for(Product p2 : products){
System.out.println(p2.getPrice());
}
Product p1 = new Product("test", "XML Tester", 77.77);
products.add(p1);
writeProducts(products);
System.out.println("XML Tester has been added to the XML document.\n");
System.out.println("Products list:");
products = readProducts();
printProducts(products);
products.remove(2);
writeProducts(products);
System.out.println("XML Tester has been deleted from the XML document.\n");
System.out.println("Products list:");
products = readProducts();
printProducts(products);
}
private static ArrayList<Product> readProducts()
{
ArrayList<Product> products = new ArrayList<>();
Product p = null;
XMLInputFactory inputFactory = XMLInputFactory.newFactory();
try{
FileReader fileReader = new
FileReader("C:\\Users\\AndrewSpiteri\\Documents\\Classes\\Baker\\CS 242\\java\\netbeans\\ex_starts\\ch19_ex1_XMLTester\\products.xml");
XMLStreamReader reader = inputFactory.createXMLStreamReader(fileReader);
while(reader.hasNext()){
int eventType = reader.getEventType();
switch(eventType){
case XMLStreamConstants.START_ELEMENT:
String elementName = reader.getLocalName();
if(elementName.equals("Product")){
p = new Product();
String code = reader.getAttributeValue(0);
p.setCode(code);
}
if(elementName.equals("Description")){
String description = reader.getElementText();
p.setDescription(description);
}
if(elementName.equals("Price")){
String priceString = reader.getElementText();
double price = Double.parseDouble(priceString);
p.setPrice(price);
}
break;
case XMLStreamConstants.END_ELEMENT:
elementName = reader.getLocalName();
if(elementName.equals("Product"))
products.add(p);
break;
default:
break;
}
reader.next();
}
}
catch(IOException | XMLStreamException e){
System.out.println(e);
}
// add code that reads the XML document from the products.xml file
return products;
}
private void writeProducts(ArrayList<Product> products)
{
XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
try{
FileWriter fileWriter = new
FileWriter("C:\\Users\\AndrewSpiteri\\Documents\\Classes\\Baker\\CS 242\\java\\netbeans\\ex_starts\\ch19_ex1_XMLTester\\products.xml");
XMLStreamWriter writer = outputFactory.createXMLStreamWriter(fileWriter);
writer.writeStartDocument("1.0");
writer.writeStartElement("Products");
for(Product product : products){
writer.writeStartElement("Product");
writer.writeAttribute("Code", product.getCode());
writer.writeStartElement("Description");
writer.writeCharacters(product.getDescription());
writer.writeEndElement();
writer.writeStartElement("Price");
double price = product.getPrice();
writer.writeCharacters(Double.toString(price));
writer.writeEndElement();
writer.writeEndElement();
}
writer.writeEndElement();
writer.flush();
writer.close();
}
catch(IOException | XMLStreamException e){
System.out.println(e);
}
}
private static void printProducts(ArrayList<Product> products)
{
for (Product p : products)
{
printProduct(p);
}
System.out.println();
}
private static void printProduct(Product p)
{
String productString =
StringUtils.padWithSpaces(p.getCode(), 8) +
StringUtils.padWithSpaces(p.getDescription(), 44) +
p.getFormattedPrice();
System.out.println(productString);
}
}
Upvotes: 0
Views: 106
Reputation: 191725
Remove the static
from this method.
public static String getFormattedPrice()
{
NumberFormat currency = NumberFormat.getCurrencyInstance();
return currency.format(price);
}
You get the error because price
in an instance variable, not a static class variable.
And add static
to private void writeProducts
in order to call writeProducts
from another static method.
Upvotes: 2