为什么我得到java.lang.NullPointerException?

时间:2014-08-12 04:19:00

标签: java android nullpointerexception

我正在尝试测试这个脚本,使用Android将一些演示文本打印到我的Zebra打印机,但是我得到了一个 java.lang.NullPointerException 。我已阅读有关初始化的内容,但我无法确定要初始化的对象是什么。

这是错误:

E/AndroidRuntime(2098): 
FATAL EXCEPTION: Thread-9874
Process: com.stihn.sibmovil, PID: 2098
java.lang.NullPointerException
at com.stihn.sibmovil.sendfile.SibPrint.testSendFile(SibPrint.java:88)
at com.stihn.sibmovil.sendfile.SibPrint.access$0(SibPrint.java:86)
at com.stihn.sibmovil.sendfile.SibPrint$1.run(SibPrint.java:53)
at java.lang.Thread.run(Thread.java:841)

感谢您的帮助!

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.os.Looper;

import com.stihn.sibmovil.util.SettingsHelper;
import com.zebra.sdk.comm.BluetoothConnection;
import com.zebra.sdk.comm.Connection;
import com.zebra.sdk.comm.ConnectionException;
import com.zebra.sdk.printer.ZebraPrinter;
import com.zebra.sdk.printer.ZebraPrinterFactory;
import com.zebra.sdk.printer.ZebraPrinterLanguageUnknownException;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import com.zebra.sdk.printer.PrinterLanguage;
import org.json.JSONArray;
import org.json.JSONException;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;

public class SibPrint extends Plugin {

    ProgressDialog loadingDialog;
    Activity activity;
    private Context context;
    String macAddress;
    String filepath;

    public PluginResult execute(String action, JSONArray args, String callbackId) {
        try {
            if (action.equals("sibPrintTicket")) {
                String echo = args.getString(0);
                filepath = args.getString(1);
                macAddress = args.getString(2);
                if (echo != null && echo.length() > 0) {

                    new Thread(new Runnable() {
                        public void run() {
                            Looper.prepare();

                            Connection connection = null;
                            connection = new BluetoothConnection(macAddress);

                            try {
                                System.out.println("Imprimiendo ...");
                                connection.open();
                                ZebraPrinter printer = ZebraPrinterFactory
                                        .getInstance(connection);
                                testSendFile(printer);

                                connection.close();
                            } catch (ConnectionException e) {
                                System.out.println("Error Try 1 "
                                        + e.getMessage());
                            } catch (ZebraPrinterLanguageUnknownException e) {
                                System.out.println("Error Catch 1 "
                                        + e.getMessage());
                            } finally {
                                if (activity != null && loadingDialog != null) {
                                    loadingDialog.dismiss();
                                }
                            }
                            Looper.loop();
                            Looper.myLooper().quit();
                        }

                    }).start();

                    //retornar mensaje de imprimiendo
                    return new PluginResult(PluginResult.Status.OK, "Imprimiendo..");
                } else {
                    return new PluginResult(PluginResult.Status.ERROR);
                }
            } else {
                return new PluginResult(PluginResult.Status.INVALID_ACTION);
            }
        } catch (JSONException e) {
            return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
        }
    }

    private void testSendFile(ZebraPrinter printer) {
        try {
            File filepath = context.getFileStreamPath("TEST.LBL");
            createDemoFile(printer, "TEST.LBL");
            printer.sendFileContents(filepath.getAbsolutePath());
            this.saveBluetoothAddress(this, macAddress);

        } catch (ConnectionException e1) {
            System.out.println("Error sending file to printer" + e1.getMessage());
        } catch (IOException e) {
            System.out.println("Error creating file" + e.getMessage());
        }
    }

    private void saveBluetoothAddress(SibPrint sibPrint,
            String macAddress) {
        // TODO Auto-generated method stub

    }    

       private void createDemoFile(ZebraPrinter printer, String fileName) throws IOException {

            FileOutputStream os = context.openFileOutput(fileName, Context.MODE_PRIVATE);

            byte[] configLabel = null;

            PrinterLanguage pl = printer.getPrinterControlLanguage();
            if (pl == PrinterLanguage.ZPL) {
                configLabel = "^XA^FO17,16^GB379,371,8^FS^FT65,255^A0N,135,134^FDTEST^FS^XZ".getBytes();
            } else if (pl == PrinterLanguage.CPCL) {
                String cpclConfigLabel = "! 0 200 200 406 1\r\n" + "ON-FEED IGNORE\r\n" + "BOX 20 20 380 380 8\r\n" + "T 0 6 137 177 TEST\r\n" + "PRINT\r\n";
                configLabel = cpclConfigLabel.getBytes();
            }
            os.write(configLabel);
            os.flush();
            os.close();
        }

}

2 个答案:

答案 0 :(得分:0)

试试这个..

你没有初始化 context testSendFile()方法内的

File filepath = context.getFileStreamPath("TEST.LBL");

你应该初始化 context才能使用它,否则你会得到 NPE

答案 1 :(得分:0)

上下文中有 java.lang.NullPointerException

File filepath = context.getFileStreamPath("TEST.LBL");

你没有在课堂上初始化语境。

请添加

public void SibPrint(Context context){
        this.context=context;
        }

在你班上。 并在活动中创建consructor,如

SibPrint mSibPrint =新的SibPrint(MyActivity.this);

public class SibPrint extends Plugin {

    ProgressDialog loadingDialog;
    Activity activity;
    private Context context;
    String macAddress;
    String filepath;

    //add this method
    public void SibPrint(Context context){
    this.context=context;
    }
.
.
.
}