Vishal Maral
Vishal Maral

Reputation: 1318

NullPointerException in JdbcTemplate.update()

I am new to spring and trying spring jdbc with PostgeSQL and getting NullPointerException in DAO class at JdbcTemplate.update(). content is as follows:

pom.xml

<!-- Spring JDBC Support -->
     <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.0.2.RELEASE</version>
    </dependency>
   <!-- postgreSQL Driver -->          
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4-1200-jdbc41</version>
    </dependency>

servlet-context.xml

<!-- declare datasource bean -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.postgresql.Driver" />
    <property name="url" value="jdbc:postgresql://localhost:5432/notesHero" />
    <property name="username" value="postgres" />
    <property name="password" value="postgres" />
</bean>

DAOImpl class

  @Component
 public class AdvertiseDAOImpl implements AdvertiseDAO 
  {

private JdbcTemplate template;
@Autowired
private DataSource dataSource;

    public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
    template = new JdbcTemplate(dataSource);
  }

    /*public AdvertiseDAOImpl(DataSource dataSource) {
        template = new JdbcTemplate(dataSource);
    }*/

@Override
public void save(Advertise ad) {
    // TODO Auto-generated method stub
    String insertQry = "INSERT INTO ad_master (name, email_id) values (?,?)";
    System.out.println(ad.getName() + ad.getEmail());
    template.update(insertQry, ad.getName(), ad.getEmail());
}

@Override
public void delete(long postId) {
    // TODO Auto-generated method stub

}

@Override
public Advertise get(long postId) {
    // TODO Auto-generated method stub
    return null;
}

}

Any help in this regard will be highly appreciated..

Upvotes: 0

Views: 1426

Answers (2)

bpgergo
bpgergo

Reputation: 16037

The problem may be Spring not calling the method

public void setDataSource(DataSource dataSource)

when wiring

@Autowired
private DataSource dataSource; 

You can initialize your JdbcTemplate after Spring finished autowiring the DataSource by adding this method to the class AdvertiseDAOImpl

@PostConstruct
public void initialize() {
    template = new JdbcTemplate(dataSource);
}

Alternatively you can define a JdbcTemplate bean and have it autowired by adding the @Autowired annotation to the private JdbcTemplate template; field.

Upvotes: 0

SMA
SMA

Reputation: 37033

Issue is here:

@Autowired
private DataSource dataSource;

public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
    template = new JdbcTemplate(dataSource);
}

You are autowiring on field and hence your setter method never gets called and hence your template variable remains uninitialised (or default null).

So instead of autowiring field autowire setter method.

Upvotes: 1

Related Questions