Skip to content

Commit

Permalink
prevent NPE by checking arrays, refactor method name
Browse files Browse the repository at this point in the history
  • Loading branch information
silverailscolo authored and bobjacobsen committed Jun 20, 2019
1 parent 4fea42c commit 9403241
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
10 changes: 6 additions & 4 deletions java/src/jmri/jmrix/AbstractSerialPortController.java
Expand Up @@ -2,7 +2,6 @@

import java.util.Enumeration;
import java.util.Vector;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import purejavacomm.CommPortIdentifier;
Expand Down Expand Up @@ -147,7 +146,7 @@ public void configureBaudRate(String rate) {
}

@Override
public void configureBaudNumber(String indexString) {
public void configureBaudRateFromNumber(String indexString) {
int baudNum;
int index = 0;
final String[] rates = validBaudRates();
Expand All @@ -172,7 +171,7 @@ public void configureBaudNumber(String indexString) {
baudNum = Integer.parseInt(indexString); // new storage format, will throw ex on old format
log.debug("new profile format port speed value");
} catch (NumberFormatException ex) {
// old pre 4.15.8 format is i18n string including thousand separator and whatever suffix like "18,600 bps"
// old pre 4.15.8 format is i18n string including thousand separator and whatever suffix like "18,600 bps (J1)"
log.warn("old profile format port speed value converted");
// filter only numerical characters from indexString
StringBuilder baudNumber = new StringBuilder();
Expand Down Expand Up @@ -242,6 +241,9 @@ public String getCurrentBaudNumber() {
if (mBaudRate != null) {
int[] numbers = validBaudNumbers();
String[] rates = validBaudRates();
if (numbers == null || rates == null || numbers.length != rates.length) { // arrays should correspond
return "";
}
String baudNumString = "";
// find the configured baud rate value
for (int i = 0; i < numbers.length; i++) {
Expand Down Expand Up @@ -310,7 +312,7 @@ public int currentBaudNumber(String currentBaudRate) {
return -1;
}

// find the baud rate value, configure comm options
// find the baud rate value
for (int i = 0; i < numbers.length; i++) {
if (rates[i].equals(currentBaudRate)) {
return numbers[i];
Expand Down
2 changes: 1 addition & 1 deletion java/src/jmri/jmrix/SerialPortAdapter.java
Expand Up @@ -74,7 +74,7 @@ public interface SerialPortAdapter extends PortAdapter {
* <p>
* Only to be used after construction, but before the openPort call.
*/
public void configureBaudNumber(String index);
public void configureBaudRateFromNumber(String index);

public void configureBaudIndex(int index);

Expand Down
Expand Up @@ -82,7 +82,7 @@ public boolean load(Element shared, Element perNode) {
String portName = perNode.getAttribute("port").getValue();
adapter.setPort(portName);
String baudNumber = perNode.getAttribute("speed").getValue();
adapter.configureBaudNumber(baudNumber);
adapter.configureBaudRateFromNumber(baudNumber);
loadCommon(shared, perNode, adapter);
// register, so can be picked up next time
register();
Expand Down
13 changes: 6 additions & 7 deletions java/src/jmri/jmrix/sprog/simulator/SimulatorAdapter.java
Expand Up @@ -196,18 +196,17 @@ public String[] validBaudRates() {
return null;
}

@Override
public String getCurrentBaudRate() {
return "";
}

/**
* {@inheritDoc}
*/
@Override
public int[] validBaudNumbers() {
int[] numbers = {0};
return numbers;
return null;
}

@Override
public String getCurrentBaudRate() {
return "";
}

@Override
Expand Down

0 comments on commit 9403241

Please sign in to comment.