intellij-community icon indicating copy to clipboard operation
intellij-community copied to clipboard

[bug] check empty before trimming the string to avoid NPE

Open yuezk opened this issue 4 years ago • 0 comments

This is a straightforward change. Before this change, we have to return the empty String explicitly in TextComponentAccessor::getText().

private static class MyTextComponentAccessor implements TextComponentAccessor<JComboBox> {
    @Override
    public String getText(JComboBox comboBox) {
        Object selectedItem = comboBox.getModel().getSelectedItem();
        if (selectedItem != null) {
            return selectedItem.toString();
        }
        return "";
    }
}

After this change, we could write it as below:

private static class MyTextComponentAccessor implements TextComponentAccessor<JComboBox> {
    @Override
    public String getText(JComboBox comboBox) {
        Object selectedItem = comboBox.getModel().getSelectedItem();
        return selectedItem;
    }
}

And the original code in platform/platform-api/src/com/intellij/openapi/ui/BrowseFolderRunnable.java did has flaws when trimming the text.

yuezk avatar May 06 '21 04:05 yuezk