2016-07-20 23 views
-6

属性に少し問題があります。私は現在、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!"; 
     } 


    } 
+4

一貫した字下げと任意の空白行の読み込みがないように、コードを読みやすい形でフォーマットできますか?現在は床に落としたようです。 – khelwood

+0

attrs.get( "mail")がヌル自体を返すため、.toString()が例外を出しています –

+0

なぜ私の質問の評価がとても下がっていますか? 「JavaScriptやJavaのコンソールで「Hello World」をどのようにプリントアウトするか」と同じようなスーパー・ダムの質問のようなものではありません。私のコードの形式は完璧ではないが、まだ読める。私が削除しなければならなかったロシア語の他の同僚のプログラマーによる多くのコメントがあったので、皆さんはそれらを気にしません。私は質問をするのに間違ったことをしましたか? –

答えて

1

この

User user = new User(attrs.get("mail")!=null?attrs.get("mail").toString():null); 
を試してみてください
+2

['Objects.toString(attrs.get(" mail "))'](https://docs.oracle.com/javase/7/docs/) api/java/util/Objects.html#toString(java.lang.Object))は、3つのステートメントとほぼ同じです( 'attrs.get'を2回呼び出さない)。 –

+0

それは実際に働いた!なぜそれが働いたのか説明できますか?それはattrs.get( "mail")がnullを返していたため、null値に対してtoString()メソッドを呼び出せなかったので動作しませんでした。 –

+0

Userオブジェクトにnullが渡されます。 –

0

最初に、!= nullを使用して確認する必要があります(たとえば、Objects.toStringを使用するか、またはifを手動で使用するかを確認してからと同じようににtoStringを使用する必要があります)。

User user = new User(Objects.toString(attrs.get("mail"))); 

それとも、また(あなたは多くを持っている場合は、getAllを使用する必要があり、属性に単一の値を取得するために)使用することができます::内部ん

Object mail = null; 
if (attrs.get("mail") != null) { 
    mail = attrs.get("mail").get(); 
} 
User user = new User(mail.toString()); 
+0

それは私にこのようなものを与える:sun.reflect.NativeMethodAccessorImpl.invokeでsun.reflect.NativeMethodAccessorImpl.invoke0(ネイティブメソッド)でLdapConnection.mainでjava.lang.NullPointerExceptionが \t(LdapConnection.java:77) \t \t( NativeMethodAccessorImpl.java:62)sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)java.lang.reflect.Method.invokeで \t(Method.java:498)で \t com.intellij.rtで \t .execution.application.AppMain.main(AppMain.java:147) –

+0

toStringの実装に依存することはお勧めできません。それが変わるとどうなりますか? –

+0

@MarkChorley 'get()'がオブジェクトを返す場合は、他にどのようなオプションがありますか? –

関連する問題