import java.io.Console;public class NewConsole {
    public static void main(String[] args)
    {
        Console c = System.console();
        String name="d";
        
      char[] pw = c.readPassword();//这句抛出异常但不知怎样改
        for(char ch: pw)
            c.format("%c ", ch);
        c.format("\n");
        
        MyUtility mu = new MyUtility();
        while(true)
        {
            
            name = c.readLine("%s", "input?: ");
            c.format("output :%s \n",mu.doStuff(name));
        }
        
    }
}class MyUtility
{
    String doStuff(String arg1)
    {
        return "result is "+arg1;
    }
}

解决方案 »

  1.   

    Console c 这个东西为null
      

  2.   


    public class NewConsole {
    public static void main(String[] args)
      {
      Console c = System.console();
      String name="d";
       if(c==null){
       System.out.println("C未获取到,导致下面那句报错!");
       }
      char[] pw = c.readPassword();//这句抛出异常但不知怎样改
      for(char ch: pw)
      c.format("%c ", ch);
      c.format("\n");
       
      MyUtility mu = new MyUtility();
      while(true)
      {
       
      name = c.readLine("%s", "input?: ");
      c.format("output :%s \n",mu.doStuff(name));
      }
       
      }
    }
      

  3.   

    console
    public static Console console()返回与当前 Java 虚拟机关联的唯一 Console 对象(如果有)。 返回:
    系统控制台(如果有),否则返回 null。
    从以下版本开始: 
    1.6 
    System.console()有可能返回空值
    童鞋可以去看api..
      

  4.   

    程序 肯定是在eclipse 下运行的吧 Console是得到当前控制台的信息的
    用cmd 下运行程序一点问题没有 
      

  5.   

    那是以为控制台的console为空,所以会报空指针异常。
    Console c = System.console();测试c一下是否为空,如果为空就返回,如果不为空进行读数据
      

  6.   

      Console c = System.console();
      if(c!=null) 加上