Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException  RSS feed

 
Rob Deer
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does this give me NullPointerException?
<%
Object o = request.getAttribute("error");
String error = o.toString();
%>
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66186
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there's no request attribute named "error", o will be null.
 
Rob Deer
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain a little further? I have, request.setAttribute("error","userinvalid");
in a servlet which forwards to a jsp below.

<%
Object o = request.getAttribute("error");
String error = o.toString();
%>

What's wrong with it?
 
Bosun Bello
Ranch Hand
Posts: 1512
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you did is okay. But, if NPE is thrown, the object o must be null.
[ October 22, 2005: Message edited by: Bosun Bello ]
 
Rob Deer
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, i see. That jsp is part of a html form file. So on first request, its null. Is there a way to keep it from being null on first loading of the form?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Rob Deer
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!