2

I try to save the object into sessionmap but it is giving me an exception

public class testclass {

public static void main(String[] args) throws IOException
{
   UserInfo ui = new UserInfo();
   UserLogin ul= new UserLogin();

   ui.setAdds("India");
   ui.setEId("someone@yahoo.com");
   ui.setFName("someone");
   ui.setLName("someone2");
   ui.setStatus("single");
   ul.setPswd("somename");
   ul.setUserId("121");
   ul.setUserinfo(ui);



   AnnotationConfiguration config = new AnnotationConfiguration();
   config.configure("hibernate.cfg.xml");
   //new  SchemaExport(config).create(true, true);
   SessionFactory factory = config.buildSessionFactory();
   Session session = factory.getCurrentSession();
   System.out.println("Session configured");
   session.beginTransaction();
   session.save(ul);
   System.out.println("object saved");
   Query q = session.createQuery("from UserLogin where UserId='121'");
   UserLogin user = (UserLogin)q.uniqueResult();

   if(user!=null)
   {

      ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
      Map<String, Object> sessionMap = externalContext.getSessionMap();
      sessionMap.put("User",user);
      UserLogin hd =  (UserLogin)sessionMap.get("User");
      System.out.println("the user id is "+hd.getUserId());  
    }
        session.getTransaction().commit();

}

Exception:

Exception in thread "main" java.lang.NullPointerException at BeanMngr.testclass.main(testclass.java:58) Java Result: 1 BUILD SUCCESSFUL (total time: 4 seconds)

How is this problem caused and how can I solve it?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
ifti
  • 649
  • 1
  • 11
  • 25
  • It is not clear if you want to save your object into `org.hibernate.Session` or `javax.servlet.http.HttpSession`? – Matt Handy Sep 20 '11 at 08:04
  • thanks for reply in short i want to save an object that can be available throughout the session until invalidate the session in JSF it. – ifti Sep 20 '11 at 08:36

1 Answers1

10

If you want to store an object in jsf session do the following:

FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("yourKey", yourObject);

The SessionMap is a Map<String, Object> and you can access your stored objects with the help of the get(Object key) method.

Matt Handy
  • 29,855
  • 2
  • 89
  • 112
  • thanks for reply Matt but if u take a look at my code i did the same thing.. actual problem is when i try to get FacesContext.getCurrentInstance().getExternalContext(); it is giving a NPE. can u please tell me how to get rid of this NPE. i have declared ma class in faces-config.xml as in session scope. i dont know much about jsf but there is some thing becoz of which it is not able to getcurrentInstance() which extends tread. plz help me out in this.. thanks in advance – ifti Sep 20 '11 at 11:14
  • 1
    Ah, I see (didn't scroll down). But what I see as well is your method `public static void main(...)` which is not necessary / valid for web applications. I recommend to go some steps back and start with one of the basic tutorials (some mentioned [here](http://stackoverflow.com/questions/650655/jsf-tutorials)) – Matt Handy Sep 20 '11 at 11:24
  • MATT this just a test class. which i used to check whether thing are working or not with hibernate queries and all. i have ma half of ma application ready with all the logic and model classes m jus struck here that how to start any session and embed a userid into it.. plz recommend some gud web and book from where i can get good knowledge. – ifti Sep 21 '11 at 04:42
  • If you are on Netbeans, [this tutorial](http://netbeans.org/kb/docs/web/jsf20-intro.html) is a good starting point. For Eclipse take a look at one of [BalusC's great tutorials](http://balusc.blogspot.com/2011/01/jsf-20-tutorial-with-eclipse-and.html). – Matt Handy Sep 22 '11 at 06:17
  • ahm is it possible for the safari browsers to remove the session data ? because i am experiencing data loss if i use safari browsers – F25rT Apr 02 '13 at 11:08