0
I’m creating a Java application that integrates with active directory. But I’m having trouble returning a specific data: the Office field (Office - physicalDeliveryOfficeName).
Theoretically, his logic should work similarly to the other methods I have, but I always take NullPointerException
when trying to return this field. The other methods that return other data (sAMAccountName, cn, givenName, mail and the like all work right). I’ll put the code down here.
This is the method getGivenName(Pessoa)
, one of those functioning properly.
public String getGivenName(Pessoa p) throws NamingException {
String givenName = "";
try {
NamingEnumeration<SearchResult> result = this.searchUser(p);
if (result.hasMoreElements()) {
SearchResult sr = (SearchResult) result.next();
Attributes attrs = sr.getAttributes();
givenName = attrs.get("givenName").toString();
givenName = givenName.substring(givenName.indexOf(":") + 1);
}
} catch (Exception e) {
e.printStackTrace();
}
return givenName;
}
This is the method I use to connect to AD, with the LDAP query in it. It also works correctly
public NamingEnumeration<SearchResult> searchUser(Pessoa p) throws NamingException {
String filter = "(&(objectCategory=person)(objectClass=user)(sAMAccountName=" + p.getUsername() + "))";
return this.dirContext.search("DC=umc,DC=br", filter, this.searchCtls);
}
This is the method I spoke of, which returns null and harms me.
public String getPhysicalDeliveryOfficeName(Pessoa p) throws NamingException {
String physicalDeliveryOfficeName = "";
try {
NamingEnumeration<SearchResult> result = this.searchUser(p);
if (result.hasMoreElements()) {
SearchResult sr = (SearchResult) result.next();
Attributes attrs = sr.getAttributes();
physicalDeliveryOfficeName = attrs.get("physicalDeliveryOfficeName").toString();
physicalDeliveryOfficeName = physicalDeliveryOfficeName.substring(physicalDeliveryOfficeName.indexOf(":") + 1);
}
physicalDeliveryOfficeName = physicalDeliveryOfficeName.replaceAll("[^\\d.]", "");
} catch (Exception e) {
e.printStackTrace();
}
return physicalDeliveryOfficeName;
}
In this field, the employee plate number is stored, and I need this number as the primary key for a registry in a database. But because of the problem I mentioned, I can’t return the data, I don’t know why. The image below has more details of the information and how they are shown.
Ad would be active directory?
– user28595
Exactly, @diegofm. I am connecting to Active Directory using LDAP.
– Thales Alves