Robolectric - ShadowCanvas - NullPointerException on InvocationPlan.toString() - android

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.

Related

I found that the result of `UCallExpression.resolve()` is `null` when I execute lint detect

When I execute lint detect,I found that the result of UCallExpression.resolve() is null in UElementVisitor#DelegatingPsiVisitor.visitMethodCallExpression.
This problem only occurs when detecting some methods, and different methods in the same class will not have this problem.
Pray for answers ...

Android Studio Return Type No Match

Question:
Does anyone know why CallCharMethod does not work? It is logging CallCharMethodV which is weird, because as you can see below, I am not using CallCharMethodV. Some help would be appreciated it.
Error:
JNI DETECTED ERROR IN APPLICATION: the return type of CallCharMethodV does not match java.lang.CharSequence android.widget.TextView.getText()
Code:
globalEnv->CallVoidMethod(jobj, globalEnv->GetMethodID(env->GetObjectClass(jobj), "sendToastMessage", "(Ljava/lang/CharSequence;)V"),
env->CallCharMethod(jview, env->GetMethodID(env->FindClass("android/widget/TextView"), "getText", "()Ljava/lang/CharSequence;")));
java.lang.CharSequence is an interface, so whatever is returned from that method will be an object, not a char. Use CallObjectMethod instead.
The reason that it is complaining about CallCharMethodV is because CallCharMethod forwards its variable argument list to that method.

How to crash app inside RxJava map operator

When, for example, a NullPointerException occurs after calling the RxJava map operator, the app doesn't crash. I want the app to crash when this happens so it can submit reports to crashlytics etc.
I tried using Exceptions.propagate() in a try/catch block but that didn't work.
The only solution that I found was throwing a RuntimeException in my error handler.
override fun getCategories(): Single<List<Category>> {
return ApiManager
.getCategoriesService()
.getCategories()
.map { categoriesResponse ->
throw KotlinNullPointerException
categoriesResponse.categories?.map { categoryResponse ->
CategoryMapper().mapFromRemote(categoryResponse)
}
}
}
The NullPointerException thrown inside the map operator does not crash the app.
If I call it before the return statement it crashes the app.
If I call it before the return statement it crashes the app.
It crashes because getCategories method is running on Android's main thread to build the rx chain.
The NullPointerException thrown inside the map operator does not crash the app.
It doesn't crash the app because this chain is being subscribed to a thread that is not Android's main thread. E.g. your chain has .subscribeOn(Schedulers.io()).
The only solution that I found was throwing a RuntimeException in my error handler.
That's the expected design of your chain as per this document:
An Observable typically does not throw exceptions. Instead it notifies any observers that an unrecoverable error has occurred by terminating the Observable sequence with an onError notification.
So rather than catch exceptions, your observer or operator should more typically respond to onError notifications of exceptions.
Ok so I think the closest to what I want to achieve is by calling Exceptions.propagate(throwable) in the error handler called in onError.
Thanks for pointing me in the right direction #Gustavo #TooManyEduardos2

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.

how to catch Error globally on android?

When an exception occurs, i try to report it to a log server.
i have caught an exception which inherited java.lang.RuntimeException when using Thread.setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler handler).
but i haven't caught any subclasses that have inherited java.lang.Error. (ex. OutOfMemoryError, IOError, ...)
so i use Runtime.addShutdownHook(Thread hookThread), but the hookThread.run() method is never called.
how do i catch an Error globally on android?
i do not want to use another process.. i just want to try to report a bug on the log server.
sorry evertyone, i try to catch error throwable continuedly, and i know java.lang.Error also is caught. but not run implemented function in UncaughtExceptionHandler.uncaughtException().
just.. printed log for checking.
i solve it. just i was mistake.
i handled Throwable object to obtained log information.
but i used Throwable.getCause() method, when occurred java.long.Error, it is null.
in uncaughtException() method, when occurred exeption, not print log exactly.
thank you, axis.

Categories

Resources