【问题标题】:Send Byte from jComboBox从 jComboBox 发送字节
【发布时间】:2016-07-05 08:17:06
【问题描述】:

我有一个带有这些值的 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();
            }

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

【问题讨论】:

    标签: java byte jcombobox


    【解决方案1】:

    通过修改解决了这个问题

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

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

    像这样制作组合框:

    setPresetcomboBox = new JComboBox<Byte>(preset);
    

    我的行动执行了:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-15
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      • 2016-03-05
      • 2011-02-28
      相关资源
      最近更新 更多