1

I encounter the following error when I using the AndroidPdfViewer library. and this is my encounter the error:

FATAL EXCEPTION: main
                                                                     Process: app.com.application, PID: 16559
                                                                     java.lang.NullPointerException: Attempt to invoke virtual method 'com.github.barteksc.pdfviewer.PDFView$Configurator com.github.barteksc.pdfviewer.PDFView.fromStream(java.io.InputStream)' on a null object reference
                                                                         at app.com.application.activity.Main2Activity$RetrievePDFStream.onPostExecute(Main2Activity.java:65)
                                                                         at app.com.application.activity.Main2Activity$RetrievePDFStream.onPostExecute(Main2Activity.java:38)

My Code :

public class Main2Activity extends Activity {

String URL_PDF = "http://192.168.1.103/android_login_api/PDF/1G.pdf";
    PDFView pdfView;

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

    // view pdf from url
    new RetrievePDFStream().execute(URL_PDF);

}

 private class RetrievePDFStream extends AsyncTask <String, Void, InputStream>{
     @Override
     protected InputStream doInBackground(String... strings) {
         InputStream inputStream = null;
         URL url = null;
         try {
             url = new URL(strings[0]);
         } catch (MalformedURLException e) {
             e.printStackTrace();
         }

         try {
             HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
             if (httpURLConnection.getResponseCode()==200){

                 inputStream = new BufferedInputStream(httpURLConnection.getInputStream());
             }
         } catch (IOException e) {
             return null;
         }

        return inputStream;
     }


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

}

Please guide me to fix the problem, Thank You. I also had another question: is it possible to use this method Volley instead of AsyncTask?

3
  • You aren't ever setting a value for your field `PDFView pdfView'. Commented Feb 4, 2018 at 11:52
  • Thank You man :) Commented Feb 4, 2018 at 11:57
  • I forgot to remove the comment below pdfView = findViewById(R.id.pdfView); Now I have no problem... @Ibrahim Commented Feb 4, 2018 at 12:01

1 Answer 1

2

It happens because pdfView is null.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh really thank you I've commented on that part of the code

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.