• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hashtable's java. lang. NullPointerException

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes.
i have initialized the hashCart,which is a Hashtable,in the constructor method.
 
simplex du
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks,jim
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic