Reputation: 37
So I have tried many changes and what not, but I am not sure what the heck is going wrong with my code. This is my first project in Spring. I am being given a null pointer exception with my sql methods.
My JdbcDAO.java
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.datasource.*;
import org.springframework.stereotype.Repository;
/**
* Created by Albedo on 6/6/2017.
*/
@Repository("reviewDAO")
public class JdbcReviewDAO {
private DataSource dataSource;
private JdbcTemplate jdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
@Bean
public JdbcTemplate getTemplate(){
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
this.jdbcTemplate = jdbcTemplate;
}
// sql methods go here
}
My spring-Datasource.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/" />
<property name="username" value="root" />
<property name="password" value="admin" />
</bean>
<bean id="reviewDAO" class="com.JdbcReviewDAO">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
My application.java:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
//ApplicationContext context =
// new ClassPathXmlApplicationContext("Spring-Review.xml");
JdbcReviewDAO reviewDAO = new JdbcReviewDAO();
reviewDAO.grabFirstPost(1);
SpringApplication.run(Application.class, args);
}
}
(I have removed package names - rest assured they are properly defined in the project. As well with SQL methods to remove redundancy. Let me know if I should include them if you suspect they are the problem.) I think this is more an 'infrastructure' issue(forgive my ignorance, still a noobie) rather than code. Anyone have an idea?
Error log: \
Exception in thread "main" java.lang.NullPointerException
at JdbcDAO.grabFirstPost(JdbcDAO.java:63)
at Application.main(Application.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Process finished with exit code 1
Method* that gives the error:
public String grabFirstPost(int id) {
String sql = "SELECT * FROM post WHERE postid ='" + id + "'";
String postContent = (String)jdbcTemplate.queryForObject(sql, new Object[] {id}, String.class);
return postContent;
}
Upvotes: 0
Views: 1707
Reputation: 5948
Your main method is wrong. You are not loading application context but directly creating your DAO. Maybe you need to change to this:
ApplicationContext context =
new ClassPathXmlApplicationContext("Spring-Review.xml");
JdbcReviewDAO reviewDAO = context.getBean("reviewDAO");
Upvotes: 1
Reputation: 70564
Looks like the variable jdbcTemplate
contains null
. The most likely cause is that Spring does not inject it, because the setter is not annotated with @Autowired
.
Upvotes: 0