1

This is my first time touching Java in a few years. I have been tasked with creating a runnable Jar file which will be called to communicate with an RFID card reader. Now, my code steps through fine in Debug mode, however, when I simply try to run the programme, I'm hit with a NullPointerException. Excuse me if the answer is obvious, but its been a while since I touched Java.

I'm using Eclipse IDE (2019)

Any help would be most appreciated

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


    }

}

However, when running out of debug mode I am hit with the following 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)
c_k000
  • 21
  • 2
  • 4
    When you are stepping through then the program is running WAY slower than when just run normally. It may be that when you call 'portReader = serialPort.readBytes();' there is no bytes to read yet. You should check that there are some bytes in portReader before converting to hex. – mwarren Oct 03 '19 at 14:41
  • @mwarren Thank you sir, I will give that a go, I just remembered after you've mentioned this a colleague did mention calling some form of wait function before continuing. I'll give this a go. – c_k000 Oct 03 '19 at 14:44
  • @mwarren worked like a charm. Thank you again sir. Just to clarify for anyone else referencing this question, I solved the issue (as directed byt mwarren) by editing the portReader variable to the code attached below. – c_k000 Oct 03 '19 at 14:55
  • @c_k000 You can accept your own answers as well, to ensure future readers know it fixed the issue. – Nexevis Oct 03 '19 at 15:10

1 Answers1

1

I solved the issue (as directed by mwarren) by editing the portReader variable to the code below:

try {
    portReader = serialPort.readBytes(28,100);
} catch (SerialPortTimeoutException e) {
    e.printStackTrace();
}// read response
Nexevis
  • 4,647
  • 3
  • 13
  • 22
c_k000
  • 21
  • 2