Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3976808
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:50:56+00:00 2026-05-20T04:50:56+00:00

I’m having NullPointerException trying to getCurrentSession() java.lang.NullPointerException servlets.ControlServlet.doPost(ControlServlet.java:46) javax.servlet.http.HttpServlet.service(HttpServlet.java:709) javax.servlet.http.HttpServlet.service(HttpServlet.java:802) I use Tomcat 5.5

  • 0

I’m having NullPointerException trying to getCurrentSession()

 java.lang.NullPointerException
 servlets.ControlServlet.doPost(ControlServlet.java:46)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

I use Tomcat 5.5
index.jsp page:

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title></title>
</head>
<body>
<%@ page import="java.util.List" %>
<%@ page import="data.Singer" %>
<jsp:useBean id="singer" class="data.Singer" scope="session"/> 
<jsp:setProperty name="singer" property="*" />

<form action="ControlServlet" method="POST">
    <form method=“POST”>
    Name:<br /> 
    <input type=“text” name="name" /><br />
    Type:<br />
    <input type=“text” name="type" /><br />
    <input type="submit" name="Add song" value="Add song">
    <input type="submit" name="save" value="Save" /><br><br>
<input type ="submit" name="values" value="Get values" >
</form>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>webproject</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/beans.xml, /WEB-INF/conf.xml, /WEB-INF/singers.hbm.xml, /WEB-INF/songs.hbm.xml, /WEB-INF/singerbeans.xml, /WEB-INF/songbeans.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>context</servlet-name>
        <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet>
        <servlet-name>test</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>test</servlet-name>
        <url-pattern>*.*</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/beans.xml, /WEB-INF/conf.xml, /WEB-INF/singers.hbm.xml, /WEB-INF/songs.hbm.xml, /WEB-INF/singerbeans.xml, /WEB-INF/songbeans.xml</param-value>
        </init-param>
        <init-param>
            <param-name>debug</param-name>
            <param-value>2</param-value>
        </init-param>
        <init-param>
            <param-name>detail</param-name>
            <param-value>2</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet>
        <description>
    </description>
        <display-name>ControlServlet</display-name>
        <servlet-name>ControlServlet</servlet-name>
        <servlet-class>servlets.ControlServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>ControlServlet</servlet-name>
        <url-pattern>/ControlServlet</url-pattern>
    </servlet-mapping>


</web-app>

ControlServlet.java

public class ControlServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    @Autowired
    private SingerDao singerdao; 
    public SingerDao getSingerDao() {
        return singerdao;
    }

    public void setSingerDao(SingerDao singerdao) {
        this.singerdao = singerdao;
    }

    public ControlServlet() {
        super();
        // TODO Auto-generated constructor stub
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        if (request.getParameter("values") != null) {
            response.getWriter().println(singerdao.getDBValues());
        }
    }

}

and SingerDao.java

public class SingerDao implements SingerDaoInterface {
    @Autowired
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public List getDBValues() {
        Session session = getCurrentSession();
        List<Singer> singers = session.createCriteria(Singer.class).list();
        return singers;
    }

    private org.hibernate.classic.Session getCurrentSession() {
        return sessionFactory.getCurrentSession();
    }

    public void updateSinger(Singer singer) {
        Session session = getCurrentSession();
        session.update(singer);
    }

    public Singer getSinger(int id) {
        Singer singer = null;
        Session session = getCurrentSession();
        singer = (Singer) session.load(Singer.class, id);
        return singer;

    }

    public void deleteSinger(Singer singer) {
        Session session = getCurrentSession();
        session.delete(singer);
    }

    public void insertRow(Singer singer) {
        Session session = getCurrentSession();
        session.save(singer);
    }



}

In simple Java Project it works fine.I think sessionFactory doesn’t autowires,but why?
Thanks all.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T04:50:57+00:00Added an answer on May 20, 2026 at 4:50 am

    You can’t use @Autowired in plain servlets since they are not managed by Spring.

    However you can use the following trick to initiate autowiring of servlet manualy:

    public class ControlServlet extends HttpServlet {
        @Autowired
        private SingerDao singerdao; 
    
        public void init(ServletConfig cfg) {
            super.init(cfg);
            WebApplicationContextUtils
                .getWebApplicationContext(cfg.getServletContext())
                .getAutowireCapableBeanFactory()
                .autowireBean(this);
        } 
        ...
    }
    

    Also, as suggested in comments, since you already have Spring MVC’s DispatcherServlet in your setup perhaps it would be better to implement functionality of ControlServlet as a Spring MVC controller rather than servlet (unless you have strong reasons to do it as a servlet).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.