System.console() 在 NetBeans 中给出 NullPointerException [英] System.console() gives a NullPointerException in NetBeans

查看:25
本文介绍了System.console() 在 NetBeans 中给出 NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题:方法 readLine()nextLine()nextInt() 等抛出异常:<代码>NullPointerException.

我使用 NetBeans IDE(如果重要).

public static void Reading(){字符串qq;qq = System.console().readLine();System.console().printf(qq);}

解决方案

某些 IDE 不提供控制台.请注意,System.console() 在这些情况下返回 null.

来自文档<块引用>

退货:

    系统控制台,如果有,否则为空.

您始终可以使用 System.inSystem.out 代替,如下所示:

字符串qq;扫描仪scanner = new Scanner(System.in);qq = 扫描仪.nextLine();System.out.println(qq);

I have the following problem: method readLine() or nextLine(), nextInt(), etc. throw an exception: NullPointerException.

I use the NetBeans IDE (if it matters).

public static void Reading()
{
    String qq;
    qq = System.console().readLine();
    System.console().printf(qq);
}

解决方案

Some IDEs don't provide a console. Note that System.console() returns null in these cases.

From the documentanion

Returns:

     The system console, if any, otherwise null.

You can always use System.in and System.out instead, as follows:

String qq;
Scanner scanner = new Scanner(System.in);
qq = scanner.nextLine();
System.out.println(qq);

这篇关于System.console() 在 NetBeans 中给出 NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆