2016-09-24 7 views
1

neo4j Graphawareソリューションの実装に問題があります。以前は、次のようなGraphDatabaseServiceオブジェクトを作成していました。 -@Contextから注入された変数から他の変数を初期化する

public class TimetreeUtil { 
    public static GraphDatabaseService db; 
    public static SingleTimeTree st; 
    public static TimeTreeBackedEvents ttbe; 
    public static TimeTreeBusinessLogic ttbl; 
    public static TimedEventsBusinessLogic tebl;  
    public static List<Event> eventsFetchedFromTimetree; 

    public TimetreeUtil() { 
     db = new GraphDatabaseFactory().newEmbeddedDatabase(new File("...")); 
     st = new SingleTimeTree(db); 
     ttbl = new TimeTreeBusinessLogic(db); 
     ttbe = new TimeTreeBackedEvents(st); 
     tebl = new TimedEventsBusinessLogic(db, ttbe); 
    } 
} 

これはうまくいきました。 GraphDatabaseService、SingleTimeTree、TimeTreeBackedEvents、TimeTreeBusinessLogic、TimedEventsBusinessLogicは静的であり、neo4jが必要とするため、それらはすべきです。

しかし、今では私たちのアーキテクチャが変更されていると我々はGraphDatabaseServiceを注入している -

public class TimetreeUtil { 
     @Context 
     public GraphDatabaseService db; 
     public static SingleTimeTree st; 
     public static TimeTreeBackedEvents ttbe; 
     public static TimeTreeBusinessLogic ttbl; 
     public static TimedEventsBusinessLogic tebl;  
     public static List<Event> eventsFetchedFromTimetree; 

     public TimetreeUtil() { 
      st = new SingleTimeTree(db); 
      ttbl = new TimeTreeBusinessLogic(db); 
      ttbe = new TimeTreeBackedEvents(st); 
      tebl = new TimedEventsBusinessLogic(db, ttbe); 
     } 
    } 

Timetreeはただのオブジェクトを作成しているヘルパークラスを - だから今、クラスがどのように見える

@Context 
public GraphDatabaseService db; 

TimetreeUtilクラスをTimetreeUtil util = new TimetreeUtil();で作成し、TimetreeUtilの1つのメソッドを呼び出します。

コンストラクタが呼び出されるまでに、のdbが既に初期化されていると想定していますが、そうではありません。 dbがnullであるため、st = new SingleTimeTree(db);がNPEを提供しています。

私はどのように会うことができますか?ありがとう。

+0

デシベルが何をするか、初期化されますプロシージャを呼び出すコードは次のように見えますか? –

+0

こんにちは@ChristopheWillemsen、あなたの返信のおかげで!元の投稿を編集して、何が起きているのかをより明確に示しています。ありがとう。 –

答えて

1

オブジェクトの作成後に依存性注入が実行されます - Javaはオブジェクトインスタンス変数を設定する前にオブジェクトを作成する必要があります。したがって、NPEです。

あなた(テストの価値が、私はそれが動作するかわからない)コンストラクタでオブジェクトを渡すことができる場合があります。あなたがプロシージャを呼び出すとき

private GraphDatabaseService db; 
    public static SingleTimeTree st; 
    public static TimeTreeBackedEvents ttbe; 
    public static TimeTreeBusinessLogic ttbl; 
    public static TimedEventsBusinessLogic tebl;  
    public static List<Event> eventsFetchedFromTimetree; 

    public TimetreeUtil(@Context GraphDatabaseService db) { 
     this.db = db 
     st = new SingleTimeTree(db); 
     ttbl = new TimeTreeBusinessLogic(db); 
     ttbe = new TimeTreeBackedEvents(st); 
     tebl = new TimedEventsBusinessLogic(db, ttbe); 
    } 
+0

ありがとう!私はこの問題を解決することができます。 –

関連する問題