File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hashtable's java. lang. NullPointerException

 
simplex du
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
simplex du
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes.
i have initialized the hashCart,which is a Hashtable,in the constructor method.
 
simplex du
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks,jim
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic