Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Testing Our Implementations

NullPointerException

Here is the error message I'm getting:

java.lang.NullPointerException
    at com.teamtreehouse.courses.dao.Sql2oCourseDaoTest.tearDown(Sql2oCourseDaoTest.java:28)    

And here is what my Sql2oCourseDaoTest.java file looks like:

package com.teamtreehouse.courses.dao;

import com.teamtreehouse.courses.model.Course;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.sql2o.Connection;
import org.sql2o.Sql2o;

import static org.junit.Assert.*;

public class Sql2oCourseDaoTest {

    private Sql2oCourseDao dao;
    private Connection conn;

    @Before
    public void setUp() throws Exception {
        String connectionString = "jdbc:h2:mem:testing;INIT=RUNSCRIPT from 'classpath:db/init.sql'";
        Sql2o sql2o = new Sql2o(connectionString);
        dao = new Sql2oCourseDao(sql2o);
        //keep connection open through entire test so that it isn't wiped out.
        conn = sql2o.open();
    }

    @After
    public void tearDown() throws Exception {
        conn.close();
    }

    @Test
    public void addingCourseSetsId() throws Exception {
        Course course = new Course("Test", "http://test.com");
        int originalCourseId = course.getId();

        dao.addCourse(course);

        //adding a course to the database should change it's id:
        assertNotEquals(originalCourseId, course.getId());
    }
}

And my Sql2oCourseDao.java file (where I'm apparently getting the error):

import org.sql2o.Sql2oException;

import java.util.List;

public class Sql2oCourseDao implements CourseDao {
    private List<Course> courses;
    private final Sql2o sql2o;

    public Sql2oCourseDao(Sql2o sql2o) {
        this.sql2o = sql2o;
    }

    @Override
    public void addCourse(Course course) throws DaoException {
        //string to hold our SQL query:
        String sql = "INSERT INTO courses(name, url) VALUES (:name, :url)";
        try (Connection con = sql2o.open()) {
            //executing the SQL query and getting the id that was returned
            int id = (int) con.createQuery(sql)
                    .bind(course)
                    .executeUpdate()
                    .getKey();

            //setting the course id equal to the id returned from our database:
            course.setId(id);
        } catch (Sql2oException exc) {
            //throwing a DaoException if anything went wrong with our database connection:
            throw new DaoException(exc, "Problem adding course");
        }

    }

    @Override
    public List<Course> findAll() {
        return this.courses;
    }
}

UPDATE:

I fixed the problem. I forgot to add an empty string for the username and password fields for the Sql2o constructor. I thought those fields were optional because my IDE wasn't giving me any problems but I guess not.