1

I was trying to get the values from DB. When i run the DAO methods i get the java.lang.NullpointerException. Can someone help me here what is wrong in below code. Reference articles are welcome.

Here is my DAO class.

package com.shiva.DAO;

import java.util.ArrayList;
import java.util.List;

import com.shiva.entity.Accountdetails;

public interface AccountDAO {
    public Accountdetails getActdetails();

    public List<String> getonlyto();

}

Below class is the Model class. Name,address,accountid are defined with getters and setters.

package com.shiva.entity;

public class Accountdetails {
    private String address;
    private int accountid;
    private String first_name;
    //Getters and Setters
}

Below class is AccountDAOImpl class annotated with @Repository.

package com.shiva.DAO;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.shiva.entity.AccountDetailMapper;
import com.shiva.entity.Accountdetails;

@Repository("accountDAO")
public class AccountDAOImpl implements AccountDAO {
    private static final Logger logger = LoggerFactory.getLogger(AccountDAOImpl.class.getName());

    @Autowired
    private JdbcTemplate jdbctemplate;

    public AccountDAOImpl() {

    }

    public AccountDAOImpl(JdbcTemplate jdbctemplate) throws SQLException {
        this.jdbctemplate = jdbctemplate;
    }

    @Override
    public List<String> getonlyto() {
        // TODO Auto-generated method stub

        String sql = ("select first_name from public.accountlist");
        getolto = this.jdbctemplate.queryForList(sql, String.class);

        return getolto;

    }

}

Testing the getonlyto method in main class.

package com.shiva.learn;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.shiva.DAO.AccountDAO;
import com.shiva.DAO.AccountDAOImpl;
import com.shiva.entity.Accountdetails;

public class Testrun {

    // private static Test one=new Test();
    private static final Logger logger = LoggerFactory.getLogger(Testrun.class);

    public static void main(String args[])

    {
        AccountDAOImpl accountimpl = new AccountDAOImpl();

        for (String daolist : accountimpl.getonlyto()) {
            System.out.println(daolist);

        }

    }

}

Error details :

Exception in thread "main" java.lang.NullPointerException
    at com.shiva.DAO.AccountDAOImpl.getonlyto(AccountDAOImpl.java:68)
    at com.shiva.learn.Testrun.main(Testrun.java:23)
  • It appears that perhaps your JDBCTemplate is not being properly injected and is null. Where are you creating the JdbcTemplate? – Ben M Aug 15 '18 at 15:39
  • You don't provide a jdbcTemplate when instantiating your implementation – Lino Aug 15 '18 at 15:40
  • In AccountDAOImpl i have created the JdbcTemplate. What should i change here to make it work. – Shiva Kumar Aug 15 '18 at 15:48
  • 1
    Before *doing* anything, you need to understand the things you're using: SPring beans, dependency injection. Read the duplicate I linked to, read the Spring documentation (at least the overview of it): https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html – JB Nizet Aug 15 '18 at 16:06
  • And then look at Spring Data, which makes most of the code you're writing unnecessary. – chrylis -cautiouslyoptimistic- Aug 15 '18 at 16:27
  • Sure i will gothrough the spring documentation. The thing is same is working in Controller class when i log the database values instead of main method. Thanks for your help – Shiva Kumar Aug 15 '18 at 17:27

0 Answers0