Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PLEASE HELP --- Java Newbie!!!

 
b rag
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GETTING "NullPointer" for this. Please advice.

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class test {
public static void main(String[] args) {
try
{
HttpServletRequest request=null;
request.setAttribute("fdfs","fdfsdf");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HttpServletRequest request=null;
request.setAttribute("fdfs","fdfsdf");


You set a variable to null, and then try to call a method on it. Of course, you are going to get a null pointer exceptions.

I think you need to familiarize yourself with basic Java before trying to dive off into Servlet programming.
 
b rag
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So how do I set a Reuqest Object and set Attributes to it? Thanks,
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't from a main method.
Servlets need to be run inside a servlet container, like Tomcat, Websphere, etc..

I would follow the advice given and go through a few Java tutorials before tackling servlets. Then, before posting in a forum, follow some servlet tutorials to get the basics down.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"b rag"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic