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 9093225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:02:46+00:00 2026-06-16T23:02:46+00:00

So, I have a simple REST web application and if my Controller calls the

  • 0

So, I have a simple REST web application and if my Controller calls the DAO directly, the JDBC calls work just fine, but if my controller calls another class that calls the DAO on my behalf, it fails with NullPointerException (NPE).

Here’s my Controller:

@Component
@Scope("request")
@Path("/emsrequest")
public class EMSRequest {

    // I'm using this for testing
    @GET
    @Path("/xml/{accountNumber}")
    @Produces({MediaType.TEXT_PLAIN})
    public String requestByAccountNumber_XML(
            @PathParam("accountNumber") String accountNumber) {

        ReqSubTest los = new ReqSubTest();

        return "account (LOS) number is : " + los.testSql(Integer.parseInt(accountNumber)) + "!";
    }
}

Here’s the intermediate (service) class:

package com.company.demo.mercury.processmanager.components;

import com.company.demo.pmrws.dao.EMSRequestDaoImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ReqSubTest {

    @Autowired
    EMSRequestDaoImpl dao = new EMSRequestDaoImpl();

    public int testSql(int quantity){
        return dao.getNextTableIds("sys_process_tbl", quantity);

    }
}

And here’s the DAO Implementation:

package com.company.demo.pmrws.dao;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class EMSRequestDaoImpl extends JdbcDaoSupport {

    private static final Logger logger = Logger.getLogger(EMSRequestDaoImpl.class);

    public int getNextTableIds(String tableName, int quantity) {

        if (logger.isDebugEnabled()) {
            logger.trace("Entering getNextTableIds");
        }

        if (getJdbcTemplate() == null) {
            System.out.println("UH OH!");
        }

        String selectSql = "select next_id "
                + "from sys_key_tbl "
                + "where table_name = ? ";
        String updateSql = "update sys_key_tbl "
                + "set next_id = ? "
                + "where table_name = ? and next_id = ? ";
        int lastId = -1;
        int updateCount = 0;
        while (updateCount == 0) {
            lastId = getJdbcTemplate().queryForInt(selectSql,
                    new Object[]{tableName});
            updateCount = getJdbcTemplate().update(updateSql,
                    new Object[]{lastId + quantity, tableName, lastId});
        }

        if (logger.isDebugEnabled()) {
            logger.trace("Leaving getNextTableIds");
        }
        return lastId + 1;
    }
}

The Application Context XML:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    <context:component-scan base-package="com.company.demo.pmrws"/>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close">
        <property name="driverClassName" value="com.sybase.jdbc3.jdbc.SybDataSource" />
        <property name="url"
                  value="jdbc:sybase:Tds:blah:10240/BLAH_DB1" />
        <property name="username" value="blah" />
        <property name="password" value="blah" />
    </bean>

    <bean id="dataSourceMain" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close">
        <property name="driverClassName" value="com.sybase.jdbc3.jdbc.SybDataSource" />
        <property name="url"
                  value="jdbc:sybase:Tds:blah:10240/BLAH_DB2" />
        <property name="username" value="blah" />
        <property name="password" value="blah" />
    </bean>
    </bean>

    <!--    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>-->

    <bean id="emsResponseDao" class="com.company.demo.pmrws.dao.EMSResponseDaoImpl">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="emsStatusDao" class="com.company.demo.pmrws.dao.EMSStatusDaoImpl">
        <property name="dataSource" ref="dataSourceMain" />
    </bean>

<!--    <bean id="collateralSybaseEmsDao" class="com.company.demo.dao.CollateralSybaseEmsDaoImpl">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="collateralSybaseDao" class="com.company.demo.dao.CollateralSybaseDaoImpl">
        <property name="dataSource" ref="dataSource" />
    </bean>-->

    <bean id="emsRequestDao" class="com.company.demo.pmrws.dao.EMSRequestDaoImpl">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <context:component-scan base-package="com.company.demo.mercury.processmanager.components" />

</beans>

What am I missing? BTW, this is Spring 2.5.

  • 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-06-16T23:02:47+00:00Added an answer on June 16, 2026 at 11:02 pm

    It is because ReqSubTest los = new ReqSubTest(); create a new normal java Object instance but not a Spring Bean, therefore there get nothing injected into the los instance.

    Three ways to fix it.

    • instead of ReqSubTest los = new ReqSubTest(); inject los via Spring, but this requires that EMSRequest is a Spring bean too

    Example:

    @Component public class EMSRequest {
    
        @Autowire ReqSubTest los;
    
        public String requestByAccountNumber_XML(@PathParam("accountNumber") String accountNumber) {
            return "account (LOS) number is : " + los.testSql(Integer.parseInt(accountNumber)) + "!";
        } 
    }
    
    • instead of ReqSubTest los = new ReqSubTest(); use ReqSubTest los = springApplicationContext.getBean(ReqSubTest.class)
    • If you use real AspectJ then you can add @Configurable and then Spring inject other beans even if an instance is created via new (Requires to be enabled via <context:spring-configured />) – You can add the annotation to EMSRequest and then use normal injection like suggeted in the first item, or add it to ReqSubTest
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a web application providing a service using a simple REST interface (GET/POST
I have a simple Spring MVC application with a REST-like web service. When I
Let's say I have a one-page Javascript-heavy web application. But for the rest of
I have deployed a simple REST based application in RAD. A simple URL is
I'm trying to create a very simple REST server. I just have a test
I have a ASP.NET Web Forms application that internally makes many SOAP and REST
I have an application which consists of SOAP and REST web services and a
I have a REST web-service interface that calls-down to a service layer which orchestrates
I have a web application with a simple file upload requirement (max 1 mb).
I have a web application that consists of Website and REST API. Should I

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.