Android Question Error Loading CSV File in Tableview

junaidahmed

Well-Known Member
Licensed User
Longtime User
I have an csv file ,I am trying to Load this csv File in Table, but its shows an error message.Please check attached file and below error log and advise how to solve the problem....


Logger connected to: LENOVO Lenovo A2010-a
--------- beginning of system
--------- beginning of main
Copying updated assets files (9)
** Activity (main) Create, isFirst = true **
Error occurred on line: 859 (Table)
java.lang.StringIndexOutOfBoundsException: length=154; regionStart=150; regionLength=-151
at java.lang.String.startEndAndLength(String.java:504)
at java.lang.String.substring(String.java:1333)
at anywheresoftware.b4a.objects.StringUtils.ReadWord(StringUtils.java:276)
at anywheresoftware.b4a.objects.StringUtils.LoadCSV2(StringUtils.java:243)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:259)
at b4a.tableclass144.main._filltablecsv(main.java:500)
at b4a.tableclass144.main._activity_create(main.java:492)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at b4a.tableclass144.main.afterFirstLayout(main.java:104)
at b4a.tableclass144.main.access$000(main.java:17)
at b4a.tableclass144.main$WaitForLayout.run(main.java:82)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5624)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
 

Attachments

  • grid.zip
    277 bytes · Views: 181

klaus

Expert
Licensed User
Longtime User
In your file there is a LF (line feed) character at the end of the file!
The LoadCSV method from the StringUtils library doesn't like it.
It admits that there is a empty line at the end.
Removing this character, like in the joined file, works as expected.
 

Attachments

  • grid1.zip
    278 bytes · Views: 220
Upvote 0

klaus

Expert
Licensed User
Longtime User
B4X:
Private txt As String
txt = File.ReadString(File.DirAssets, "grid.csv")
If txt.CharAt(txt.Length - 1) = Chr(10) Then
    txt = txt.SubString2(0, txt.Length - 2)
    File.WriteString(File.DirInternal, "grid.csv", txt)
End If
Be aware that you cannot save the text back to File.DirAssets, it's read only!
How did you generate this csv file, or where does it come from?
 
Upvote 0

junaidahmed

Well-Known Member
Licensed User
Longtime User
Actually I am generating csv file from below text using "TextWriter".Please advise how to do this...

Stage;YProd;TProd
Applique;1055;1790
Bundling;5387;5543
Cutting;3767;4191
Cutting QC;1392;1686
Final QA;2036;2158
Finger Tacking;2118;2980
Finishing;2237;2218
Hemming;1911;2497
Insert Lining;2252;2779
Lining QC;2024;2666
Marking;2433;2244
PreLaying QC;2992;2610
PreSewing;2530;2611
PreSewing QC;3441;2926
Quality Control;0;0
Sewing;2362;2235
Slitting;2164;2376
Spraying;0;0
Shipped;0;0
B4X:
Private tx As TextWriter
tx.Initialize2(File.OpenOutput(File.DirRootExternal, "Grid.csv", False), "Windows-1252")
tx.WriteLine(incomex)
tx.Close
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
In this case, you don't need the code above.
Just remove the line feed at the end of the last line.
That's what I did with your file in Notepad++.

upload_2018-3-26_9-1-33.png

With line feed.

upload_2018-3-26_9-1-49.png

Without line feed.
 
Upvote 0

junaidahmed

Well-Known Member
Licensed User
Longtime User
Thanks for reply...

Actually above text is dynamically,I am generating this text in "ASP.net",after getting the response from Server.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Sorry, I misread your post.

You could try this:
B4X:
If incomex.CharAt(txt.Length - 1) = Chr(10) Then
    incomex = incomex.SubString2(0, txt.Length - 2)
    Private tx As TextWriter
    tx.Initialize2(File.OpenOutput(File.DirRootExternal, "Grid.csv", False), "Windows-1252")
    tx.WriteLine(incomex)
    tx.Close
End If
Do ou really need "Windows-1252" encoding?
If you don't need to read the file back to any Windows program use simply:
tx.Initialize(File.OpenOutput(File.DirRootExternal, "Grid.csv", False))

I'm not sure if File.DirRootExternal will work in the future with the newer Android versions.
You should have a look at this Runtime Permissions (Android 6.0+ Permissions) or File.DirRootExternal and RuntimePermissions.
 
Upvote 0
Top