• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Referencing classes and variables

Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
When all four tires fall off your canoe, how many tiny ads does it take to build a doghouse?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic