NoSuchElementException after closing system.in

[ Interrupt a thread waiting for user input and then exit the app ]

I wanted to do the same exercise as mentioned in the above link. But, I ran into NoSuchElementException when I tried to use inConsole.nextLine(), after successfully closing BufferedReader object, in spite of reassigning inConsole like inConsole = new Scanner(System.in) to read from Standard input.

After closing BufferedReader object, can’t I reassign to Scanner object like above and use it?

Answer

Never close neither System.in nor System.out. This will work and cause some strange effects. Actually you will never be able to read or write to SYSIN of SYSOUT respectively. Find other solution.

If you need advise how to solve your problem without closing System.in please provide more information about lifecycle of your application and probably send relevant code snippets.

Leave a Reply

Your email address will not be published. Required fields are marked *