Search code examples
neo4jneo4j-ogm

NullPointerException at doing Session.load() while first time trying out Neo4J OGM


I am trying out Neo4J OGM for first time from simple example given on its github page and with the help of its manual.

I am running neo4j-community-3.0.0-M05.

I am getting following exception:

Exception in thread "main" java.lang.NullPointerException
    at org.neo4j.ogm.session.delegates.LoadOneDelegate.lookup(LoadOneDelegate.java:56)
    at org.neo4j.ogm.session.delegates.LoadOneDelegate.load(LoadOneDelegate.java:49)
    at org.neo4j.ogm.session.delegates.LoadOneDelegate.load(LoadOneDelegate.java:39)
    at org.neo4j.ogm.session.Neo4jSession.load(Neo4jSession.java:137)
    at org.neo4j.ogm.session.Capability$LoadOne$load.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:110)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:130)
    at Main.main(Main.groovy:24)

The exception occurs on line 24 of Main.groovy. I debugged. It occurs on session.load() call.

I feel that this must be beacuse I must have made some mistake in setting up dependencies. But cant figure it out.

This is my code:

pom.xml

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mahesha999.exp</groupId>
  <artifactId>Neo4JTemp</artifactId>
  <version>0.0.1-SNAPSHOT</version>      
    <dependencies>
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-ogm-core</artifactId>
            <version>2.0.4</version>
        </dependency>   
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-ogm-http-driver</artifactId>
            <version>2.0.4</version>
        </dependency>   
    </dependencies>    
</project>

ogm.properties

driver=org.neo4j.ogm.drivers.http.driver.HttpDriver
URI=http://neo4j:password@localhost:7474

Actor.groovy

import org.neo4j.ogm.annotation.NodeEntity
import org.neo4j.ogm.annotation.Relationship
import org.neo4j.ogm.annotation.GraphId

@NodeEntity
public class Actor {    
    @GraphId
    private Long id;
    private String name;

    @Relationship(type = "ACTS_IN", direction = "OUTGOING")
    private Set<Movie> movies = new HashSet<>();

    public Actor() { }

    public Actor(String name) {
        this.name = name;
    }

    public void actsIn(Movie movie) {
        movies<< movie;
        movie.getActors() << this;
    }
}

Movie.groovy

import org.neo4j.ogm.annotation.NodeEntity
import org.neo4j.ogm.annotation.Relationship
import org.neo4j.ogm.annotation.GraphId

@NodeEntity
public class Movie {    
    @GraphId
    private Long id;
    private String title;
    private int released;

    @Relationship(type = "ACTS_IN", direction = "INCOMING")
    List<Actor> actors = [];

    public Movie() {}

    public Movie(String title, int year) {
        this.title = title;
        this.released = year;
    }    
}

Main.groovy

1    import org.neo4j.ogm.session.Session
2    import org.neo4j.ogm.session.SessionFactory
3    
4    class Main {
5       
6       static main(def args)
7       {
8           //Set up the Session
9           SessionFactory sessionFactory = new SessionFactory("movies.domain");
10          Session session = sessionFactory.openSession();
11          
12          Movie movie = new Movie("The Matrix", 1999);
13          
14          Actor keanu = new Actor("Keanu Reeves");
15          keanu.actsIn(movie);
16          
17          Actor carrie = new Actor("Carrie-Ann Moss");
18          carrie.actsIn(movie);
19      
20          //Persist the movie. This persists the actors as well.
21          session.save(movie);
22                  
23          //Load a movie
24          Movie matrix = session.load(Movie.class, movie.id);
25          for(Actor actor : matrix.getActors()) {
26              System.out.println("Actor: " + actor.name);
27          }
28      }   
29    }

Solution

  • The package being scanned (movies.domain) does not seem to contain your entity classes. That's why the entity is not saved, and the id is null.

    The OGM reference guide (with tutorial) is here: http://neo4j.com/docs/ogm-manual/current/

    And a small sample app: https://github.com/neo4j-examples/neo4j-ogm-university/tree/2.0