从jComboBox发送字节

时间:2016-07-05 08:17:07

标签: java byte jcombobox

我有一个带有这些值的jComboBox:

String[] preset = { "1", "2", "3", "4", "5" };

因此,如果选择“1”,那么我想在OutputStream中添加一个字节。

byte preset1 = 0X01; 

基于组合框中的选择。我认为这可能是这个,但它给出了一个NullPointerException。

byte preset = (byte)setPresetcomboBox.getSelectedItem();
            try {

                byte[] command = {(byte) startTx, address, setPreset, 0x00, preset, endTx, 0x0F};               
                TwoWaySerialComm.SerialWriter sw = new TwoWaySerialComm.SerialWriter(
                        twoWaySerCom.serialPort.getOutputStream());

            sw.out.write(command);

            } catch (IOException e) {
            e.printStackTrace();
            }

我在这里做错了什么?抱歉,如果这很明显,这是我的第一个项目。

1 个答案:

答案 0 :(得分:0)

通过更改

解决了这个问题
String[] preset = { "1", "2", "3", "4", "5" };

Byte[] preset = { 1, 2, 3, 4, 5};

制作像这样的组合框:

setPresetcomboBox = new JComboBox<Byte>(preset);

我的行动已经完成:

byte _preset = (Byte)setPresetcomboBox.getSelectedItem();