Announcement Announcement Module
Collapse
No announcement yet.
[SDN] NPE Neo4 Repository.save() Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • [SDN] NPE Neo4 Repository.save()

    Hi all,
    i'm new to SDN and Neo4j.

    Basically what i am trying to do is: Autowiring a Repository in a Class and then instantiate this class on my own and not via Dependency Injection of Spring.

    Here is what my Code looks like:

    Peson.class (NodeEntity)

    Code:
    package hello;
    
    @NodeEntity
    public class Person {
    
        @GraphId Long Graphid;
        @Indexed public String name;
        public String Type;
    
        public Person() {}
        public Person(String name, String type) {
            this.name = name;
            this.Type = type;
            }
    
        @RelatedTo(type = "knows", direction = Direction.OUTGOING)
        public Set<Person> successors;
        
        @RelatedTo(type="pre", direction=Direction.INCOMING)
        public Person predecessor;
    
    }
    PersonRepository:

    Code:
    package hello;
    
    @Repository
    public interface PersonRepository extends GraphRepository<Person>, CRUDRepository<Person> {
    
        Person findByName(String name);
    }
    The Class i try to instantiate:

    Code:
    package hello;
    
    public class UseRepo {
    
        @Autowired
        private PersonRepository repo;
    
        public UseRepo(){ }
    
        public void savetodatabase(){
               
            Person person = new Person("Testperson", "ExternalPerson");
            
            System.out.println("Person to be saved:" + person.getName() );
            
            Person ret = repo.save(person);
            
            System.out.println("Person saved to db:" + ret.getName() + " ID: " +ret.getID() );
    
            
        }
    }
    And here a simple Junit Testcase where i try to instantiate the UseRepo class and call the savetodatabase Method:

    Code:
    package hello;
    
    @ContextConfiguration(locations={"classpath*:Context.xml"})
    @RunWith(SpringJUnit4ClassRunner.class)
    @Transactional
    public class UseRepoTest {
        
    
        private UseRepo repo;
    
        @Before
        public void setUp() throws Exception {
            repo = new UseRepo();
        }
    
        @After
        public void tearDown() throws Exception {
        }
    
        @Test
        public void test() {
            
            repo.savetodatabase();
        }
    
    }
    My Context.XML looks like this:

    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/data/neo4j
    http://www.springframework.org/schema/data/neo4j/spring-neo4j.xsd"
    
    >
    
    <context:annotation-config/>
    <context:component-scan base-package="hello" />
    
    <neo4j:config storeDirectory="target/config-test" base-package="hello"/>
    <neo4j:repositories base-package="hello" />
    
    </beans>
    And the last snippet my POM.XML

    Code:
    <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>org.springframework</groupId>
      <artifactId>gs-accessing-data-neo4j</artifactId>
      <version>0.1.0</version>
      <packaging>jar</packaging>
    <repositories>
               <repository>
                <id>spring-milestones</id>
                <url>http://repo.springsource.org/libs-milestone/</url>
              </repository>
          </repositories>
           <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
        
        <dependencies>   
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-neo4j</artifactId>
                 <version>2.3.3.RELEASE</version>
            </dependency>
    
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.11</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>cglib</groupId>
                <artifactId>cglib</artifactId>
                <version>2.2.2</version>
            </dependency>
     
            <dependency>
                <groupId>javax.validation</groupId>
                <artifactId>validation-api</artifactId>
                <version>1.1.0.Final</version>
            </dependency>
        
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>3.1.4.RELEASE</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-nop</artifactId>
                <version>1.6.1</version>
            </dependency>
        </dependencies>
    </project>
    If I run the UseRepoTest i get the following NPE:

    Code:
    java.lang.NullPointerException
        at hello.UseRepo.savetodatabase(UseRepo.java:40)
        at hello.UseRepoTest.test(UseRepoTest.java:36)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
        at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
        at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
        at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
        at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
    Can anybody tell me what i am doing wrong?
    Thanks in advance.

  • #2
    Please, move your question to the StackOverflow - we are going to close this forum soon and rely on SO.
    We need to clean this forum (old unanswered question) before close it.

    Thanks for understanding

    Comment

    Working...
    X