2016-07-05 52 views
1

發送字節我有一個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(); 
      } 

我在做什麼錯在這裏?如果這是顯而易見的道歉,這是我的第一個項目。

回答

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