Java Question java.lang.RuntimeException: Method: parsePdf not found

MarcoRome

Expert
Licensed User
Longtime User
Hi all,
i have this library ( Library Compilation B4A )
It is compiled without any problems.
When i try the same then i get this error:

B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
AssetsDir
/storage/emulated/0
main_activity_create (java line: 344)
java.lang.RuntimeException: Method: parsePdf not found in: b4a.example.main
    at anywheresoftware.b4j.object.JavaObject$MethodCache.getMethod(JavaObject.java:366)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:119)
    at library.pdftotext.pdftotext._converti(pdftotext.java:62)
    at b4a.example.main._activity_create(main.java:344)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
    at b4a.example.main.afterFirstLayout(main.java:102)
    at b4a.example.main.access$000(main.java:17)
    at b4a.example.main$WaitForLayout.run(main.java:80)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6692)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

If i open the jar library i see this method ( parsePdf), and if move the code from class to Main activity, this work without problem. So where i wrong ??

upload_2017-7-10_12-37-4.png



The code Class is:
B4X:
#Event: OnMessage
'#Event: OnSecond
Private Sub Class_Globals
    Private nativeMe As JavaObject
    Private nome_evento As String
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(evento As String)
    nome_evento = evento
End Sub

Public Sub Converti(namefilepdf As String, namefiletxt As String)
    Log(File.DirAssets)
    Log(File.DirRootExternal)
    nativeMe.InitializeContext
    Dim filepdf As String = namefilepdf
    Dim filetxt As String = namefiletxt
    nativeMe.RunMethod("parsePdf", Array(filepdf, filetxt, nome_evento))
End Sub



#If Java

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy;
import com.itextpdf.text.pdf.parser.TextExtractionStrategy;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.*;



private BA ba;


    public void parsePdf(String filepdf, String filetxt, String nomeevento) throws IOException {
            String pdf = filepdf;
            String txt = filetxt;
            String EventName = nomeevento;
            PdfReader reader = new PdfReader(pdf);
            PdfReaderContentParser parser = new PdfReaderContentParser(reader);
            PrintWriter out = new PrintWriter(new FileOutputStream(txt));
            TextExtractionStrategy strategy;
            for (int i = 1; i <= reader.getNumberOfPages(); i++) {
                strategy = parser.processContent(i, new SimpleTextExtractionStrategy());
                out.println(strategy.getResultantText());
            }
            reader.close();
            out.flush();
            out.close();
            String Convertito = "Done";
            try {
            BA ba = (BA) this.getClass().getField("processBA").get(null);
            if(ba.subExists(EventName.toLowerCase(BA.cul) + "_onmessage")){
                BA.Log("Found");
                ba.raiseEventFromUI(this, EventName.toLowerCase(BA.cul) + "_onmessage", Convertito);
            } else {
                BA.Log("Not Found");
            }
            } catch (Exception e) {
throw new RuntimeException(e);
}
    }

#End If

The Code Activity that recall this class is:

B4X:
#Region Module Attributes
    #CanInstallToExternalStorage: False
    #LibraryAuthor: Devil-App
    #LibraryName: PdfToTxtB4
    #LibraryVersion: 1.00
#End Region

#AdditionalJar: itextpdf-5.5.6




Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    'Dim nativeMe As JavaObject

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Dim filepdf As String = File.DirRootExternal &"/test_armen.pdf"
    Dim filetxt As String = File.DirRootExternal &"/test.txt"
    Dim pdf As pdftotext
    pdf.Initialize("pdf")
    pdf.Converti(filepdf, filetxt)


End Sub

Sub pdf_OnMessage
    Log("OK")
End Sub



Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

The JAR file is itextpdf-5-5-6.jar ( click on for download )
In attachment Project.
Thank you
 

Attachments

  • PdtToTxt2.zip
    10.2 KB · Views: 392

JordiCP

Expert
Licensed User
Longtime User
Try to change a line in 'Converti'. It worked for me, not in a library but in a similar situation
B4X:
Public Sub Converti(namefilepdf AsString, namefiletxt AsString)
   Log(File.DirAssets)Log(File.DirRootExternal)

   'nativeMe.InitializeContext    '<--- This will call your inline Java in main
   nativeMe=Me                     '<--- This one will call it in your class

   Dim filepdf AsString = namefilepdf
   Dim filetxt AsString = namefiletxt
   nativeMe.RunMethod("parsePdf", Array(filepdf, filetxt, nome_evento))
End Sub
 

MarcoRome

Expert
Licensed User
Longtime User
Yes Jordi you are right.
Now work but i have again a problem:
B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
AssetsDir
/storage/emulated/0
main_activity_create (java line: 344)
java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:131)
    at library.pdftotext.pdftotext._converti(pdftotext.java:62)
    at b4a.example.main._activity_create(main.java:344)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
    at b4a.example.main.afterFirstLayout(main.java:102)
    at b4a.example.main.access$000(main.java:17)
    at b4a.example.main$WaitForLayout.run(main.java:80)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6692)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
Caused by: java.lang.RuntimeException: java.lang.NoSuchFieldException: processBA
    at library.pdftotext.pdftotext.parsePdf(pdftotext.java:111)
    ... 16 more
Caused by: java.lang.NoSuchFieldException: processBA
    at java.lang.Class.getField(Class.java:1549)
    at library.pdftotext.pdftotext.parsePdf(pdftotext.java:103)
    ... 16 more

for sure depend on this code ( in class ):

B4X:
.....
String Convertito = "Done";
            try {
            BA ba = (BA) this.getClass().getField("processBA").get(null);
            if(ba.subExists(EventName.toLowerCase(BA.cul) + "_onmessage")){
                BA.Log("Found");
                ba.raiseEventFromUI(this, EventName.toLowerCase(BA.cul) + "_onmessage", Convertito);
            } else {
                BA.Log("Not Found");
            }
            } catch (Exception e) {
throw new RuntimeException(e);
}


In attachment the project
Any suggestion ?
Thank you
 

Attachments

  • PdtToTxt2Rel2.zip
    10.2 KB · Views: 444

JordiCP

Expert
Licensed User
Longtime User
Not at home now, so I can't test. But I would try:

retrieve processBA not from this.getClass() but from its parent

perhaps this.ba is accessible, can't try it now
 

MarcoRome

Expert
Licensed User
Longtime User
I resolved in this mode:

B4X:
....
BA.Log("Name Event: " + eventName + "_onmessage");                              
            try {
                if(ba.subExists(eventName + "_onmessage")){
                    BA.Log("Found");
                    ba.raiseEventFromUI(this, eventName.toLowerCase(BA.cul) + "_onmessage", Convertito);
                } else {
                    BA.Log("Not Found");
                }
            } catch (Exception e) {
throw new RuntimeException(e);
}

without this line:
B4X:
BA ba = (BA) this.getClass().getField("processBA").get(null);
 
Top