0

I am using a PDFViewer library to view documents in an Activity which is loaded from a given url.

I am using the open source this library hosted on Git.

Everything works fine in the latest android version Nougat & Oreo. The file loads up and is shown in the screen just easy.

However, when testing with the older versions like Marshmallow & Kitkat, the document isn't loaded and I receive the following error:

Attempt to invoke virtual method 'int java.io.InputStream.read(byte[])' on a null object reference

My full Log report is:

E/PDFView: load pdf error
           java.lang.NullPointerException: Attempt to invoke virtual method 'int java.io.InputStream.read(byte[])' on a null object reference
               at com.github.barteksc.pdfviewer.util.Util.toByteArray(Util.java:36)
               at com.github.barteksc.pdfviewer.source.InputStreamSource.createDocument(InputStreamSource.java:37)
               at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:53)
               at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:25)
               at android.os.AsyncTask$2.call(AsyncTask.java:295)
               at java.util.concurrent.FutureTask.run(FutureTask.java:237)
               at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
               at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
               at java.lang.Thread.run(Thread.java:818)
E/Surface: getSlotFromBufferLocked: unknown buffer: 0xa2f736e0
D/Link:: http://mylink.com/FileUpload/Master/PDFModule/636586353416035750_सेफ हाउस संचालन सम्बन्धी सूचना.pdf
E/PDFView: load pdf error
           java.lang.NullPointerException: Attempt to invoke virtual method 'int java.io.InputStream.read(byte[])' on a null object reference
               at com.github.barteksc.pdfviewer.util.Util.toByteArray(Util.java:36)
               at com.github.barteksc.pdfviewer.source.InputStreamSource.createDocument(InputStreamSource.java:37)
               at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:53)
               at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:25)
               at android.os.AsyncTask$2.call(AsyncTask.java:295)
               at java.util.concurrent.FutureTask.run(FutureTask.java:237)
               at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
               at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
               at java.lang.Thread.run(Thread.java:818)

My PDFclass code is as follows:

public void getPdfFile() {
        pdf_link = this.getIntent().getExtras().getString("PDF_URL");
        checkStrUrl = pdf_link.substring(pdf_link.lastIndexOf("_") + 1);
        if (!checkFileInDevice(checkStrUrl)) {
            new RetrievePDFStream().execute(pdf_link);
        } else {
            File file = new File(Environment.getExternalStorageDirectory().toString() + "/Application Documents/" + checkStrUrl);
            pdfView.fromFile(file).load();
        }

    }

    class RetrievePDFStream extends AsyncTask<String, Void, InputStream> {

        @Override
        protected InputStream doInBackground(String... strings) {
            InputStream inputStream = null;
            try {
                URL url = new URL(strings[0]);
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                if (urlConnection.getResponseCode() == 200) {
                    inputStream = new BufferedInputStream(urlConnection.getInputStream());

                }
            } catch (IOException e) {
                return null;
            }
            return inputStream;
        }

        @Override
        protected void onPostExecute(InputStream inputStream) {
            pdfView.fromStream(inputStream).load();
        }

    }
23
  • It's a simple pdf file document. Commented Apr 10, 2018 at 12:34
  • whats the value of pdf_link? just use Log.d to print it out Commented Apr 10, 2018 at 12:35
  • Where exactly do you get the error? Commented Apr 10, 2018 at 12:37
  • @pskink I get the following Log detail: D/Link:: mylink.com/FileUpload/Master/PDFModule/636586353416035750_सेफ हाउस संचालन सम्बन्धी सूचना.pdf Commented Apr 10, 2018 at 12:39
  • @PeterBruins I get null value in my inputStream when it is returned from doInBackground() Commented Apr 10, 2018 at 12:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.