samedi 11 avril 2015

Null Pointer Exception for JdbcTemplate with SpringMVC

I'm pretty new to Java, but I wanted to try to build a simple project with SpringMVC. It's just a simple CRUD app that should allow people to post notes.


When attempting to submit the form or query for notes, I get a null pointer exception when I attempt to call methods on JdbcTemplate. Here is the code for my application class:



package mvc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.JdbcTemplate;

@SpringBootApplication
public class SpringMvcApplication implements CommandLineRunner {

public static void main(String[] args) {
SpringApplication.run(SpringMvcApplication.class, args);
}

@Autowired
JdbcTemplate jdbcTemplate;

public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}

public void run(String... string) throws Exception {
System.out.println("Creating tables");
jdbcTemplate.execute("drop table notes if exists");
jdbcTemplate.execute(("create table notes(" +
"id serial, content varchar(255), author varchar(255))"));
}

}


Here is my NoteController.java:



package mvc;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class NoteController {
@RequestMapping("/notes")
public String index() {
return "index";
}

@RequestMapping("/notes/new")
public String newNote(Model model) {
model.addAttribute("note",new Note());
return "new";
}

@RequestMapping(value="/notes", method= RequestMethod.POST)
public String create(Note note, Model model) {
model.addAttribute("note", note);
Note.create(note.getContent(), note.getAuthor());
return "show";
}

@RequestMapping(value = "/notes/{noteId}", method=RequestMethod.GET)
public String show(@PathVariable String noteId) {
long id = Long.parseLong(noteId);
Note note = Note.find(id);
return "show";
}
}


My Note model:



package mvc;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class Note {

private Long id;
private String content;
private String author;

public Note() {}

public Note(Long id, String content, String author) {
this.id = id;
this.content = content;
this.author = author;
}

public Long getId() {
return id;
}

public String getContent() {
return content;
}

public String getAuthor() {
return author;
}

public String toString() {
return content + "by " + author;
}

public static void create(String content, String author) {
JdbcTemplate jdbcTemplate = SpringMvcApplication.getInstance().getJdbcTemplate();
jdbcTemplate.update("INSERT INTO notes(content, author) values (?,?)", content, author);
}

public static Note find(Long id) {
JdbcTemplate jdbcTemplate = SpringMvcApplication.getInstance().getJdbcTemplate();
List<Note> notes = jdbcTemplate.query("SELECT id, content, author FROM notes WHERE id = ?", new Object[]{id},
new RowMapper<Note>() {
@Override
public Note mapRow(ResultSet rs, int rowNum) throws SQLException {
return new Note(rs.getLong("id"), rs.getString("content"),
rs.getString("author"));
}
});
return notes.get(0);
}
}


Here's my pom.xml:



<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://ift.tt/IH78KX" xmlns:xsi="http://ift.tt/ra1lAU"
xsi:schemaLocation="http://ift.tt/IH78KX http://ift.tt/VE5zRx">

<modelVersion>4.0.0</modelVersion>

<groupId>org.test</groupId>
<artifactId>mvc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Spring MVC</name>
<description>MVC Project for Ship It Saturday</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>mvc.SpringMvcApplication</start-class>
<java.version>1.7</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.4</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.3-1100-jdbc41</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>


I'm not really sure what the issue is with my code here. I was following a few different guides but most of them weren't too clear on what code was doing what. I'm sure there are probably some really obvious issues here, but I'm totally new to Spring and Java so I'm not seeing it.


Aucun commentaire:

Enregistrer un commentaire