pakcikkantin
pakcikkantin

Reputation: 101

Resultset of Spring 3 MVC and JDBC returns null

Hi im new to this Spring , MVC and JdBC support. I want to be able to connect to a mysql database..but when i run my web it returns null. Below codes i believe should be easy,What am i missing here ? Thanks for all replies

Below is my error when try to query the URL

java.lang.NullPointerException
com.simple.myacc.dao.JdbcContactDao.findAll(JdbcContactDao.java:55)
com.simple.myacc.ContactController.getAll(ContactController.java:44)

My spring.xml

.....

<context:component-scan base-package="com.simple.myacc" />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/jsp/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/webcontact" />
    <property name="username" value="root" />
    <property name="password" value="password" />
</bean>

<bean id="jdbcContactDao" class="com.simple.myacc.dao.JdbcContactDao">
    <property name="dataSource" ref="dataSource" />
</bean>

My JdbcContactDao

public class JdbcContactDao {
protected static Logger logger = Logger.getLogger("service");
private DataSource dataSource;
private JdbcTemplate jdbcTemplate;

public JdbcContactDao() {

}

public List<Contact> findAll() {

    String sql = "select * from contact";
    List<Contact> contacts = new ArrayList<Contact>();
    List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql);
    for (Map rs : rows) {
        Contact contact = new Contact();
        contact.setId((Integer) rs.get("id"));
        contact.setFirstname((String) rs.get("firstname"));
        contact.setLastname((String) rs.get("lastname"));
        contact.setEmail((String) rs.get("email"));
        contact.setPhone((String) rs.get("phone"));
        contacts.add(contact);
    }
    return contacts;
}

@Resource(name = "dataSource")
public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;

} }

My controller , some part of it

@RequestMapping(value="/contact/list2",method = RequestMethod.GET)
public String getAll(ModelMap model) {
    dao=new JdbcContactDao();
    List<Contact> contacts = dao.findAll();

     // Attach persons to the Model
     model.addAttribute("contacts", contacts);


     return "contact.list";

}

This is the line that says the NULL

        List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql);

Upvotes: 0

Views: 3008

Answers (4)

Benjamin Thomas
Benjamin Thomas

Reputation: 1

Had a similar problem was connecting to old tables using java/jdbc String sql = "select user_name from table"

jdbc.queryForList(sql); 

queryReturnList = jdbc.queryForList(sql);  

    for (Map mp : queryReturnList){          
        String userName = (String)mp.get("user_name");          
}

userName was always null. looking through the map of returned values found that the map was not using user_name but a label set up on the table of "User Name" Had to have DBA's fix. Hope this helps

Upvotes: 0

rprab
rprab

Reputation: 38

You have your dataSource and your JdbcContactDAO bean configured in the config file. So in the same way you need to inject the jdbcContactDAO bean into your Controller.

<bean id="myController" class="mypath.MyController">
<property name="dao" ref="jdbcContactDao"/>
</bean>

And in your controller....

public JdbcContactDao dao;

@Resource(name="dao")
public void setDao(JdbcContactDao dao){
    this.dao = dao;
}


@RequestMapping(value="/contact/list2",method = RequestMethod.GET)
public String getAll(ModelMap model) {

    List<Contact> contacts = dao.findAll();

    // Attach persons to the Model
    model.addAttribute("contacts", contacts);


    return "contact.list";

}

Upvotes: 0

shazinltc
shazinltc

Reputation: 3666

A common idiom when using the JdbcTemplate class is to configure a DataSource in your Spring configuration file, and then dependency inject that shared DataSource bean into your DAO classes; the JdbcTemplate is created in the setter for the DataSource. private JdbcTemplate jdbcTemplate;

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

You can read more on this here

Your code will look like this

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/webcontact" />
<property name="username" value="root" />
<property name="password" value="password" />

You don't need this

<bean id="jdbcContactDao" class="com.simple.myacc.dao.JdbcContactDao">
<property name="dataSource" ref="dataSource" />

Instead do this

 @Autowired
public void setDataSource(DataSource dataSource) {
    this.jdbcTemplate = new JdbcTemplate(dataSource);
}

and annotate your JdbcContactDao class with @Repository I think that should work

Upvotes: 1

digitaljoel
digitaljoel

Reputation: 26574

I'm guessing line 55 of JdbcContactDao is this one List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql); You declare jdbcTemplate, but never give it a value and it's also not annotated for injection, so it's always going to be null. So, when you attempt to use it, you will get the NPE.

Upvotes: 0

Related Questions