gpt4 book ai didi

java - 运行程序时抛出NullPointerException,但 Debug模式下不抛出NullPointerException Java jssc 串口通信

转载 作者:行者123 更新时间:2023-11-30 05:27:43 25 4
gpt4 key购买 nike

这是我几年来第一次接触Java。我的任务是创建一个可运行的 Jar 文件,该文件将被调用以与 RFID 读卡器进行通信。现在,我的代码在 Debug模式下运行良好,但是,当我只是尝试运行该程序时,我遇到了 NullPointerException。如果答案很明显,请原谅,但自从我接触 Java 以来已经有一段时间了。

我正在使用 Eclipse IDE (2019)

任何帮助将不胜感激

    package rfid;

import jssc.SerialPort;
import jssc.SerialPortException;
import jssc.SerialPortList;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.Formatter;

import javax.xml.bind.DatatypeConverter;

public class RFID_TEST {

public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}// for
return data;
} // hexStringToByteArray

public static String[] reverse(String[] nums) {
String[] reversed = new String[nums.length];
for (int i=0; i<nums.length; i++) {
reversed[i] = nums[nums.length - 1 - i];
}
return reversed;
}

static String byteToHex(final byte[] hash)
{
Formatter formatter = new Formatter();
for (byte b : hash)
{
formatter.format("%02x", b);
}
String result = formatter.toString();
formatter.close();
return result;
}

public static void main (String[] args) {

String[] portNames = SerialPortList.getPortNames();
for(int i = 0; i < portNames.length; i++) {
System.out.println(portNames[i]);
}

String portReader2 = "";

SerialPort serialPort = new SerialPort("COM4");
try {
serialPort.openPort();//Open serial port
serialPort.setParams(38400,8,1,0);

byte[] bytesToSend = new byte[] {};
bytesToSend = hexStringToByteArray("020008010000020001aa550000b463");
serialPort.writeBytes(bytesToSend);//Write data to port
byte[] portReader = new byte[] {};
portReader = serialPort.readBytes();// read response
//portReader2 = DatatypeConverter.printHexBinary(portReader);
portReader2 = byteToHex(portReader);
System.out.println(portReader2);
String portReader3 = portReader2.substring(22, 36);
System.out.println(portReader3);

String[] array= portReader3.split("(?<=\\G.{2})");
System.out.println(java.util.Arrays.toString(array));

String[] finalArray = reverse(array);

String str="";
for(int i=0;i<finalArray.length;i++)
str+=finalArray[i];

String finalOne = str.replaceAll("\\s+","");

BigInteger hex = new BigInteger(finalOne, 16);
System.out.println(hex);

serialPort.closePort();//Close serial port
}
catch (SerialPortException ex) {
System.out.println(ex);
}


}

}

但是,当退出 Debug模式时,我遇到了以下 NullPointerException

COM3
COM4
Exception in thread "main" java.lang.NullPointerException
at rfid.RFID_TEST.byteToHex(RFID_TEST.java:36)
at rfid.RFID_TEST.main(RFID_TEST.java:65)

最佳答案

我通过将 portReader 变量编辑为以下代码解决了该问题(按照 mwarren 的指示):

try {
portReader = serialPort.readBytes(28,100);
} catch (SerialPortTimeoutException e) {
e.printStackTrace();
}// read response

关于java - 运行程序时抛出NullPointerException,但 Debug模式下不抛出NullPointerException Java jssc 串口通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58221367/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com