Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes JSP and the fly likes Referencing classes and variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Referencing classes and variables" Watch "Referencing classes and variables" New topic

Referencing classes and variables

shuzo monsoon
Ranch Hand

Joined: Feb 11, 2004
Posts: 54
Hello all, I have written a class called BasketItems, which basically stores the title, artist, price and quantity of an item. Then another class - ShoppingBasket is used to store an instance of the basketItems class in a vector. However, the vector itself is not instantiated until a user adds an item (through a JSP page) to the basket, in which case a process request method should add the product to the vector. I am having some trouble in specifying that if the product is already present, then the quantity should be incremented. Here are the 2 classes -

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;
package catalogue;
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) {
public void removeProduct(String 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)) {

added = true;
if(!added) {
else if(submit.equals("remove")) {
else {
public void reset() {
submit = null;
product = null;
Teh problem I have is that I have used "products.get(i)" for example which is actually a reference to the vector created on the JSP page.
I hope this isn't too confusing and that someone can try and help!
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
What's going wrong here?
My guess is you're getting a NullPointerException because the Vector is not instantiated?
If so, instantiate the Vector instead of iterating over it (as you know it'll be empty...

I agree. Here's the link:
subject: Referencing classes and variables
It's not a secret anymore!