Malte said:
I am using JNDI to retrieve records from Active Directory.
For some of the users the following snippet throws a NullPointerException:
int userAccountControl =
Integer.parseInt((String)att.get("userAccountControl").get());
In fact, just this code: att.get("userAccountControl").get();
throws an exception.
The att object is a result of
att = sr.getAttributes()
where sr is a SearchResult object.
Using an ldap browser I see nothing unusual, the userAccountControl
attribute usually has the value 512, in some instances 514 (invalid).
I'd like to know what might throw this exception.
Hard to say just from this code snippet and your schema description.
Plus I only really use OpenLDAP. Even still, I'd try to log all the
attributes when the exception is thrown, using something like...
for (NamingEnumeration ne = attrs.getAll(); ne.hasMoreElements()
{
Attribute attr = (Attribute)ne.next();
String attrID = attr.getID();
System.out.println (attrID+":");
for (Enumeration vals = attr.getAll();vals.hasMoreElements()
{
System.out.println ("\t"+vals.nextElement());
}
}
Since you only get the error sometimes, one of your fields must be
unexpectedly null, ie, my guess is that your data is not always
organized as expected.
For what its worth, I do something like:
/**
Gets a java object from LDAP associated by a distiguished name.
<p>
This method is inteneded to be used for schemas like inetOrgPerson
for attributes
such as userCertificate;binary
<p>
@param type Which attribute to search for and return
@param results Complete list of all available entries for the
requested DN
@return Object or null if there are problems
*/
private Object doLookup (NamingEnumeration results, String type)
throws WSSecurityException
{
try
{
Fwlog.debug(this, Fwlog.DB, "doLookup...");
if (results.hasMore())
{
SearchResult sr = (SearchResult) results.next();
javax.naming.directory.Attributes xanswer =
sr.getAttributes();
javax.naming.directory.Attribute attribute =
xanswer.get(type);
// check if attribute missing
if (null==attribute)
{
throw new IllegalStateException("\nERROR: Attribute type not
found: " + type);
}
// retrieve attribute as object and return
Object o = attribute.get();
if (null == o)
{
throw new IllegalStateException("Recieved null object for
attribute type: " + type);
}
return o;
}
else
{
throw new IllegalStateException("Empty Subject recieved");
}
}//end try
catch ( Exception e )
{
Fwlog.error(this, Fwlog.DB, "doLookup() failed for for type: " +
type);
Fwlog.error(this, Fwlog.DB, e);
}
}
HTH,
iksrazal
http://www.braziloutsource.com/