NullPointerException when trying to add an object to many relationship of other object
java.lang.NullPointerException at com.activeandroid.query.From.getArguments(Unknown Source) at com.activeandroid.query.From.execute(Unknown Source) at com.activeandroid.Model.getMany(Unknown Source) at it.mediaprime.terme.models.RKThermae.categories(RKThermae.java:20)
@Table(name = "RKThermaes")
public class RKThermae extends Model{
@Column(name ="ThermaeId", unique = true)
public int thermaeId;
public List<RKCategory> categories() {
return getMany(RKCategory.class, "RKThermae");
};
@Column(name ="Gallery")
public RKGallery gallery;
...
public void update(JSONObject json){
JSONArray arrayCategories = json.optJSONArray("categories");
if (arrayCategories != null) {
int categoriesLength = arrayCategories.length();
for (int i = 0; i < categoriesLength; i++) {
int categoryId = arrayCategories.optInt(i);
RKCategory category = RKCategory.find(categoryId);
if(category != null){
try{
this.categories().add(category);
} catch (Exception e){
Log.e("thermae add category", "EXC", e);
}
}
}
}
}
}
public class RKCategory extends Model {
public List<RKProduct> products() {
return getMany(RKProduct.class, "RKCategory");
};
@Column(name = "ModelId")
public int modelId;
@Column(name = "CategoryId", unique = true)
public int categoryId;
@Column(name = "Deleted")
public boolean deleted;
@Column(name = "CreatedAt")
public String createdAt;
@Column(name = "Title")
public String title;
@Column(name = "ModelType")
public String modelType;
...
static public RKCategory find(int id){
return new Select().from(RKCategory.class).where("categoryId = ?", id).executeSingle();
}
}
I didnt’t look into this, but RKCategory is lacking the @Table-annotation.
is only the copy error , i didn't selected it ..
I would like to Vote up this issue. any updates on this?
Allright, I had a closer look at this and I think you are just using the models in a wrong way.
First, you cannot add a model to the relationship like this:
this.categories().add(category);
The call to categories() just does a query and returns a list with the query result. Adding to that list will NOT add a model to the relationship. In order to do that, you would have to do something like:
RKCategory category = getCategoryFromSomewhere();
category.setThermae(this);
category.save();
ActiveAndroid does not support ManyToMany relationships out of the box (at least it did not last time I checked). Product<->Category is one such relationship. For that to work, you would have to create a "binding" model like this (in SQL, you would also use a binding table to do the many-to-many relationship):
@Table(name="ProductToCategory")
public class ProductToCategory extends Model {
@Column(name="CategoryFK")
public Category category;
@Column(name="ProductFK")
public Product product;
}
Now, to the NullPointerException, did you save the model before calling the update? The stacktrace looks exactly like trying to get the models in relationship from a not-yet-saved model.
I've writen a small test that demonstrates this (and what you were trying to do) here: https://gist.github.com/vsigler/6252929
Just checkout ActiveAndroid, add this file to the tests subproject in org.activeandroid.test package run the instrumentation tests. You will get 2 failures. One is the NullPointerException and the other is the attempt to add models into the relationship by adding to the collection.