the difference is that in the first part you check your variable whether
it's null or not, in the second part you try to access some class variable
of ShoppingCart which is null if your ShoppingCart object is null, so a
NullPointerException is thrown.
-mw
----- Original Message -----
From: "Daniel Liu" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, February 16, 2001 3:44 PM
Subject: Problem: share objects
> I have some trouble in using the methods: getAttribute and
> setAttribute.
>
> A code segment of the method init() in my servlet as below:
> /**************************************************************
> ServletContext sc = getServletContext();
> // Part 1
> String test = (String) sc.getAttribute("test");
>
> if (test == null ) {
> test = new String("test");
> sc.setAttribute("test", test);
> }
> // Part 2
> ShoppingCartDB sdb =
> (ShoppingCartDB) sc.getAttribute("ShoppingCartDB");
> if (db.empty()) {
> sdb = new ShoppingCartDB();
> sc.setAttribute("ShoppingCartDB", sdb);
> }
> //.....
>
> ************************************************************/
>
> the program throw an exception when execute Part 2. I do not see
> any difference between Part 1 and Part 2.
>
> Please help. Thanks.
>
>
>
> ----------------------
> Daniel Liu
> MONTAGE eIntegration(tm) Inc.
> Tel: (613) 820-0080 x500
> [EMAIL PROTECTED]
>
>
___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html