我正在使用Eclipse IDE。
我有一个抛出NullPointerException
的代码,我正在使用try/catch
block
处理该异常>
这是我的代码-
String name = null; int length = 0; try { length = name.length(); System.out.println("Number of letters in name: " + length); } catch(NullPointerException e) { System.out.println("NullPointerException occured"); } System.out.println("I am being executed");
[具有
try
block
的行中的identifier
length
内,我有警告-
Null pointer access: The variable name can only be null at this location
我正在尝试通过使用-来抑制警告]
@SuppressWarnings("all") length = name.length();
这里有
identifier
length
的行我有错误-Multiple markers at this line - - Syntax error on token "length", VariableDecalaratorId expected after this token - Null pointer access: The variable name can only be null at this location - length cannot be resolved to a type
如何解决此问题?
[我不希望我的程序在显示我正在执行的操作时显示此警告。
我正在使用Eclipse IDE。我有一个引发NullPointerException的代码,并且正在使用try / catch块处理该异常。这是我的代码-字符串名称= null; int长度= 0; ...
感谢@Slaw对这个答案的评论-
我通过以下方式修改了代码-