I am creating and trying to save a table using active android but for some reason the object fails to save. The error that I get is
java.lang.NullPointerException: Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null object reference
Upon debugging the decompiled Model.class file I observed that an exception occurs while executing the following statement
(Please note that the below line is from the decompiled Model.class file of activeandroid)
long entityId1 = ((Model)e).getId().longValue();
It fails and the above said exception occurs. In the catch block I observe that for all non-primitive type fields the mId is non null, but for the field where the exception occurs the mId is null. I tried to search the web but could only find one line about mId.
ActiveAndroid automatically creates another auto-increment ID column. This is mId.
Then why does it fail to do so in this case. Does somebody have an idea? Thanks !!
OK I found the answer.
From codepath I found that
The problem is that This is because ActiveAndroid needs you to save
all objects separately. Before saving a tweet for example, be sure to
save the associated user object first. So when you have a tweet that
references a user be sure to user.save() before you call tweet.save()
since storing the user requires the local id to be set and assigned as
the foreign key for the tweet.
Thus I had to save my non primitive type object field first before saving the object.
Related
I am trying to connect a Python program through an Arduino to a MongoDB database, however, I keep running into:
TypeError: 'Collection' object is not callable.
If you meant to call the 'collection' method on a 'Database' object it is failing because no such method exists.
(This is my first post, excuse any bad formatting and let me know if more info is needed to understand the issue)
The program used to be connected to a firebase database and the code didn't have any issues then. When I made the new database in MongoDB, I named the collections the same thing.
client = MongoClient("mongodb+srv://<username>:<password>#smartcontainer-jp0au.mongodb.net/test?retryWrites=true&w=majority") #connects to MongoDB
db = client.get_database('SmartContainerDemo') #connects to database
users_ref = db.collection(u'devices') #connects to collection (I believe error is here)
def update_device(device_num, readingGram, lastRead):
doc_ref = db.collection(u'devices').document(u'device' + str(device_num))
doc_ref.set({u'readingGram': readingGram, u'lastRead': lastRead})
I expect to have my database updated automatically when weight is put onto a scale, but instead, I am getting:
TypeError: 'Collection' object is not callable. If you meant to call the 'collection' method on a 'Database' object it is failing because no such method exists.
Check out documentation: http://api.mongodb.com/python/current/api/pymongo/database.html#pymongo.database.Database
Database object indeed does not have collection() method as the error suggests, you need to use get_collection().
Previously, when there was no stable version of realm for Java (Android), we could not store null values in realm and we had to perform some unnatural hack to be able to do so, as explained in this post.
But as of now realm 1.0 is released, are there any update about being able to store null value?
For example : unfortunate cases when there is no field in JSON which I want to store in realm after parsing but haven't handled it manually.
I have the following code:
realmObject.setData(jsonObject.getString("SELECTOR"));
the program flow stops and exits the block the code is located inside.
the logcat shows
W/System.err: org.json.JSONException: No value for SELECTOR
But when I do:
realmObject.setData(null);
The program flow does not stop and continues across my realm statement realmObject.setData(null);
In some cases, there is no value for the tag "SELECTOR" in my Json file.
And I want it to be null in default.
I actually found out that the problem is actually with just :
jsonObject.getString("SELECTOR")
not the whole statement:
realmObject.setData(jsonObject.getString("SELECTOR"));
so the fix for me was
realmObject.setData(jsonObject.optString("SELECTOR"));
you can use has that will check whether key is available of not and basis of that save value to realm object
if (jsonObject.has("SELECTOR")) {
realmObject.setData(jsonObject.getString("SELECTOR"));
}
else{
realmObject.setData(null);
}
I have seen the use of #Ignore for certain fields but I’m looking for something slightly different. https://realm.io/docs/java/latest/#models
Is it possible to specify skipping a nested object when writing a parent object to realm?
The reason for this:
I have a complex JSON object which I’m parsing and then saving to my Realm.
This object can get really large so there is some optimisation on my backend to return:
A complete object
A preview object
At some points I get a preview user object which returns only a subset of fields.
When saving to realm this overwrites the complete object (as expected) and wipes the fields not present.
The problem is that I still need those wiped fields later on.
You are not using the JSON support of Realm? If so you can use this, see last item (my emphasis):
Parsing JSON with Realm is subject to the following rules.
Creating object with JSON which has the field with a null value:
For a not-required field, set it to null which is the default value.
For a required field, throw an exception.
Updating object with JSON which has the field with a null value:
For a not-required field, set it to null.
For a required field, throw an exception.
JSON doesn’t have the field: Leave the value unchanged for both required and not-required fields.
Source:
https://realm.io/docs/java/latest/#json
One of the coolest features of the Android data binding support is that it also generates fields for View with IDs set. This tidies up the codebase as no field or findViewById() calls are necessary.
But the problem is that the binding instance can only be retrieved via the bind() call which tends to schedule binding. This is bad when the data is being received asynchronously and commonly the NullPointerException gets thrown.
Can the binding instance with View fields be retrieved minus the actual data binding process?
Stack-trace:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Boolean.booleanValue()' on a null object reference
at com.app.android.databinding.ActivityRestaurantDetailsBinding.executeBindings(ActivityRestaurantDetailsBinding.java:381)
at android.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:350)
at android.databinding.ViewDataBinding$6.run(ViewDataBinding.java:167)
at android.databinding.ViewDataBinding$5.onViewAttachedToWindow(ViewDataBinding.java:137)
at android.view.View.dispatchAttachedToWindow(View.java:14525)
This doesn't seem to make sense, data binding will ignore null variables thus no null pointer should be thrown, that is, i believe, one of its most promoted features. If you need to modify variables after async calls etc you can just use dataBinding.executePendingBindings()
From the docs
The generated binding class will have a setter and getter for each of the described variables. The variables will take the default Java values until the setter is called — null for reference types, 0 for int, false for boolean, etc.
and
Generated data binding code automatically checks for nulls and avoid null pointer exceptions. For example, in the expression #{user.name}, if user is null, user.name will be assigned its default value (null). If you were referencing user.age, where age is an int, then it would default to 0.
Got the same problem with java.lang.Boolean. Solved by using primitive boolean type instead.
<variable
name="var"
type="boolean" />
I'm trying to figure out how to use sugarORM (version 1.4, imported with Gradle).
I have a simple object with a public String name field, among others.
I construct that simple object with that name and save it like this (with TEST_TYPE a random String) :
CustomObject type = new CustomObject(TEST_TYPE);
long l=type.save();
Toast.makeText(this, "Type ajouté ! id="+l, Toast.LENGTH_SHORT).show();
My log is showing id=14 by now, so I assume writing is OK.
But when I want to read, there is no sense :
CustomObject.count(CustomObject.class) returns 14
CustomObject.findById(CustomObject.class, 2) returns null
CustomObject.first(CustomObject.class) returns null
CustomObject.find(LifeEventType.class, "NAME = ?", new String[]{TEST_TYPE}) returns null
I don't know if it is related, but I get this exception on warning log :
java.lang.NoSuchMethodException: <init> at com.orm.SugarRecord.find(SugarRecord.java:196) (which is code too advanced for a noob like me...)
What am I doing wrong ?
It happens when you don't provide an empty contructor.
SugarORM can correctly save the entities, but can't instantiate them when you try to perform a select query.
The solution is to include an empty constructor and the getters/setters within your entity
public CustomObject(){}
More documentation here