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());
}
Related
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
I am making a chatting application in which when I login, I get this error in Logcat and my app crashes.
2022-03-12 16:09:44.805 22115-22115/com.dccodes.chugli E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.dccodes.chugli, PID: 22115
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at com.dccodes.chugli.MainActivity$2.onDataChange(MainActivity.java:91)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6810)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
I don't understand why I get this error, I used firebase for my app. My main activity is very large, so I uploaded the photo of code.
I don't understand why I get this error.
Try using addListenerForSingleValueEvent(new ValueEventListener() {
instead of ValueEventListener
Because ValueEventListener is triggered everytime that you have a change in 'reference' tree, if nothing happens it won't be triggered, and this block of code won't run.
However addListenerForSingleValueEvent runs this block of code one time no matter what.
Also check that 'refernece' is pointing to the exact place in your Firebase that you want to listen to. And if it's right check that the user it's pointing to isn't null.
When attempting to launch billing for an in-app item, I am getting the following error.
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
The code is simple. The user selects an item. I then pass that item into querySkuDetailsAsync. I get and see the result. For API reasons, I then call...
SkuDetails skuDetails = new SkuDetails(skuDetails.getOriginalJSON());
BillingFlowParams flowParams = BillingFlowParams.newBuilder().setSkuDetails(skuDetails).build();
BillingResult billingResult = client.launchBillingFlow(activity, flowParams);
And then, most of the time, I get the NPE. Things were working fine. I don't know what change caused this issue.
Note that I did try passing the skuDetails directly, in case JSON was the issue. It didn't work.
The billing library isn't open source, so I am at a loss as to how to solve this issue.
I found a solution. If I call activity.setIntent(new Intent()) before calling "launchBillingFlow", the error goes away. Seems like a bug in the now closed sourced billing library.
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)
{
}
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.