如何在Java中禁止空指针访问警告?

问题描述 投票:1回答:1

我正在使用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; ...

java exception nullpointerexception annotations suppress-warnings
1个回答
0
投票

感谢@Slaw对这个答案的评论-

我通过以下方式修改了代码-

© www.soinside.com 2019 - 2024. All rights reserved.