OutputStream.write()

时间:2018-11-05 06:12:21

标签: java android nullpointerexception itext

尝试使用itext写入pdf格式 晚上一切正常,但现在不起作用。

我已经看了100遍代码了,但是我还是不明白是什么问题。

这是我的代码

public void writetopdf()
{

    flag=1;


    File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/pdf_report");
    if(f.isDirectory())
        Log.d("Main", "writetopdf: " + f.isDirectory());
    else {
        f.mkdir();
        Log.d("Main", "writetopdf: " + f.isDirectory());
    }

    File pdf = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/pdf_report/" + title.getText().toString().trim() + ".pdf");
    OutputStream output = null;
    try {
        output = new FileOutputStream(pdf);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }


    try {
        reader = new PdfReader( getResources().openRawResource(R.raw.hrgroup) );
    } catch (IOException e) {
        e.printStackTrace();
    }


    try {
        stamper = new PdfStamper(reader, output);
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    acroFields = stamper.getAcroFields();

    Set<String> fldNames = acroFields.getFields().keySet();

    for (String fldName : fldNames) {
        Log.d("Main","some = " + fldName );
        fieldnames.add(fldName);
    }


    try {
        setthedata();
    } catch (IOException | DocumentException e) {
        e.printStackTrace();
    }


    stamper.setFormFlattening(true);
    try {
        stamper.close();
    } catch (DocumentException | IOException e) {
        e.printStackTrace();
    }


    Toast.makeText(Objects.requireNonNull(getActivity()).getApplicationContext(),"PDF Generated", Toast.LENGTH_LONG).show();


}

这是Logcat错误

java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.OutputStream.write(byte[], int, int)' on a null object reference
    at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
    at java.io.BufferedOutputStream.write(BufferedOutputStream.java:126)
    at java.io.FilterOutputStream.write(FilterOutputStream.java:97)
    at com.itextpdf.text.pdf.OutputStreamCounter.write(OutputStreamCounter.java:104)
    at com.itextpdf.text.pdf.PdfObject.toPdf(PdfObject.java:175)
    at com.itextpdf.text.pdf.PdfDictionary.toPdf(PdfDictionary.java:148)
    at com.itextpdf.text.pdf.PdfIndirectObject.writeTo(PdfIndirectObject.java:157)
    at com.itextpdf.text.pdf.PdfWriter$PdfBody.write(PdfWriter.java:402)
    at com.itextpdf.text.pdf.PdfWriter$PdfBody.add(PdfWriter.java:380)
    at com.itextpdf.text.pdf.PdfWriter.addToBody(PdfWriter.java:869)
    at com.itextpdf.text.pdf.PdfStamperImp.close(PdfStamperImp.java:433)
    at com.itextpdf.text.pdf.PdfStamperImp.close(PdfStamperImp.java:408)
    at com.itextpdf.text.pdf.PdfStamper.close(PdfStamper.java:230)
    at com.example.aditya.pdf_report.GroupFragment.writetopdf(GroupFragment.java:245)
    at com.example.aditya.pdf_report.GroupFragment.checkfill(GroupFragment.java:289)
    at com.example.aditya.pdf_report.GroupFragment.onClick(GroupFragment.java:412)
    at android.view.View.performClick(View.java:6312)
    at android.view.View$PerformClick.run(View.java:24811)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:171)
    at android.app.ActivityThread.main(ActivityThread.java:6649)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)

在245: 我有stamper.close()

在289:

writetopdf()

我已经尝试了所有方法,删除了所有可能的空指针异常问题,但是仍然可以通过某种方式获取。

请帮助。!

1 个答案:

答案 0 :(得分:2)

我敢打赌发生FileNotFoundException。通过实施的例外情况(不是):

OutputStream output = null;
try {
    output = new FileOutputStream(pdf);
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

为您提供null的{​​{1}}参考!