Getting a null pointer exception while calling getTimeStamp from firebase database - android

The first time i run the app after uninstalling & reinstalling sometimes it crashes due to
viewHolder.setTimestamp(DateUtils.getRelativeTimeSpanString((long) model.getTimestamp()).toString());
giving an error
Exception java.lang.NullPointerException: Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null object reference
what should i do to solve this issue as after crashing once the next time everything works fine.If you need more codes to guide me then just ask.

you can use it in try catch
like this way.
try
{
viewHolder.setTimestamp(DateUtils.getRelativeTimeSpanString((long) model.getTimestamp()).toString());
}
catch(Exception e)
{
}

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

FirebaseAuth.getCurrentUser() is returning null value

FirebaseAuth.getCurrentUser() is returning null value. Previously my code worked fine but since couple of days its returning currentUser as null.
Also I've tried my app on another device and in that device everything is working fine with the same code.
mAuth=FirebaseAuth.getInstance();
mCurrentUser=mAuth.getCurrentUser()
mUserDatabase=FirebaseDatabase.getInstance().getReference().child("Users")
.child(mCurrentUser.getUid());
Exception:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'java.lang.String
com.google.firebase.auth.FirebaseUser.getUid()' on a null object
reference
I have already tried erasing app data and uninstalling and reinstalling my app on my device but nothing worked.
Is there a problem with my device or Firebase?
It looks like the user isn't signed into your app, which causes mCurrentUser.getUid() to throw an exception.
The solution is to only call getUid() is there is a signed-in yser:
mCurrentUser=mAuth.getCurrentUser();
if (mCurrentUser != null) {
mUserDatabase=FirebaseDatabase.getInstance().getReference().child("Users")
.child(mCurrentUser.getUid());
}

Attempt to get length of null array exception

Whenever I call getContentResolver().applyBatch(authority,batch), My app crashes on a Asus mobile, while it works fine on other android smartphones.
Logcat
java.lang.NullPointerException: Attempt to get length of null array
at android.content.ContentProvider$Transport.applyBatch(ContentProvider.java:288)
at android.content.ContentProviderClient.applyBatch(ContentProviderClient.java:377)
at android.content.ContentResolver.applyBatch(ContentResolver.java:1244)
How can I solve this problem?
What happens:
You are calling ContentResolver.applyBatch() with a null operations array, this is why you receive a NullPointerException.
This method expects this parameter not to be null, as shown by its signature:
public #NonNull ContentProviderResult[] applyBatch(#NonNull String authority,
#NonNull ArrayList<ContentProviderOperation> operations)
throws RemoteException, OperationApplicationException
You should test the validity of your batch array before calling applyBatch(), and refrain from calling it should batch be null (assuming that having it null means there's no operation to apply).
Why does it work on some cellphones and not on other?
Either because the list of operations to apply depends on the cellphone itself or its configuration (installed apps, Android API, stored data, anything...), which makes it null in the specific case of your Asus ;
or because it works "by accident" on the other cellphones you have tested. This happens sometimes when the code does something wrong but without raising any error due to some permissivity you cannot rely on.
This depends on what your app does.

Error using getView() with a Listview

I'm am a newbie with Android development and using the following example to do something similar: https://stackoverflow.com/a/11626706/5724649
But am getting the following error: "Attempt to invoke virtual method 'java.lang.Object java.util.ArrayList.get(int)' on a null object reference"when I try to set one of the radio button. Also mSource (which is an ArrayList) is null within getView(), so I see why I could get this error, but not sure as to how I should pass the arraylist to getView(). Please help.
The error is about you calling a method on an object which currently "null".
This will also throw the same error:
String a = null;
a.length(); // <<< This will cause an error, because a is null.
You should make sure the following line:
mSource = new ArrayList<RowObject>();
is running before any of these lines:
mSource.get(position).setFirstChecked(true);
mSource.get(position).setFirstChecked(false);
if (mSource.get(position).isFirstChecked()) {

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