JDBCTemplate给Null指针异常? [英] JDBCTemplate giving Null Pointer Exception?

查看:509
本文介绍了JDBCTemplate给Null指针异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向表中插入一些值,并且不断收到空指针异常.我最初以为是因为某些值是空的,但是即使那些永不为空的值也会给我NPE.

I'm trying to insert some values into my table and I keep getting a null pointer exception. I first thought it was because some values were null, but even with the ones which are never null give me NPE.

这是我的TwitterJDBCTemplate:

Here's my TwitterJDBCTemplate:

@Service
public class TwitterJDBCTemplate {


@Autowired
private DataSource dataSource;
private JdbcTemplate jdbcTemplateObject;




public void storeTweet(long id, long user_id, String created_at, String language, String message, String searchterm,
        String user_description, String user_location, String user_pic, String user_screenname, String username) {

    String SQL = "insert into tweets (id, user_id, created_at, language,"
            + "message, searchterm, user_description, user_location, user_pic,"
            + "user_screenname, username) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

    jdbcTemplateObject.update(SQL, new Object[] { id, user_id, created_at, language, message, searchterm,
            user_description, user_location, user_pic, user_screenname, username });
}

这是我的主应用程序:

@SpringBootApplication
@EnableScheduling
public class TwitterApp {

public static void main(String[] args) {

    // TODO Auto-generated method stub
    SpringApplication.run(TwitterApp.class, args);

}
}

我的application.properties文件:

My application.properties file:

server.port: 9040
management.port: 9041
management.address: 127.0.0.1

spring.datasource.url=jdbc:mysql://localhost:3306/twitter

spring.datasource.username=root
spring.datasource.password=********

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect 

spring.jpa.show-sql=false

spring.jpa.hibernate.ddl-auto=create

在我的TweetController内:

Inside of my TweetController:

@RestController
@Scope("singleton")
public class TweetController {


@Autowired
TwitterJDBCTemplate template;

推荐答案

您没有在应用程序上创建jdbcTemplate对象,这就是为什么要获取NPE的原因.我认为有两种可能的选择:

You did not create jdbcTemplate object at your app,that's why you are getting NPE. I think there is two posible options:

1)将jdbcTemplate bean添加到spring上下文并自动装配.

1) Add jdbcTemplate bean to spring context and autowire it.

@Bean
public JdbcTemplate getJdbcTemplate() {
   return new JdbcTemplate(dataSource());
}

还有你的班级

@Service
public class TwitterJDBCTemplate {


@Autowired
private JdbcTemplate jdbcTemplateObject;
.....
}

2)在您的类中创建jdbcTemplate

2) Create jdbcTemplate inside your class

JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

此外,我相信使用具有大量参数的方法不是最佳实践.也许您应该像这样创建自定义类Twit或smth.

In addition i bielive that it's not the best practice to use methods with big number of params. Maybe you should create your custom class Twit or smth like this.

这篇关于JDBCTemplate给Null指针异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆