• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Null pointer Exception in Thread Program

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am running the following program created by somebody else. I am getting Null pointer Exception,






Please enter the number of threads n:
java.lang.NullPointerException
at hw7q1_sleep.HW7Q1_sleep.main(HW7_sleep.java:33)




Somebody please guide me how to correct this error.

Zulfi.
 
Saloon Keeper
Posts: 7165
165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which object (or variable) is null?
 
Marshal
Posts: 74353
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...and which line is the exception thrown from? Line 33 doesn't throw any exceptions.

There is a lot more to say about your code, including line length, indentation, and names of classes.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


It's got to be this line.
I don't see anything else that could throw an NPE in there.

readLine on Console can.
 
Campbell Ritchie
Marshal
Posts: 74353
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depends how you are starting the program. If you start it from the terminal with java HW..., then there will be a Console, but it you start it with JWS or javaw, which you get if you run it as part of an executable .jar or from an IDE there won't be a Console and any attempt to use the Console will suffer such an exception. That is why I have forgotten that Console even exists.
 
Zulfi Khan
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am using Netbeans 8.2. This means that NetBeans 8.2 is not suitable for this program. Is there any way I can run run it on NetBeans 8.2?

Zulfi.
 
Tim Moores
Saloon Keeper
Posts: 7165
165
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just run it on the command line instead. Problem (probably) solved.
 
Campbell Ritchie
Marshal
Posts: 74353
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Zulfi Khan wrote:. . . . This means that NetBeans 8.2 is not suitable for this program. Is there any way I can run run it on NetBeans 8.2? . . .

Haven't you answered your own question? As Tim M says, it should run nicely at the command line/terminal.

Stop using Console. I have tried Console, but soon found its behaviour too unpredictable; it is too likely to be null and cause problems like what you are seeing. Use a Scanner reading from System.in, or better still, write a utility class to read from System.in. System,in is usually represented on IDEs (at least I think it is).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic