2012-04-24 105 views
1

我有這些類:堅持雙向關係ormlite

public class AllegationContent { 

    @DatabaseField(generatedId = true) 
    protected long id; 

    @DatabaseField(canBeNull = true, foreign = true) 
    private AllegationItem allegationItem; 

    @DatabaseField() 
    protected String content; 
    ... 
} 

和:

public class AllegationItem { 
    @DatabaseField(generatedId = true) 
    protected long id; 

    @ForeignCollectionField(eager = false) 
    protected ForeignCollection<AllegationContent> allegationContent; 
    ... 
} 

我如何能堅持兩者兼而有之? 我想這一點:

allegationContentDao = getAllegationContentDao(); 
    AllegationContent allegationContent = new AllegationContent(); 
    allegationContent.setContent("allegation content");  
    allegationContentDao.create(allegationContent); 

    allegationItemDao = getAllegationItemDao(); 
    AllegationItem allegationItem = new AllegationItem(); 
    AllegationContent allegationContent2 = allegationContentDao.queryForSameId(allegationContent); //is that wrong? 
    allegationItem.getAllegationContent().add(allegationContent2); 
    allegationItemDao.create(allegationItem); 

,但我得到這個錯誤:

04-24 10:41:57.128: ERROR/AndroidRuntime(802): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.project/br.com.project.DaoTestActivity}: java.lang.NullPointerException

+0

那麼NPE發生了什麼?你有沒有試過調試你的代碼來找出那行上哪些對象是空的?沒有足夠的信息讓任何人弄清楚你試圖訪問空指針的代碼在哪裏。 – Gray 2012-04-24 14:00:41

+0

我沒找到線。我從AllegationItem中刪除了ForeignCollection,並嘗試創建一個AllegationItem,並得到這個錯誤:「INFO/Database(1024):sqlite returned:error code = 1,msg = near」)「syntax」 我無法理解.. 。AllegationItem只有字段Id。 – Munir 2012-04-24 14:33:54

+0

對不起,我們提供的幫助信息太少。 – Gray 2012-04-24 14:36:28

回答

1

我解決了。表不能創建,因爲沒有屬性,只有id(什麼是另一個問題)。 NPE的發生是因爲getAllegationContent返回null,所以首先我需要創建一個ForeignCollection列表,使用setAllegationContent設置然後獲取。