-1

I am trying to test this script, to get print some demo text to my Zebra printer using Android, but I am getting a java.lang.NullPointerException. I have read is something about an initialization, but I can't identify what is the object to initializate.

this is the error:

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)

Thanks for your help!

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();
        }

}
ufo20
  • 35
  • 1
  • 5
Marcos
  • 77
  • 1
  • 9

2 Answers2

0

Try this..

You have not initialize context that's inside testSendFile() method

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

you should initialize context before using it otherwise you will get NPE

Hariharan
  • 28,756
  • 7
  • 51
  • 55
  • Thanks Hariharan, i am new at java and want to learn how to use context in my app. don't know too much about it. can you tell me how to initialize context on my program ?, Thanks in advance – Marcos Aug 12 '14 at 04:40
0

you got java.lang.NullPointerException in Context at

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

you are not initialize context in your class.

please add

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

in your class. and create consructor in Activity like

SibPrint mSibPrint = new 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;
    }
.
.
.
}
Mr X
  • 1,038
  • 1
  • 11
  • 24
  • Thanks again Prakash, i have added the method in my class, but i am stuck with SibPrint mSibPrint = new SibPrint(MyActivity.this);. This is a constructor i have to create inside my class ? sorry, i am completely new in java. – Marcos Aug 12 '14 at 05:00
  • @Marcos `SibPrint mSibPrint = new SibPrint(MyActivity.this);` add this line in your activity class like `MyActivity extends Activity{...}`. – Mr X Aug 12 '14 at 05:04
  • where you create constructor of `SibPrint` class? – Mr X Aug 12 '14 at 05:04
  • ok, i am understanding that i have to create another class called MyActivity that extends Activity ? (because my actual class extends Plugin.) and inside of the new class, i create a constructor : SibPrint mSibPrint = new SibPrint(MyActivity.this);, it's that correct ? – Marcos Aug 12 '14 at 05:13
  • you are not understand perfectly – Mr X Aug 12 '14 at 05:19
  • You are right, is my fault. i will use your help and investigate a little more how to create a constructor in my activity class and will try to implement it.Thanks for your help, really appreciated Prakash – Marcos Aug 12 '14 at 05:24
  • @Marcos i am sorry I was created constructor in above code. in Bold line – Mr X Aug 12 '14 at 05:32
  • i saw it, but i just don't know where to place that code to make it work – Marcos Aug 12 '14 at 05:39
  • replace your old constructor with new constructor in activity – Mr X Aug 12 '14 at 06:04