• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why readLine method of System.Console is throwing NullPointerException

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone, can anyone please let me know why the following code compiles fine but gives the following error at runtime.
Exception in thread "main" java.lang.NullPointerException
at practice.ColsoleTest.main(ColsoleTest.java:13)



I will be very grateful for any help.
 
Ranch Hand
Posts: 236
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried this program and did not get any run-time exception.
All it does is to take the input from console and print back.

How're you running it? From IDE like Eclipse?

Try from command line and enter some text and it prints back.
 
amit kumararya
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes i am doing from eclipse.
thanks a lot for pointing this out.

can you please explain why so.

--
amit
 
Surendra Kumar
Ranch Hand
Posts: 236
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eclipse does not support interactive console.

(Use System.in instead)
 
amit kumararya
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much...
 
amit kumararya
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
also, can you please tell me the complete declaration of how to use system.in.

just tell me the object declarations, i will be able to go ahead from there.

Thank you.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

amit kumararya wrote:also, can you please tell me the complete declaration of how to use system.in.


The fact is that most people rarely use it directly - although there's nothing to stop you doing so - it's usually wrapped in a more user-friendly object like a Scanner (java.util.Scanner). This page, for example, shows you how to get console input using a BufferedReader, but personally, I'd advise reading the tutorial chapter on I/O because that will give you a much more comprehensive explanation on how streams work in Java.

Winston
 
amit kumararya
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much....
 
Ranch Hand
Posts: 235
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simply because I came across it first, and it's simple enough to use, I tend to use the Scanner object for my user input.



replace with nextInt, etc. as the need arises.

Robert
 
amit kumararya
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wow, this is wonderful.

thanksssss a lotttt.....
 
Don't listen to Steve. Just read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic