Haj Ali
Haj Ali

Reputation: 93

Null Pointer Exception when using PDF Viewer Lib

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?

Upvotes: 1

Views: 1308

Answers (1)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363469

It happens because pdfView is null.

Upvotes: 2

Related Questions