我创建了一个这样的 JavaBean 类。
package beans;
public class Invoice {
private String companyName;
private double price;
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
然后我创建了一个 Servlet,其中从 HTML 文件中获取参数,创建了一个 Bean。我正在尝试将 bean 添加到 ArrayList。
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String companyName = request.getParameter("txtCompany");
double price = Double.parseDouble(request.getParameter("txtPrice"));
ArrayList<Invoice> list = (ArrayList<Invoice>) new ArrayList();
Invoice r = new Invoice();
r.setCompanyName(companyName);
list.add(r.getCompanyName());
r.setPrice(price);
}
}
但我在.add上收到此错误
The method add(Invoice) in the type ArrayList<Invoice> is not applicable for the arguments (String)
我在哪里可能是错的?