2

I wrote a simple four-step java program to understand the functioning of Console function but it throws NullPointerException console is used. I have no idea why the error shows up.

The following is my code.

import java.io.*;
import java.io.Console;

public class ConsoleWork {
    public static void main(String[] args) {        
        Console c =System.console();
        System.out.println("Enter your name ");
        //line 21
        String s=c.readLine();
        System.out.println("Welcome "+s);       
    }
}

And this is what I get in the console when I run my program:

Enter your name 
Exception in thread "Timer-0" java.lang.NullPointerException
    at Swapping$1.run(Swapping.java:21)
    at java.base/java.util.TimerThread.mainLoop(Timer.java:556)
    at java.base/java.util.TimerThread.run(Timer.java:506)

0 Answers0