开发者

The method clear() is undefined for the type ShoppingCart

开发者 https://www.devze.com 2023-03-05 05:55 出处:网络
I am implementing a shopping website through JSP. I have a Java object called ShoppingCart and one called Item. In ShoppingCart there is a vector which holds Item objects. The idea is when I make a ca

I am implementing a shopping website through JSP. I have a Java object called ShoppingCart and one called Item. In ShoppingCart there is a vector which holds Item objects. The idea is when I make a call to the addItem() method, I use:-

cart.addItem(name, image, price, details);

ensuring that the ShoppingCart has already been declared:-

ShoppingCart cart = new ShoppingCart("file_path_to_file"); 

and the the content of addItem is:-

public void addItem(String name, String image, String price, String detail) throws IOException  
{  
     items.add(new Item(name, image, price, detail));  
     this.saveMe();  
}  

where items is the vector. This works absolutely fine. I have, however, now created a new method called clear:-

public void clear() throws IOException  
{  
    items.clear();  
    this.saveMe();  
}  

The saveMe method simply saves to an Object file:-

private void saveMe() throws IOException  
{  
     FileOutputStream fos = new FileOutputStream(this.filename);  
     ObjectOutputStream oos = new ObjectOutputStream(fos)  
     oos.writeObject(this.开发者_如何学编程items);  
     oos.close();  
}  

When I call the clear method using:-

cart.clear();

I get the error message:-

An error occurred at line: 108 in the jsp file: /project/cart.jsp The method clear() is undefined for the type ShoppingCart

Can anybody help with any ideas I can try to resolve this issue?


If that method was newly added and you get this error, you're apparently still using an old version of the class lacking the method in the classpath. You need to save the source code file, recompile/rebuild the class/project, redeploy the project and restart the server to get the new changes to work.

A bit decent IDE with a bit decent server plugin will do that automagically by the way.


Try (re)compiling the code, deploying it to the server and then opening your .jsp page. And do it every time you make changes in java code, not html code.

Java is not interpreted language, but a compiled programming language, whose programs are converted into an executable form before being executed.


I was just fighting a very similar problem in Eclipse. I deselected "Project->Build Automatically" option and I was clicking "Project->Clean" and cleaning the project, but the error wouldn't go away. Finally, in the dialog that pops up after clicking "Project->Clean", I deselected the "Start a build immediately" option and did a clean.

It then FINALLY "really" cleaned the project and cleared the error. Then I started a build and all was fine.

At one point I had commented out the offending line and saved, and Eclipse was still reporting the error on that line!!! I don't know if this is an Eclipse bug or an underlying tool chain bug, but it's definitely not doing the write thing.

This was with Eclipse Helios Service Release 2 and a Java SE 6 target. Running under Windows XP.

0

精彩评论

暂无评论...
验证码 换一张
取 消