• 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

Thread Null pointer problem

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got my program to compile, but I'm getting this null pointer error. Can anyone help me figure out why? I'd appreciate it.
<br /> [errors]<br /> Microsoft Windows 2000 [Version 5.00.2195]<br /> (C) Copyright 1985-2000 Microsoft Corp.<br /> C:\myjava>cd assignment3<br /> C:\myjava\assignment3>cd problem1
C:\myjava\assignment3\problem1>java printOutThreadGroups
Top Most ThreadGroup: "system" has the following Thread Groups:
**********************
Thread Group: "main" has the following threads:
"main"
Exception in thread "main" java.lang.NullPointerException
at printOutThreadGroups.printOutThreadGroups(printOutThreadGroups.java:5
5)
at printOutThreadGroups.main(printOutThreadGroups.java:9)
C:\myjava\assignment3\problem1>
[/errors]
------------------
Thanks, Dianne
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The NullPointerException message tells you that it originates from line 55 of your file. This is really useful information, and much easier for you to find than for us. So whe you post an error like this, it's a good idea to point out for us exactly which line is throwing the error. By inspection though, I'm pretty sure it's the line
<pre> System.out.println(indent +" \""+aThread.getName()+"\"");</pre>
The reason is that aThread is null - at least, it may be null. Note that the ThreadGroup activeGroupCount() method only returns an estimate of the number of active groups - which means the number of groups in the array after enumerate() is called, may be different. And if enumerate returns fewer groups than the original estimate, then your array will have nulls in the remaining slots. Instead, save the return value from enumerate() to find out how many groups were actually returned, and use this to control your for loop through the array. Or, check each element to see if it's null before you access it.
 
Whose rules are you playing by? This tiny ad doesn't respect those rules:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic