ASPOSE WORD API null pointer exception

Hi ,

I am currently running code as shown below on separate thread2 :

    try {

            Log.d(TAG, "Atcual OrignalDocument : " + orignalDocument);

            totalPages = orignalDocument.getPageCount();
           
            Log.d(TAG, "Actual Page " + totalPages);
          
        } catch (Exception e) {
      
            Log.d(TAG, "Error Actual " + e.getMessage());

        } 

orignalDocument = new Document(file input stream) this initialisation of Document object is done on separate thread1 then actual page is find with the help of thread2 as above shown .

I am getting error at some time in catch block and error is shown below :

Attempt to read from field ‘int com.aspose.words.zzVW.zzZOI’ on a null object reference

Kindly provide solution for given problem .

Regards
Mahesh Anand
Data Resolve Technology
9990407589

@rajnishkumar

Thanks for your inquiry. I am afraid you can’t access a document from multiple threads. Please note Aspose APIs are multithread safe as long as only one thread works on a document at a time. It is a typical scenario to have one thread working on one document. Different threads can safely work on different documents at the same time.

means we cannot call function on same document object from different thread at same time…?

like initialise the document with inputstream from one thread1 and
getting the pagecount for this document object from thread2 …?

@rajnishkumar

Yes, your understanding is correct. You can’t access a document from two threads using Aspose.Words for Java.

Here I am sharing some code and test files for implementation of multi-threading done at my end :

    new Thread(new Runnable() {
        @Override
        public void run() {

            InputStream fileInputStream = null;
            try {
                long startTime = System.currentTimeMillis();

                final String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "testdocs";

                fileInputStream = new FileInputStream(new File(path, "page343.doc"));

                Document document2 = new Document();
                document2.updatePageLayout();

                long endTime = System.currentTimeMillis();

                Log.d(TAG, "UpdatePageLayout: " + ((endTime - startTime) / 1000) + "ms \n");

                startTime = System.currentTimeMillis();

                document = new Document(fileInputStream);

                endTime = System.currentTimeMillis();

                Log.d(TAG, "Document Load Time: " + ((endTime - startTime) / 1000) + "ms \n");

                ImageSaveOptions options = new ImageSaveOptions(SaveFormat.JPEG);
                options.setPageCount(1);
                
                //Thread Second for finding total page in File on Phone 
                new Thread(new Runnable() {
                    @Override
                    public void run() {

                        try {

                            long starttime = System.currentTimeMillis();

                            int pagecount = document.getPageCount();

                            long endtime = System.currentTimeMillis();

                            Log.d(TAG, "Total Page No. 1: " + pagecount + " " + ((endtime - starttime) / 1000) + "ms \n");

                        } catch (Exception e) {
                            Log.d(TAG, "Actual Page Error : " + e.getMessage());
                        }

                    }
                }).start();


                startTime = System.currentTimeMillis();

                int pageCountDoc = document.getBuiltInDocumentProperties().getPages();

                endTime = System.currentTimeMillis();

                Log.d(TAG, "Total Page No. 2: " + pageCountDoc + " " + ((endTime - startTime) / 1000) + "ms \n");

                startTime = System.currentTimeMillis();

                for (int pageCount = 0; pageCount < pageCountDoc; pageCount++) {

                    options.setPageIndex(pageCount);

                    ByteArrayOutputStream baos = new ByteArrayOutputStream();

                    document.save(baos, options);

                    endTime = System.currentTimeMillis();

                    Log.d(TAG, "ByteArray " + pageCount + ": " + ((endTime - startTime) / 1000) + "ms \n");

                    long st = System.currentTimeMillis();

                    //saving image in bitmap using byte array output stream 
                    savebitmap(baos, pageCount);

                    long et = System.currentTimeMillis();

                    Log.d(TAG, "Image Save: " + pageCount + "/" + ((et - st) / 1000) + "ms \n");

                    startTime = System.currentTimeMillis();
                }


            } catch (Exception e) {

                Log.d(TAG, "run 1 Error: " + e.getMessage());

            }

        }
    }).start();

As you can see in above code i have separate threads for document initialisation and for finding actual page of this document .

As per your reply we cannot access document object from two threads at same time but i am able to get worked above code for some documents and getting error in some others .

Like

  • page3.doc : the error get in catch block of total page thread
  • page343.doc : the calling of total page on different thread does not interrupt as error in catch block …why ?

test.zip (184.7 KB)

Kindly explain why i am able to access same object from different threads in page343.doc case .

Regards
Mahesh Aanand
9990407589

@rajnishkumar

Thanks for your feedback. You are getting expected results. As already stated that Aspose.Words is multithread safe as long as only one thread works on a document at a time. Otherwise if we manipulate a single document in different threads, then results would be unstable.