• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread Null pointer problem  RSS feed

Dianne Calhoun
Posts: 29
  • Mark post as helpful
  • send pies
  • 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:
Exception in thread "main" java.lang.NullPointerException
at printOutThreadGroups.printOutThreadGroups(printOutThreadGroups.java:5
at printOutThreadGroups.main(printOutThreadGroups.java:9)
Thanks, Dianne
Jim Yingst
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!