Doom Dooms Doom Dooms - 11 months ago 35
Java Question

"Cast" a String Attribute to String Java

So here am having a little problem with Attributes. I am currently working on a project that parses emails from an ldap server into the Java application that will be doing some interesting stuff with emails in the future. I am currently having this code that takes emails from users on ldap and it needs to put emails in the User class as seen in my code:

[some code here, TRY CATCH is also included]

LdapContext ctx = new InitialLdapContext(env, null);
NamingEnumeration<?> namingEnum2 ="[path to the server]", "(objectClass=user)", getSimpleSearchControls());

System.out.println("Test: print emails from whole DIRECOTRY: \n");

while (namingEnum2.hasMore()) {

SearchResult result = (SearchResult);
Attributes attrs = result.getAttributes();

/** This line above works fine, but every time there is no email in User info,
it prints "null" in some cases when there is no email, which is not perfect. But This is just here to see if everything works and indeed it does.**/

/**User Class accepts a String parameter, checks if it's empty and all that,
does some checking and etc... BUT! attrs.get("mail") is an Attribute, NOT a String. And I need to somehow "cast" this attribute to a String, so I can work with it.**/
User user = new User(attrs.get("mail")); //error yet,because the parameter is not a String.

User user = new User(attrs.get("mail").toString());//gives an expeption.

/** And guys, I know that there is a toString() method in Attribute Class,
but it doesn't work, it gives an "java.lang.NullPointerException" exeption when I use it as attrs.get("mail").toString()

Please help me solve this problem, I have a deadline, I am still a Junior developer, and I am really trying to get it done. Here is User class's constructor:

public User(String mail){
eMail = "NO EMAIL!";
if (mail != null && !mail.isEmpty()){

eMail = mail;

eMail = "NO EMAIL!";




try this

User user = new User(attrs.get("mail")!=null?attrs.get("mail").toString():null);