Search code examples
postgresqlactivejdbcjavalite

Postgres driver for activejdbc keeps throwing nullpointers


I've been trying to get this example for activejdbc to work to no avail. My database has the tables in to make use of this code, this is how it looks when opened in my editor (intellij):

database view

I cut out some tables that aren't used by this code, as it's just to test for a different project, and those tables don't matter for this question.

The code I'm trying to run is as follows:

package activejdbc.examples.simple;

import org.javalite.activejdbc.Base;
import org.javalite.activejdbc.DB;
import org.postgresql.Driver;

public class Main {

public static void main(String[] args) {
    try {

        System.out.println(Class.forName("org.postgresql.Driver"));


    } catch (ClassNotFoundException e) {

        System.out.println("Where is your PostgreSQL JDBC Driver? "
                + "Include in your library path!");
        e.printStackTrace();
        return;

    }
    Base.open("org.postgresql.Driver", "jdbc:postgresql://<ip>:<port>/<database_name>", "<database_user>", "<password>");

    Person director  = new Person();
    director.set("name", "Steven Spielberg");
    director.saveIt();

    director.add(new Movie("Saving private Ryan", 1998));
    director.add(new Movie("Jaws", 1982));
    director.getAll(Movie.class).forEach(System.out::println);

    Base.close();
    }
}

The objects (Movie, MoviesPeople, Person) are all empty classes as per http://javalite.io/activejdbc#supported-databases

and this gives me the following errors when run:

Connected to the target VM, address: '127.0.0.1:42363', transport: 'socket'
class org.postgresql.Driver
Exception in thread "main" org.javalite.activejdbc.InitException: java.lang.NullPointerException
    at org.javalite.activejdbc.Registry.init(Registry.java:154)
    at org.javalite.activejdbc.Registry.getMetaModel(Registry.java:103)
    at org.javalite.activejdbc.ModelDelegate.metaModelOf(ModelDelegate.java:289)
    at org.javalite.activejdbc.Model.<init>(Model.java:79)
    at activejdbc.examples.simple.Person.<init>(Person.java:10)
    at activejdbc.examples.simple.Main.main(Main.java:25)
Caused by: java.lang.NullPointerException
    at org.postgresql.jdbc2.AbstractJdbc2DatabaseMetaData.getColumns(AbstractJdbc2DatabaseMetaData.java:2502)
    at org.postgresql.jdbc4.AbstractJdbc4DatabaseMetaData.getColumns(AbstractJdbc4DatabaseMetaData.java:99)
    at org.javalite.activejdbc.Registry.fetchMetaParams(Registry.java:198)
    at org.javalite.activejdbc.Registry.init(Registry.java:138)
    ... 5 more
Disconnected from the target VM, address: '127.0.0.1:42363', transport: 'socket'

It seems as though the database connection is never made, and the objects can't be made/called from activejdbc. But I have no idea what's going wrong here, I feel like it should work, I know the url used for my database is correct, as it works in my editor to connect to the database. And the driver is clearly there and working, as my check doesn't fail. Any suggestions?

PS: this is my pom.xml if that is any help to figuring this out:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.heroku.sample</groupId>
<artifactId>webappRunnerSample</artifactId>
<version>1.0-SNAPSHOT</version>
<name>webappRunnerSample Maven Webapp</name>
<packaging>war</packaging>
<url>http://maven.apache.org</url>
<properties>
    <activejdbc.version>2.1-SNAPSHOT</activejdbc.version>
    <environments>development.test,development</environments>
</properties>
<build>
    <finalName>webappRunnerSample</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.javalite</groupId>
            <artifactId>activejdbc-instrumentation</artifactId>
            <version>2.0</version>
            <executions>
                <execution>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>instrument</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.javalite</groupId>
            <artifactId>db-migrator-maven-plugin</artifactId>
            <version>2.0</version>
            <configuration>
                <configFile>
                    ${project.basedir}/src/main/resources/database.properties
                </configFile>
                <environments>${environments}</environments>
            </configuration>
            <executions>
                <execution>
                    <id>dev_migrations</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>migrate</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>postgresql</groupId>
                    <artifactId>postgresql</artifactId>
                    <version>9.1-901-1.jdbc4</version>
                </dependency>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>5.1.34</version>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.18.1</version>
            <configuration>
                <reportFormat>brief</reportFormat>
                <trimStackTrace>true</trimStackTrace>
                <useFile>false</useFile>
                <includes>
                    <include>**/*Spec*.java</include>
                    <include>**/*Test*.java</include>
                </includes>
                <excludes>
                    <exclude>**/helpers/*</exclude>
                    <exclude>**/*$*</exclude>
                </excludes>

            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>com.github.jsimone</groupId>
                                <artifactId>webapp-runner</artifactId>
                                <version>7.0.22</version>
                                <destFileName>webapp-runner.jar</destFileName>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.javalite</groupId>
        <artifactId>activejdbc</artifactId>
        <version>2.0</version>
        <exclusions>
            <exclusion>
                <groupId>opensymphony</groupId>
                <artifactId>oscache</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.1-901-1.jdbc4</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.34</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.8.0-beta1</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy</artifactId>
        <version>1.8.3</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.2.3</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.25.1</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.json</artifactId>
        <version>1.0.2</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>com.sun.xml.ws</groupId>
        <artifactId>jaxws-rt</artifactId>
        <version>2.2.8</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-core</artifactId>
        <version>2.2.7</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.stream.buffer</groupId>
        <artifactId>streambuffer</artifactId>
        <version>1.5.3</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.2.7</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.ws</groupId>
        <artifactId>policy</artifactId>
        <version>2.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.gmbal</groupId>
        <artifactId>gmbal-api-only</artifactId>
        <version>3.2.0-b003</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.ha</groupId>
        <artifactId>ha-api</artifactId>
        <version>3.1.9</version>
    </dependency>
</dependencies>
<repositories>
    <repository>
        <id>snapshots1</id>
        <name>JavaLite Snapshots1</name>
        <url>http://repo.javalite.io/</url>
        <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
            <checksumPolicy>warn</checksumPolicy>
        </snapshots>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>snapshots2</id>
        <name>JavaLite Snapshots2</name>
        <url>http://repo.javalite.io/</url>
        <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
            <checksumPolicy>warn</checksumPolicy>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

Solution

  • Try a more up-to-date PostgreSQL driver. Here is the Maven definition:

            <dependency>
                <groupId>org.postgresql</groupId>
                <artifactId>postgresql</artifactId>
                <version>9.3-1102-jdbc4</version>
                <scope>test</scope>
            </dependency>