Search code examples
androidcordovacouchdbektorp

Null pointer exception when trying to fetch all documents from CouchDB database using ektorp on Android


I have simple item model with name and quantity:

public class Item  extends CouchDbDocument{
private static final long serialVersionUID = -3645214415410443346L;
private String name;
private int quantity;

public Item (String name, int qty){
    this.name = name;
    this.quantity = qty;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getQuantity() {
    return quantity;
}
public void setQuantity(int quantity) {
    this.quantity = quantity;
}
}

and an Item Repository class

public class ItemRepository extends CouchDbRepositorySupport<Item>{

public ItemRepository(CouchDbConnector db) {
        super(Item.class, db);
        initStandardDesignDocument();
}
@GenerateView @Override
public List<Item> getAll() {
    ViewQuery q = createQuery("all").descending(true);
    return db.queryView(q, Item.class);
}

and an Item Controller

public class ItemController {
ItemRepository itemRepository;

public String viewAll() {
    List<Item> items = itemRepository.getAll();        
    return items.toString();
}
}

I have a CouchDBConnector class which is added as javascript interface:

public class CouchDBConnector {
private WebView webView;
private DroidGap droidGap;
private CouchDbConnector db ;
private CouchDbInstance dbInstance;
private ItemController itemController;
private ItemRepository repo;

public CouchDBConnector(DroidGap gap, WebView view) {
    this.webView = view;
    this.droidGap = gap;
}

public void init(){
    HttpClient httpClient = new StdHttpClient.Builder()
            .host("localhost")
            .port(5984)
            .build();

    dbInstance = new StdCouchDbInstance(httpClient);
    db = new StdCouchDbConnector("itemdatabase", dbInstance);
    repo = new ItemRepository(db);
    itemController = new ItemController();
}

public String getAllItems(){
    String prefix = "Items \n";

    return prefix + repo.getAll().toString();
}

public void add(String item_name, String qty){
    repo.add(new Item(item_name, Integer.parseInt(qty)));
    return;
}
public void update(String item_name, String qty){
    repo.update(new Item(item_name, Integer.parseInt(qty)));
    return;
}
public void delete(String item_name, String qty){
    repo.remove(new Item(item_name, Integer.parseInt(qty)));
    return;
}
}

From the HTML page of the PhoneGap app, I am trying to call CouchDBConnector class to get all the documents or a string "No Items found" but I get null pointer exceptions. Here's the stack trace:

`12-14 16:12:45.273: I/dalvikvm(675): java.lang.NullPointerException:
12-14 16:12:45.273: I/dalvikvm(675):    at com.phonegap.TestDB.CouchDBConnector.getAllItems(CouchDBConnector.java:44)
12-14 16:12:45.273: I/dalvikvm(675):    at android.webkit.JWebCoreJavaBridge.nativeServiceFuncPtrQueue(Native Method)
12-14 16:12:45.273: I/dalvikvm(675):    at android.webkit.JWebCoreJavaBridge.nativeServiceFuncPtrQueue(Native Method)
12-14 16:12:45.273: I/dalvikvm(675):    at android.webkit.JWebCoreJavaBridge.handleMessage(JWebCoreJavaBridge.java:115)
12-14 16:12:45.273: I/dalvikvm(675):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 16:12:45.273: I/dalvikvm(675):    at android.os.Looper.loop(Looper.java:137)
12-14 16:12:45.273: I/dalvikvm(675):    at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:722)
12-14 16:12:45.283: I/dalvikvm(675):    at java.lang.Thread.run(Thread.java:856)
12-14 16:12:45.293: I/dalvikvm(675): "WebViewCoreThread" prio=5 tid=11 NATIVE
12-14 16:12:45.303: I/dalvikvm(675):   | group="main" sCount=0 dsCount=0 obj=0x41381ca0 self=0x1dbf90
12-14 16:12:45.303: I/dalvikvm(675):   | sysTid=688 nice=0 sched=0/0 cgrp=default handle=1748120
12-14 16:12:45.312: I/dalvikvm(675):   | schedstat=( 3460347960 1824920390 1206 ) utm=321 stm=24 core=0
12-14 16:12:45.312: I/dalvikvm(675):   at android.webkit.JWebCoreJavaBridge.nativeServiceFuncPtrQueue(Native Method)
12-14 16:12:45.312: I/dalvikvm(675):   at android.webkit.JWebCoreJavaBridge.nativeServiceFuncPtrQueue(Native Method)
12-14 16:12:45.323: I/dalvikvm(675):   at android.webkit.JWebCoreJavaBridge.handleMessage(JWebCoreJavaBridge.java:115)
12-14 16:12:45.323: I/dalvikvm(675):   at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 16:12:45.323: I/dalvikvm(675):   at android.os.Looper.loop(Looper.java:137)
12-14 16:12:45.333: I/dalvikvm(675):   at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:722)
12-14 16:12:45.343: I/dalvikvm(675):   at java.lang.Thread.run(Thread.java:856)

Solution

  • Looks like you forgot to call init() from your constructor, because the NPE is in getAllItems(), so eiter repo is null or repo.getAll() is.