我开发了android应用程序来打印蓝牙打印机的账单,我的代码抛出异常为“java.lang.NullPointerException:尝试调用虚方法'void java.io.OutputStream.write(byte [])'on空对象引用“。
用于连接蓝牙打印机并将数据发送到打印的我的代码如下:
enter code here
//这将找到一个蓝牙打印机设备
void findBT()
{
try {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
//myLabel.setText("No bluetooth adapter available");
Utilities.showToast("No bluetooth adapter available", true);
}
if (!mBluetoothAdapter.isEnabled())
{
Utilities.showToast("Bluetooth is not On, Please Turn on Blueetooth", true);
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter
.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
//Toast.makeText(PrintMain.this, "Device Name : " + device.getName(), Toast.LENGTH_SHORT).show();
// MP300 is the name of the bluetooth printer device
if (device.getName().equals("PTP-III")) {
mmDevice = device;
break;
}
}
}
// myLabel.setText("Bluetooth Device Found");
Utilities.showToast("Bluetooth Device is Found", true);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
enter code here
void openBT() throws IOException {
try {
UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");
//UUID uuid = UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
mmSocket.connect();
mmOutputStream = mmSocket.getOutputStream();
mmInputStream = mmSocket.getInputStream();
beginListenForData();
// myLabel.setText("Bluetooth Opened");
Utilities.showToast("Bluetooth connection is Open now", true);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
enter code here
void beginListenForData() {
try {
final Handler handler = new Handler();
// This is the ASCII code for a newline character
final byte delimiter = 10;
stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[1024];
workerThread = new Thread(new Runnable() {
public void run() {
while (!Thread.currentThread().isInterrupted()
&& !stopWorker) {
try {
int bytesAvailable = mmInputStream.available();
if (bytesAvailable > 0) {
byte[] packetBytes = new byte[bytesAvailable];
mmInputStream.read(packetBytes);
for (int i = 0; i < bytesAvailable; i++) {
byte b = packetBytes[i];
if (b == delimiter) {
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0,
encodedBytes, 0,
encodedBytes.length);
final String data = new String(
encodedBytes, "US-ASCII");
readBufferPosition = 0;
handler.post(new Runnable() {
public void run() {
// myLabel.setText(data);
}
});
} else {
readBuffer[readBufferPosition++] = b;
}
}
}
} catch (IOException ex) {
stopWorker = true;
}
}
}
});
workerThread.start();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
enter code here
void sendData(String VoucherNumber, String ConnectedCmpName, String EntryUserName, String PartyName, String VoucherDate, Context mContext1, String[] InvItemDtls ) throws IOException
{
try
{
String StrVisitAgain = "VISIT AGAIN \n";
mmOutputStream.write(StrVisitAgain.getBytes());
mmOutputStream.flush();
} catch (Exception e) {
Log.e("Main", "Exe ", e);
System.out.println("Excelption is : " + e.toString());
}
}
enter code here
void closeBT() throws IOException {
try {
stopWorker = true;
mmOutputStream.close();
mmInputStream.close();
mmSocket.close();
Utilities.showToast("Bluetootj is now Closed", true);
// myLabel.setText("Bluetooth Closed");
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
enter code here
//打印数据的代码
PrintSalesInvoice p = new PrintSalesInvoice();
try {
p.findBT();
p.openBT();
p.sendData(VoucherNumber, ConnectedCmpName, EntryUserName, PartyName, VoucherDate, getApplicationContext(),InvItemDtls);
p.closeBT();
} catch (IOException ex) {
}
答案 0 :(得分:0)
在SendData函数中添加波纹管代码(打印数据的功能)解决了这个问题。
enter code here
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}