i am working on parsing .csv files using OpenCsv for my android application . I writing code in kotlin . Here is my code :
This is how i parse the csv file:
val beans: List<csvdataclass> = CsvToBeanBuilder<csvdataclass>(FileReader(file))
.withType(csvDataClass::class.java).build().parse()</csvdataclass></csvdataclass>
for (i:Int in 0..beans.size-1) {
Log.d("Tag", " NAME = ${beans.get(i).name} Number = ${beans.get(i).phone_one_value}")
}
This is the object class to which i want to convert the csv data.
class csvDataClass {
@CsvBindByName (column = "Name")
var name:String = ""
@CsvBindByName (column = "Phone 1 - Value")
var phone_one_value:Int = 0
constructor() {}
constructor(name: String, phone_one_value: Int) {
this.name = name
this.phone_one_value = phone_one_value
}
}
I have added this line in build.gradle file:
implementation 'com.opencsv:opencsv:4.1'
Here is my csv file content:
Name,Phone 1 - Value
G T,11111 11111 H K,22222 22222 K K,33333 33333 P K,44444 44444
But when i run the code i am getting :
05-04 16:13:31.821 25829-25829/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.pk.opencsvpoc, PID: 25829
java.lang.NoClassDefFoundError: Failed resolution of: Ljava/beans/Introspector;
at com.opencsv.bean.HeaderColumnNameMappingStrategy.loadDescriptors(HeaderColumnNameMappingStrategy.java:372)
at com.opencsv.bean.HeaderColumnNameMappingStrategy.loadDescriptorMap(HeaderColumnNameMappingStrategy.java:284)
at com.opencsv.bean.HeaderColumnNameMappingStrategy.setType(HeaderColumnNameMappingStrategy.java:415)
at com.opencsv.bean.opencsvUtils.determineMappingStrategy(opencsvUtils.java:83)
at com.opencsv.bean.CsvToBeanBuilder.build(CsvToBeanBuilder.java:156)
at com.example.pardeepkumar.opencsvpoc.MainActivity.readCsvFile(MainActivity.kt:46)
at com.example.pardeepkumar.opencsvpoc.MainActivity.onCreate(MainActivity.kt:38)
at android.app.Activity.performCreate(Activity.java:6726)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2619)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
Caused by: java.lang.ClassNotFoundException: Didn't find class "java.beans.Introspector" on path: DexPathList[[zip file "/data/app/com.example.pk.opencsvpoc-1/base.apk", zip file "/data/app/com.example.pk.opencsvpoc-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.pk.opencsvpoc-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.pk.opencsvpoc-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.pk.opencsvpoc-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.pk.opencsvpoc-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.pk.opencsvpoc-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.pk.opencsvpoc-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.pk.opencsvpoc-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.pk.opencsvpoc-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.pk.opencsvpoc-1/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.pk.opencsvpoc-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.pk.opencsvpoc-1/lib/arm64, /system/lib64, /vendor/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at com.opencsv.bean.HeaderColumnNameMappingStrategy.loadDescriptors(HeaderColumnNameMappingStrategy.java:372)
at com.opencsv.bean.HeaderColumnNameMappingStrategy.loadDescriptorMap(HeaderColumnNameMappingStrategy.java:284)
at com.opencsv.bean.HeaderColumnNameMappingStrategy.setType(HeaderColumnNameMappingStrategy.java:415)
at com.opencsv.bean.opencsvUtils.determineMappingStrategy(opencsvUtils.java:83)
at com.opencsv.bean.CsvToBeanBuilder.build(CsvToBeanBuilder.java:156)
at com.example.pardeepkumar.opencsvpoc.MainActivity.readCsvFile(MainActivity.kt:46)
at com.example.pardeepkumar.opencsvpoc.MainActivity.onCreate(MainActivity.kt:38)
at android.app.Activity.performCreate(Activity.java:6726)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2619)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
java.beans.Introspector is part of the JDK, so this is not an opencsv problem. I don't know anything about your setup, but perhaps you're running into problems because as of Java 9, Introspector is in the package java.desktop.
Try running on a Apple/Windows/Linux JDK on a desktop Or a newer Android JDK to see if you have the same issues. I did some googling around and found several old articles (2010-2016) that showed that Android version of Java was a subset and Introspector was one of the missing items.
https://github.com/FasterXML/jackson-jr/issues/11
Closed as not relevant to opencsv.
Hello,
I think this is actually a documentation issue. I stumbled onto the same problem and it took me about one day of browsing the internet to find a solution. Maybe you should add an "Android warning" under the section "Reading into beans" stating that Android does not support this due to a limitation of their port of the standard Java libraries.
Best regards,
mathieu
Hello Mathieu - I updated the FAQ in our Wiki page to note this.