How to remove the 'is null error' in Flutter BLOC pattern? - android

When I run my app and select a dog breed of my choice and like it, I get an error saying -
════════ Exception caught by gesture ═══════════════════════════════════════════
The following NoSuchMethodError was thrown while handling a gesture:
The method 'contains' was called on null.
Receiver: null
Tried calling: contains(Instance of 'BreedsModel')
The likedList list contains all the dog breeds that I have liked. How do I solve this problem?
Here is the link to my code -
My Code

This is because you're calling the contains function on a null variable.
try printing the variable on which you're calling contains function.
print(likedList);
//output will "null"
make sure you're handling the null values

Related

Android Crashlytics Null Pointer exception, on a non null var

Craslitycs send a crash event of null pointer exception
NullPointerException: Attempt to invoke interface method 'java.lang.Object[] java.util.Collection.toArray()' on a null object reference
that refers to mutablelist.addAll(it.list), where it.list is an object from an api response.
When I try to check if this can be null, (it.list != null), AndroidStudio suggest Condition 'it.list != null' is always 'true' so, why this crash? And how can i fix it?
Thanks
You can check your list is empty or not by using kotlin collections function.
Use below function
ArrayList.isNullOrEmpty()
Reference link:
Kotlin collection

How to fix Null check operator used on a null value in Flutter

I am trying to save data to firebase, I used if(_formKey.currentState!.validate()) to valid the Textfield, and I get an error "Null check operator used on a null value in Flutter", Can anyone help me, please?
You can check the code.

Only safe or non null assserted calls are allowed on a nullable receiver type of arraylist

Just started using kotlin for android development.My arraylist is declared like this-
var day1: ArrayList<DietPlanDetailModel>? = null
Now I am trying to access an element by its position
val dietPlan= day1[position]
but i am getting below compile time error-
Only safe or non null assserted calls are allowed on a nullable
receiver type of arraylist
Why am i getting this error and how can i resolve it?
The problem is, that you defined the ArrayList as nullable. You have two options here:
don't define the variable nullable (this depends on your code):
var day1: ArrayList<DietPlanDetailModel> = ArrayList()
access your data-structure with a null check:
val dietPlan= day1?.get(position)
As defined, day1 can be null but you're invoking a function by doing [], which is basically the same as calling day1.get(index).
This can throw a NullpointerException, which the Kotlin compiler tries to prevend. Thus, only safe calls like this are allowed: day1?.get().
You told compiler that your variable can be null (and assigned null to it).
day1[position] is essentially day1.get(position) which will crash with NPE if day1 is null -> null.get(position)
If you can guarantee that day1 will be initialized id recommend lateinit or just straight up assigning new Arraylist with declaration. Of course, simple day1?.get(position) works fine.

What is mId in activeandroid and when can it be null?

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.

Robolectric - ShadowCanvas - NullPointerException on InvocationPlan.toString()

When trying to execute ShadowCanvas.getHeight(), I get a NullPointerException in ShadowWrangler.InvocationPlan.toString()
My app code:
public float getCanvasHeight() {
return mCanvas.getHeight();
}
Throws exception:
Method threw 'java.lang.NullPointerException' exception. Cannot
evaluate
com.xtremelabs.robolectric.bytecode.ShadowWrangler$InvocationPlan.toString()
Any ideas what I'm doing wrong?
I'm guessing that this shows up in your debugger but doesn't get thrown when you actually run the code?
The debugger probably calls toString() when showing the variables, and some value used in the implementation of toString() isn't yet initialized. So instead of showing you the string of the contents, it shows this exception.
Unless you actually call toString() in the code this exception wouldn't be thrown at runtime, although the uninitialized value could be an issue somewhere else.

Categories

Resources