Java Question Error parsing libraries - Object reference not set to an instance of an object.

Discussion in 'Libraries developers questions' started by qle, Aug 20, 2015.

  1. qle

    qle Member Licensed User

    Hi,
    I think trying to create a library from this SDK may have me beat.. i just dont have the skill level.

    In my library java code i added a class within my class,
    Code:
    private Barcode1D mInstance;[/INDENT]
    public class c4000{
        
    private boolean isOpened = false;
        
    public int DeviceControlinit () throws IOException
        {
     
            
    try {
                //get 1D instance
                mInstance = Barcode1D.getInstance();
               //This actually now works thanks 
    to DonManFred (well, i dont get an error)
                
    return 1;
            
    } catch (Exception e) {
                e.printStackTrace();

                return 0;
            }
        }


        public int openscanner () throws IOException
        {
                new InitTask().execute();
            return 1;
        }

        public class InitTask extends AsyncTask<String, Integer, Boolean> {

            @Override
            protected Boolean doInBackground(String... params) {
                // TODO Auto-generated method stub
                return mInstance.open();
            }

            @Override
            protected void onPostExecute(Boolean result) {
                super.onPostExecute(result);
                isOpened = result;

            }
        }

        etc.......
    }
    It is when i added the class,
    public class InitTask extends AsyncTask<String, Integer, Boolean> {
    now causes a problem when i try to load the library in A4B, i get
    [​IMG]

    Any help is very appreciated.

    Lee.
    in above my head...​
     
    Last edited: Aug 20, 2015
  2. Erel

    Erel Administrator Staff Member Licensed User

    Is this class needs to be exposed to B4A? My guess that you can hide it with @Hide annotation.
     
  3. qle

    qle Member Licensed User

    Adding the @Hide caused the SLC to throw an error on compile.
    Code:
    public int openscanner () throws IOException
        {
            new InitTask().execute();
            
    return 1;
        
    }

        @Hide
        public class InitTask extends AsyncTask<String, Integer, Boolean> {

            @Override
            protected Boolean doInBackground(String... params) {
                // TODO Auto-generated method stub
                return mInstance.open();
            }

            @Override
            protected void onPostExecute(Boolean result) {
                super.onPostExecute(result);
                isOpened = result;

            }
        }

    SLC reports
    Code:
    Starting step: Compiling Java code.
    javac 
    1.7.0_51
    C:\Users\Lee\workspace\Serial_c4000\src\com\android\barcode1d\c4000.java:
    77: error: cannot find symbol
        @Hide
         ^
      symbol:   class Hide
      
    location: class c4000
    1 error

    So.. i removed the hide and changed the public class InitTask to private class InitTask
    This then compiled ok, and B4A loaded the library without error.

    I could even call the .openscanner that calls the private InitTask, and it returns a 1 to B4A ok..

    But then, it throws an error
    Code:
    DeviceControlinit1
    Open1
    java.lang.RuntimeException: An error occured 
    while executing doInBackground()
        at android.os.AsyncTask$
    3.done(AsyncTask.java:278)
        at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:
    273)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:
    124)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:
    307)
        at java.util.concurrent.FutureTask.run(FutureTask.java:
    137)
        at android.os.AsyncTask$SerialExecutor$
    1.run(AsyncTask.java:208)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:
    1076)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:
    569)
        at java.lang.Thread.run(
    Thread.java:856)
    Caused by: java.lang.ExceptionInInitializerError
        at com.rscja.deviceapi.Barcode1D.open(SourceFile:
    52)
        at com.android.barcode1d.c4000$InitTask.doInBackground(c4000.java:
    83)
        at com.android.barcode1d.c4000$InitTask.doInBackground(c4000.java:
    78)
        at android.os.AsyncTask$
    2.call(AsyncTask.java:264)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:
    305)
        ... 
    5 more
    Caused by: java.lang.UnsatisfiedLinkError: Couldn
    't load DeviceAPI: findLibrary returned null
        at java.lang.Runtime.loadLibrary(Runtime.java:365)
        at java.lang.System.loadLibrary(System.java:
    535)
        at com.rscja.deviceapi.DeviceAPI.<clinit>(SourceFile:
    526)
        ... 
    10 more
    I seem to be back to this java.lang.UnsatisfiedLinkError: Couldn't load DeviceAPI:

    yet the DeviceControlinit worked! (mInstance = Barcode1D.getInstance();) and this uses the DeviceAPI


    Lee.

    EDIT: Looking through the logs i see while loading the libDeviceAPI.so library that it sys "skipping init"

    Code:
    Trying to load lib /data/data/pos.c4000/files/libDeviceAPI.so 0x417ca960
    Added shared lib /data/data/pos.c4000/files/libDeviceAPI.so 
    0x417ca960
    No JNI_OnLoad found 
    in /data/data/pos.c4000/files/libDeviceAPI.so 0x417ca960, skipping init
    ** 
    Activity (main) Create, isFirst = true **
    is this a clue?
    does skipping init mean it failed to load?
    and what is No JNI do i need it?

    Lee.
     
    Last edited: Aug 21, 2015
  4. DonManfred

    DonManfred Expert Licensed User

    you need to add the correct import to your imports
    no. The library does not automatically init itself when loading. No problem if the lib has other methods to get initialized
    JNI is the "JAVA Native Interface".
     
  5. qle

    qle Member Licensed User

    Thank you..


    May i ask, What is the correct import for @Hide?
     
  6. DonManfred

    DonManfred Expert Licensed User

    Code:
    import anywheresoftware.b4a.BA.Hide;
     
  7. qle

    qle Member Licensed User

    Thank you

    edit: This allowed it to complile as a public class,
    Code:
    @Hide
        
    public class InitTask extends AsyncTask<String, Integer, Boolean> {

    But back to same issue.
    Caused by: java.lang.UnsatisfiedLinkError: Couldn't load DeviceAPI: findLibrary returned null


    Lee.
     
    Last edited: Aug 21, 2015
  8. Erel

    Erel Administrator Staff Member Licensed User

    How are you adding the .so file to the jar?

    Where is the code that loads it?
     
  9. qle

    qle Member Licensed User

    Im trying both Eclipse and SLC. (both give the same results)

    In eclipse the .so file is in
    /lib/armeabi/libDeviceAPI.so
    and the DeviceAPI.jar is in
    /lib/DeviceAPI.jar
    (in eclipse i have added the /lib/DeviceAPI.jar as a referenced library)

    With SLC the .so file is in
    /lib/armeabi/libDeviceAPI.so
    and
    /additional/lib/libDeviceAPI.so
    and the DeviceAPI.jar is in
    /lib/DeviceAPI.jar
    and
    /additional/lib/DeviceAPI.jar


    I load the libDeviceAPI.so from the java library with
    Code:
    static {
        //System.loadLibrary(
    "serial_port");
        
    try {
           
    if (File.Exists(File.getDirInternal(), "libDeviceAPI.so") == true){
               //
    Log.i(TAG,"About to load: libDeviceAPI.so");
               System.load(
    File.Combine(File.getDirInternal(), "libDeviceAPI.so"));
               //
    Log.i(TAG,"Afterload: libDeviceAPI.so"); 
           
    }else{.........
    edit: i also added the libDeviceAPI.so and DeviceAPI.jar into the files of the A4B project.
    And the DeviceAPI.jar to the <program files>\Anywhere Software\Basic4android\Libraries

    Lee.
     
    Last edited: Aug 21, 2015
  10. Erel

    Erel Administrator Staff Member Licensed User

    Correct file structure:
    [​IMG]

    You need to load it with:
    System.loadLibrary("DeviceAPI"); //assuming that the file name is libDeviceAPI.so
     
  11. qle

    qle Member Licensed User

    In the Java library i put
    Code:
    static {
            
    try {
                   
    if (File.Exists(File.getDirInternal(), "libDeviceAPI.so") == true){
                         
    Log.i(TAG,"About to load: DeviceAPI");
                         System.loadLibrary(
    "DeviceAPI");   //throws UnsatisfiedLinkError
                         //System.load(
    File.Combine(File.getDirInternal(), "libDeviceAPI.so")); // this loads ok, no errors loading (but get UnsatisfiedLinkError in InitTask: doInBackground)
    ........
    throws error:
    Caused by: java.lang.UnsatisfiedLinkError: Couldn't load DeviceAPI: findLibrary returned null


    In SLC the dir structure for libs is
    [​IMG]
    and in armeabi
    [​IMG]


    Lee.
     
    Last edited: Aug 24, 2015
  12. Erel

    Erel Administrator Staff Member Licensed User

    1. The folder structure is incorrect. See the screenshot I posted. libs <> lib

    2. Not sure what is the purpose of the if File.Exists check. It has nothing to do with the actual file.
     
  13. qle

    qle Member Licensed User

    Oh my,
    i missed that... i assumed because in the eclipse it was /libs that in the /additional would be also /libs (i copy and paste the folder!)... and i did not even notice... unbelievable!

    This got me to the next step... thank you!


    The file.exists is from at older project where i had to System.load the .so from the File.getDirInternal() to get it to work, if it was not there then it would copy it from File.getDirAssets(). (this actually worked!) i had assumed i may have to do the same with this .so...

    Im not great at java... but im learning..

    Thank you for your help.

    edit: WhooHoo!.. it all works... SLC is indeed a very good utility for building libraries from 3rd party SDK's. (i still cant believe i missed the libs<>lib bit...)
     
    Last edited: Aug 25, 2015
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice