aspose file tools*
The moose likes Java in General and the fly likes Instantiation Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Instantiation Exception" Watch "Instantiation Exception" New topic
Author

Instantiation Exception

shuzo monsoon
Ranch Hand

Joined: Feb 11, 2004
Posts: 54
Hello everyone, now I'm having a little trouble using two classes I have create. one to handle the attributes of an item (e.g name, price etc) and another to place these items into a vector when they are selected by a user from a JSP page. The code compiles fine, but when I tru to run it i get a java.lang.InstantiationException on the first class (which is called BasketItems). Perhaps I am not calling it properly or does anyone know what else it could be?
Thanks.
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
From the api docco on InstantiationException:

Thrown when an application tries to create an instance of a class using the newInstance method in class Class, but the specified class object cannot be instantiated because it is an interface or is an abstract class


The nice thing about Standards is that there are so many to choose from!
shuzo monsoon
Ranch Hand

Joined: Feb 11, 2004
Posts: 54
Well thanks for that definition Eddie, the class I am trying to create an instance of is shown below, is this what you'd call an interface or abstract class? I'm not too sure myself!
package catalogue;
public class BasketItem {
String product = null;
String artist = null;
float price = 0;
int quantity = 0;

public BasketItem(String product_, String artist_, float price_) {
product = product_;
artist = artist_;
price = price_;
quantity = 1;
}
public String getTitle() {
return product;
}
public String getArtist() {
return artist;
}
public float getPrice() {
return price;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity_) {
quantity = quantity_;
}
public void addToItemQuantity(int q) {
quantity += q;
}
}
Thanks.
Aneesha Singh
Ranch Hand

Joined: Jan 14, 2002
Posts: 47
Hi there,
Can you also post the code of the calling class. The class you have posted looks fine to me!! Donno if i am missing something

Cheers!
shuzo monsoon
Ranch Hand

Joined: Feb 11, 2004
Posts: 54
This is the code for the ShoppingBasket, which uses the basket items, the process request method is actually called in a JSP page.
import java.util.*;
import javax.servlet.http.HttpServletRequest;
public class ShoppingBasket extends Object {
private Vector basket = null;
String product = null;
String submit = null;

public ShoppingBasket() {
basket = new Vector();
}
public void setProduct(String product) {
this.product = product;
}
public void setSubmit(String submit) {
this.submit = submit;
}
public Vector getProducts() {
return basket;
}
public void addProduct(String product) {
basket.add(product);
}
public void removeProduct(String product) {
basket.remove(product);
}
public void processRequest(HttpServletRequest req) {
if(submit!=null) {
if(submit.equals("Add")) {

BasketItem item = null;
boolean added = false;
for(int i=0;i<products.size();i++) {
item = (BasketItem)products.get(i);
if(item.getTitle().equals(product)) {
((BasketItem)products.get(i)).addToItemQuantity(1);

added = true;
break;
}
if(!added) {
addProduct(product);
}
}
}
}
else if(submit.equals("remove")) {
removeProduct(product);
}
else {
reset();
}
}
public void reset() {
submit = null;
product = null;
}
}
Thanks aneesha.
shuzo monsoon
Ranch Hand

Joined: Feb 11, 2004
Posts: 54
Well I got past that exception, basically it was the way I was calling the class. But there is another error I could used help on. The code I have pasted above, the ShoppingBasket class, has errors on the variable "products" and I think this is because what I am trying to use is actually a vector which is created in a JSP page and I'm not sure how, or even if, I can reference it. This class is basically supposed to put the items created from the BasketItems class, i.e. an instance of that class (also shown above) into a vector in the ShoppingBasket class. I'm not sure how to do this really, so any help and explanations would be very much appreciated!
Aneesha Singh
Ranch Hand

Joined: Jan 14, 2002
Posts: 47
Hi shuzo,
You could try using the getAttribute method of the request object to get the vector and also the value of submit. Assign the vector u get by using getAttribute to the variable that you have created and then continue using it. Try using a System.out.println to print out the size of the vector and also the value of submit to see that they are being correctly recd by your class.
Do let me know what comes of it :-D
Cheers!
 
jQuery in Action, 2nd edition
 
subject: Instantiation Exception