Struts 2 Hibernate null pointer exception while submitting the form

前端 未结 2 949
太阳男子
太阳男子 2021-01-14 21:34

I am trying to create a registration page by integrating Struts 2 & Hibernate. But when I am running the below code , I am getting a null pointer exception when I click

2条回答
  •  不思量自难忘°
    2021-01-14 22:30

    In the class RegisterAction, you called the static method UserDao.addUser(u). The sf field of the UserDao class is only initialized when you call the constructor of UserDao, but in your case, the constructor is never called => the sf is never initialized thats why calling sf.openSession(); throws a NullPointerException();

    You should initialize sf before calling one of its method.

    Note that it is a very bad practice to initialize a static field in a no static and/or a no synchronized method.

提交回复
热议问题