This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Null pointer Exception in Thread Program

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • 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: 6066
154
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which object (or variable) is null?
 
Marshal
Posts: 67464
257
  • Mark post as helpful
  • send pies
  • 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: 4450
47
  • Mark post as helpful
  • send pies
  • 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: 67464
257
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 125
  • Mark post as helpful
  • send pies
  • 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: 6066
154
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just run it on the command line instead. Problem (probably) solved.
 
Campbell Ritchie
Marshal
Posts: 67464
257
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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).
 
What do you have in that there bucket? It wouldn't be a tiny ad by any chance ...
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!