reset password
Author Message
nextmatrixman
Posts: 57
Posted 14:08 Oct 19, 2014 |

Hi Dr. Sun, I'm experiencing a "NullPointerException" when executing the "GuestBook" project and opening the link to "AddCommentWithCookie" while following along with the lecture video "Cookies and session tracking". To make sure I didn't make any mistakes, I've then tried using the original code from "http://csns.calstatela.edu/file/view?id=4607268" and yield the same error:

SEVERE: Servlet.service() for servlet [cs320.servlet.AddCommentWithCookie] in context with path [/cs320a] threw exception
java.lang.NullPointerException
    at cs320.servlet.AddCommentWithCookie.getName(AddCommentWithCookie.java:31)

The code is: for( Cookie cookie : cookies )

I think it might be that "cookies" is simply "null" the first time we execute the program, so iterating through it will cause this error. I added a "try/catch" around this piece of code and it worked as expected. Somehow the program in the video worked without any errors.

Please advise.

cysun
Posts: 2935
Posted 14:18 Oct 19, 2014 |

You are right. request.getCookies() returns null if there's no cookie in the request. I've updated the code to check for this.

When I showed the code in the video, the request probably already had a cookie from somewhere else.

Thanks for letting me know.