This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Hashtable's  java. lang. NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Hashtable Watch "Hashtable New topic
Author

Hashtable's java. lang. NullPointerException

simplex du
Greenhorn

Joined: Mar 25, 2002
Posts: 24
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());
}

}
when i use this method,i got a java.lang.NullPointerException.
so,how can i avoid this exception.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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
yes.
i have initialized the hashCart,which is a Hashtable,in the constructor method.
simplex du
Greenhorn

Joined: Mar 25, 2002
Posts: 24
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: 18671
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
thanks,jim
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
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
 
Consider Paul's rocket mass heater.
 
subject: Hashtable's java. lang. NullPointerException
 
Similar Threads
Prob with String arrays
How to insert autoincrement value?
java.net.UnknownHostException
Threads doubt
OutputStream already obtained