Java lang reflect invocationtargetexception no error message

Understanding java.lang.reflect.InvocationTargetException and why it occurs

In this post, we will see about java.lang.reflect.InvocationTargetException .

You might get java.lang.reflect.InvocationTargetException while working with reflection in java.

Reason for java.lang.reflect.InvocationTargetException

Reflection layer throws this exception when calling method or constructor throws any exception. java.lang.reflect.InvocationTargetException wraps underlying exception thrown by actual method or constructor call.

Let’s see this with the help of example:
Create a simple class named StringUtils.java . It will have method getLengthOfString() which does not have null handling, so when we pass null to this method, it will throw java.lang.NullPointerException.

Create anther class to call getLengthOfString using reflection.

> catch ( NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e )

java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.arpit.java2blog.ReflectionMain.main(ReflectionMain.java:16)
Caused by: java.lang.NullPointerException
at org.arpit.java2blog.StringUtils.getLengthOfString(StringUtils.java:7)
… 5 more

As you can see, we are getting java.lang.reflect.InvocationTargetException exception because of underlying NullPointerException.

Reflection layer wraps java.lang.reflect.InvocationTargetException around actual Exception to demostrate that this exception was raised during reflection API call.

Handle InvocationTargetException

As underlying exception is actual cause of InvocationTargetException, we can use Throwable’s getCause() method to get actual underlyting exception and use it to log or rethrow the exception.

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Читайте также:  Css 326 24g 2s rm

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Execution failed for task ‘:app:kaptDebugKotlin’. > A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction > java.lang.reflect.InvocationTargetException (no error message) #1327

Execution failed for task ‘:app:kaptDebugKotlin’. > A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction > java.lang.reflect.InvocationTargetException (no error message) #1327

Comments

I was just started learning Retrofit2 with Moshi but having difficulty during compilation/building.

This is the error from Build Output

Execution failed for task ':app:kaptDebugKotlin'. > A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction > java.lang.reflect.InvocationTargetException (no error message) 

This is what my app level gradle file looks like

plugins < id 'com.android.application' id 'kotlin-android' id 'kotlin-kapt' id 'com.google.firebase.crashlytics' >android < compileSdkVersion 30 defaultConfig < applicationId "com.app.app" minSdkVersion 21 targetSdkVersion 30 versionCode 1 versionName "1.0" vectorDrawables.useSupportLibrary = true testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" >buildTypes < debug < debuggable true minifyEnabled true shrinkResources true multiDexEnabled true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' >release < debuggable false minifyEnabled true shrinkResources true multiDexEnabled true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' >> compileOptions < sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 >kotlinOptions < jvmTarget = '1.8' >buildFeatures < viewBinding true >> dependencies < implementation 'androidx.core:core-ktx:1.3.2' implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'com.google.android.material:material:1.3.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1' implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1' implementation 'androidx.navigation:navigation-fragment-ktx:2.3.4' implementation 'androidx.navigation:navigation-ui-ktx:2.3.4' implementation 'androidx.legacy:legacy-support-v4:1.0.0' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.0-M2' //Required: Google Play Services Ads Identifier (If publishing to Google Play) implementation 'com.google.android.gms:play-services-ads-identifier:17.0.0' //Required: Install Referrer (If publishing to Google Play) implementation 'com.android.installreferrer:installreferrer:2.2' implementation 'com.google.firebase:firebase-config:20.0.4' implementation 'com.google.firebase:firebase-firestore:22.1.2' implementation 'com.google.firebase:firebase-analytics:18.0.2' implementation 'com.google.firebase:firebase-messaging:21.0.1' implementation 'com.google.firebase:firebase-crashlytics:17.4.1' implementation 'com.google.firebase:firebase-inappmessaging:19.1.5' implementation 'com.google.firebase:firebase-inappmessaging-display:19.1.5' // FirebaseUI for Firebase Auth implementation 'com.firebaseui:firebase-ui-auth:7.1.1' implementation 'com.google.firebase:firebase-auth:20.0.3' implementation 'com.google.android:flexbox:1.0.0' implementation 'com.github.mahimrocky:TagView:1.0.3' kapt('com.squareup.moshi:moshi-kotlin-codegen:1.11.0') implementation 'com.squareup.moshi:moshi-kotlin:1.11.0' implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0' implementation 'com.squareup.retrofit2:converter-moshi:2.9.0' implementation 'io.reactivex.rxjava2:rxandroid:2.1.1' >apply plugin: 'com.google.gms.google-services' 

This is project level gradle file

// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript < repositories < google() mavenCentral() maven < url 'https://maven.google.com/' name 'Google' >maven < url "https://jitpack.io" >maven < url "https://google.bintray.com/flexbox-layout" >maven < url = "https://dl.bintray.com/kotlin/kotlin-eap" >> dependencies < classpath "com.android.tools.build:gradle:7.0.0-alpha10" classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.0-M2' classpath 'com.google.gms:google-services:4.3.5' // Add the Crashlytics Gradle plugin. classpath 'com.google.firebase:firebase-crashlytics-gradle:2.5.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files >> allprojects < repositories < google() mavenCentral() maven < url 'https://maven.google.com/' name 'Google' >maven < url "https://jitpack.io" >maven < url "https://google.bintray.com/flexbox-layout" >maven < url = "https://dl.bintray.com/kotlin/kotlin-eap" >> > task clean(type: Delete)

The text was updated successfully, but these errors were encountered:

Читайте также:  Python модуль верхнего уровня

Источник

Оцените статью