我有以下几行代码,而sonarqube说:


  “更改此条件,以便它不会总是评估为false”




下面是这行。

if (params.isEmpty() && params == null) {
        throw new ServiceSDKException("Parameters cannot be empty or null!");
    }


以下是您需要时的整个方法。

public void init(String params) throws ServiceSDKException {
        if (params.isEmpty() && params == null) {
            throw new ServiceSDKException("Parameters cannot be empty or null!");
        }
        String[] configParams = params.split(",");
        options.setMqttURL(configParams[0]);
        options.setMqttClientID(configParams[1]);
        try {
            options.setWillMessage("v1/items/mqtt/0/event/will"
                    , "Last will"
                    , 2, true);
            new File("./db").mkdir();
            edgeNode = EdgeNodeFactory.createMQTTChannel("./db", options,
                    subscriptionTask, 500, 500);
            isClientConnected = true;
        } catch (EdgeNodeException e) {
            isClientConnected = false;
            throw new ServiceSDKException("EdgeNodeException occurred", e);
        }

    }

最佳答案

if (params.isEmpty() && params == null)


如果您成功执行了params.isEmpty却没有抛出NullPointerException,那么params必然非空。

我想也许你的意思是:

if (params == null || params.isEmpty())

09-04 21:47