属性に少し問題があります。私は現在、LDAPサーバーからJavaアプリケーションへの電子メールを解析するプロジェクトを進めており、今後は電子メールで興味深いものをやっていきます。文字列属性を文字列にキャストするJava
私は現在、LDAP上のユーザーからのメールを受け取り、それは私のコードに見られるように、Userクラスに電子メールを置く必要がある、このコードを持っています:ここで
[some code here, TRY CATCH is also included]
LdapContext ctx = new InitialLdapContext(env, null);
ctx.setRequestControls(null);
NamingEnumeration<?> namingEnum2 = ctx.search("[path to the server]", "(objectClass=user)", getSimpleSearchControls());
System.out.println("Test: print emails from whole DIRECOTRY: \n");
while (namingEnum2.hasMore()) {
SearchResult result = (SearchResult) namingEnum2.next();
Attributes attrs = result.getAttributes();
System.out.println(attrs.get("mail"));
/** 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 I know that there is a toString() method in Attribute Class,
but it doesn't work, it gives an "java.lang.NullPointerException"
exception when I use it as attrs.get("mail").toString() **/
}
は、Userクラスのコンストラクタです:
public User(String mail){
eMail = "NO EMAIL!";
if (mail != null && !mail.isEmpty()){
eMail = mail;
}
else
{
eMail = "NO EMAIL!";
}
}
一貫した字下げと任意の空白行の読み込みがないように、コードを読みやすい形でフォーマットできますか?現在は床に落としたようです。 – khelwood
attrs.get( "mail")がヌル自体を返すため、.toString()が例外を出しています –
なぜ私の質問の評価がとても下がっていますか? 「JavaScriptやJavaのコンソールで「Hello World」をどのようにプリントアウトするか」と同じようなスーパー・ダムの質問のようなものではありません。私のコードの形式は完璧ではないが、まだ読める。私が削除しなければならなかったロシア語の他の同僚のプログラマーによる多くのコメントがあったので、皆さんはそれらを気にしません。私は質問をするのに間違ったことをしましたか? –