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

Binyamin Friedman
Binyamin Friedman
14,615 Points

I'm getting a NullPointerException

This is my test class:

package com.teamteehouse.courses.dao;

import com.teamteehouse.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.add(course);

        assertNotEquals(originalCourseId, course.getId());
    }
}
org.sql2o.Sql2oException: Could not acquire a connection from DataSource - Syntax error in SQL statement "RUNSCRIPT from [*]'classpath:db/init.sql"; SQL statement:
RUNSCRIPT from 'classpath:db/init.sql [42000-191]

    at org.sql2o.Connection.createConnection(Connection.java:291)
    at org.sql2o.Connection.<init>(Connection.java:51)
    at org.sql2o.Sql2o.open(Sql2o.java:225)
    at com.teamteehouse.courses.dao.Sql2oCourseDaoTest.setUp(Sql2oCourseDaoTest.java:23)

Caused by: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "RUNSCRIPT from [*]'classpath:db/init.sql"; SQL statement:
RUNSCRIPT from 'classpath:db/init.sql [42000-191]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
    at org.h2.message.DbException.get(DbException.java:179)
    at org.h2.message.DbException.get(DbException.java:155)
    at org.h2.message.DbException.getSyntaxError(DbException.java:191)
    at org.h2.command.Parser.getSyntaxError(Parser.java:528)
    at org.h2.command.Parser.checkRunOver(Parser.java:3678)
    at org.h2.command.Parser.initialize(Parser.java:3589)
    at org.h2.command.Parser.parse(Parser.java:302)
    at org.h2.command.Parser.parse(Parser.java:291)
    at org.h2.command.Parser.prepareCommand(Parser.java:252)
    at org.h2.engine.Session.prepareLocal(Session.java:560)
    at org.h2.engine.Session.prepareCommand(Session.java:501)
    at org.h2.engine.Engine.openSession(Engine.java:220)
    at org.h2.engine.Engine.createSessionAndValidate(Engine.java:153)
    at org.h2.engine.Engine.createSession(Engine.java:136)
    at org.h2.engine.Engine.createSession(Engine.java:28)
    at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:349)
    at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:107)
    at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:91)
    at org.h2.Driver.connect(Driver.java:72)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:208)
    at org.sql2o.GenericDatasource.getConnection(GenericDatasource.java:62)
    at org.sql2o.Connection.createConnection(Connection.java:288)
    ... 34 more


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

Craig did the same thing as me, so I am confused. The error seems to be saying that I wrote my connectionString wrong.

2 Answers

Seth Kroger
Seth Kroger
56,413 Points

You need to close off the name of the SQL file with a single quote.