代码写完,findbugs了一把,出现提示。
Pattern: Immediate dereference of the result of readLine()
id: NP_IMMEDIATE_DEREFERENCE_OF_READLINE, type: NP, category: STYLE
The result of invoking readLine() is immediately dereferenced. If there are no more lines of text to read, readLine() will return null and dereferencing that will generate a null pointer exception.
大概的意思就是说 BufferedReader类的readLine()在没有文本读取的情况下将 返回null,在操作之前需要判断是否为null。
String cmd = new BufferedReader(new InputStreamReader(System.in)).readLine().trim();
不加trim()方法不会有问题,即trim()之前需要先判断是否为null.