Hi,everybody. when i write a segment like this: public void insCartItem(String merchNo,int merchNum)// { try { hashCart.put("merchNo"+merchNo,merchNo); hashCart.put("merchNum"+merchNo,new Integer(merchNum)); } catch(Exception ex) { System.out.println(ex.toString()); }
The NullPointerException stack trace should tell you exactly which line is throwing the exception. Study it carefully. Somewhere on that line, something is null. (Generally, it's something to the left of a . symbol, since it's trying to invoke .anything() with a null that causes a NullPointerException.) Hint - have all your variables been initialized?
"I'm not back." - Bill Harding, Twister
simplex du
Greenhorn
Joined: Mar 25, 2002
Posts: 24
posted
0
yes. i have initialized the hashCart,which is a Hashtable,in the constructor method.
simplex du
Greenhorn
Joined: Mar 25, 2002
Posts: 24
posted
0
this is my code,a shopcart bean example; import java.util.*; public class shopCart { private Hashtable hashCart;
public void shopCart() { try { hashCart = new Hashtable(); System.out.println("initializing...."); } catch(Exception ex) { System.out.println(ex.toString()); } }
public void insCartItem(String merchNo,int merchNum)// { System.out.println("yaya"); try { hashCart.put("merchNo"+merchNo,merchNo); hashCart.put("merchNum"+merchNo,new Integer(merchNum)); } catch(Exception ex) { System.out.println(ex.toString()); } } } i use this bean in a jsp file. Hashtable shopBean=new shopCart(); shopBean.insCartItem("12345678",2)
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
posted
0
So, which line is throwing the exception? Look for the first line number in the stack trace which is in a class which you have written (e.g. not in Hashtable). If the exception is actually thrown from another class/method which you have called from your code, consult the API for that method to see if it offers any clues.
simplex du
Greenhorn
Joined: Mar 25, 2002
Posts: 24
posted
0
thanks,jim
Maulin Vasavada
Ranch Hand
Joined: Nov 04, 2001
Posts: 1865
posted
0
hi u've got a LITTLE problem! u have "public void shopCart()" as a constructor declaration. u CAN'T HAVE "void" as a constructor return type. so here it is being considered as a normal method of a shopCart class and when u do new shopCart() that is not invoked as it only tries to find out the constructor w/o argument which is not there! try remove "void" and c if it works... regards maulin