Rathan
Rathan

Reputation: 29

Why does the PDFViewer always gives a NullPointer Exception while opening file using Barteksc PDFViewer

I have a file to load from Assets and display the pdf within app. I have the file "one.pdf" displayed in Assets folder.

My dependencies are:

    compile 'com.github.barteksc:android-pdf-viewer:2.8.1'
    compile "commons-io:commons-io:+"

The code to load the PDF on screen is (DisplayActivity.java):

    pdfView= (PDFView)findViewById(R.id.pdfView);
    setContentView(R.layout.activity_main);
    pdfView.fromAsset("one.pdf").load();

The code to display the PDF (DisplayActivity.xml)

    <com.github.barteksc.pdfviewer.PDFView
    android:id="@+id/pdfView"
    android:layout_below="@+id/tv_header"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

Error log:

FATAL EXCEPTION: main Process: in.edu.reva.revauniversity_pu, PID: 15597
java.lang.RuntimeException: Unable to start activity ComponentInfo{in.edu.reva.revauniversity_pu/in.edu.reva.revauniversity_pu.FormulaActivity}: 
java.lang.NullPointerException: Attempt to invoke virtual method 'com.github.barteksc.pdfviewer.PDFView$Configurator com.github.barteksc.pdfviewer.PDFView.fromAsset(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2720)
at android.app.ActivityThread.-wrap12(ActivityThread.java)                                                                                   
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1466)
at android.os.Handler.dispatchMessage(Handler.java:102)                                                                                   
at android.os.Looper.loop(Looper.java:154)                                                                                   
at android.app.ActivityThread.main(ActivityThread.java:6111)                                                                                   
at java.lang.reflect.Method.invoke(Native Method)                                                                                   
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)  

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.github.barteksc.pdfviewer.PDFView$Configurator com.github.barteksc.pdfviewer.PDFView.fromAsset(java.lang.String)' on a null object reference
at in.edu.reva.revauniversity_pu.FormulaActivity.onCreate(FormulaActivity.java:42)
at android.app.Activity.performCreate(Activity.java:6734)                                                                                   
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2720  
at android.app.ActivityThread.-wrap12(ActivityThread.java)                                                                                    
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1466)
at android.os.Handler.dispatchMessage(Handler.java:102)at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6111)                                                                                    
at java.lang.reflect.Method.invoke(Native Method)                                                                                    
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

The file is present within Assets folder. I have added permissions to access external storage within Manifest. What could be the reason for the below error?

Upvotes: 1

Views: 2014

Answers (1)

Learning Always
Learning Always

Reputation: 1561

Change your dependencies To

compile 'com.github.barteksc:android-pdf-viewer:2.8.1'
compile "commons-io:commons-io:+"

From

compile 'com.github.barteksc:android-pdf-viewer:2.0.3'
compile 'org.apache.commons:commons-collections4:4.1'

then you Xml File Should have

<com.github.barteksc.pdfviewer.PDFView
        android:id="@+id/pdfView"
        android:layout_below="@+id/tv_header"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

And Your Activity Code:

public class YourActivity extends AppCompatActivity implements OnPageChangeListener, OnLoadCompleteListener {
    public static final String SAMPLE_FILE = "one.pdf";

    PDFView pdfView;
    Integer pageNumber = 0;
    String pdfFileName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_your);
        pdfView = (PDFView) findViewById(R.id.pdfView);
        displayFromAsset(SAMPLE_FILE);
    }

    private void displayFromAsset(String assetFileName) {
        pdfFileName = assetFileName;
        pdfView.fromAsset(SAMPLE_FILE).defaultPage(pageNumber).enableSwipe(true)
                .swipeHorizontal(false)
                .onPageChange(this)
                .enableAnnotationRendering(true)
                .onLoad(this)
                .scrollHandle(new DefaultScrollHandle(this))
                .load();
    }


    @Override
    public void onPageChanged(int page, int pageCount) {
        pageNumber = page;
        setTitle(String.format("%s %s / %s", pdfFileName, page + 1, pageCount));
    }


    @Override
    public void loadComplete(int nbPages) {
        PdfDocument.Meta meta = pdfView.getDocumentMeta();
        printBookmarksTree(pdfView.getTableOfContents(), "-");

    }

    public void printBookmarksTree(List<PdfDocument.Bookmark> tree, String sep) {
        for (PdfDocument.Bookmark b : tree) {

            Log.e(TAG, String.format("%s %s, p %d", sep, b.getTitle(), b.getPageIdx()));

            if (b.hasChildren()) {
                printBookmarksTree(b.getChildren(), sep + "-");
            }
        }
    }

}

also Add this Permission

 <uses-permission android:name="android.permission.WRITE_SETTINGS"/>

and MAke sure your minSdkVersion 15 or greater.

it will Work fine .

Upvotes: 1

Related Questions