0

I am making application in Android Studio which use to load PDF file from firebase database. I tested my code and it can load pdf file normally. when I change network and my app fail to load pdf file. here is error which I found in Logcat:

2022-10-08 10:58:12.733 22071-22071/com.tonyapp.hoanguhsk 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)

below is my code to load pdf file:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_a18);

    read = findViewById(R.id.read);
    dialog = new ProgressDialog(this);
    dialog.setMessage("Loading..");
    dialog.show();
    urls = getIntent().getStringExtra("dt18");
    new RetrivePdfStream().execute(urls);



}

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

    @Override
    protected InputStream doInBackground(String... strings) {
        InputStream inputStream = null;
        try {

            // adding url
            URL url = new URL(strings[0]);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            // if url connection response code is 200 means ok the execute
            if (urlConnection.getResponseCode() == 200) {
                inputStream = new BufferedInputStream(urlConnection.getInputStream());
            }
        }
        // if error return null
        catch (IOException e) {
            return null;
        }
        return inputStream;
    }

    @Override
    // Here load the pdf and dismiss the dialog box
    protected void onPostExecute(InputStream inputStream) {
        read.fromStream(inputStream).load();
        dialog.dismiss();
    }

}

Can any experts please help me how to solve the error? It make me headache many days. Thanks

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
jackie Maik
  • 77
  • 1
  • 3

0 Answers0