1 Reply Latest reply on Jun 6, 2006 5:46 PM by Andrew Oliver

    Null Pointer in MessageData class

    sappenin Novice

      In org.jboss.mail.mailbox.MessageData.getHeader(String key), a null pointer exception is thrown if an attempt to retrieve an empty header is made. Retrieving non-empty headers is fine. Here's the original code, and my suggested fix below. Are multiple headers allowed in SMTP? If so, this would alter my fix. For example, what if for some reason there are two "Cc" headers....is that Legal? More realistic would be some sort of X- header, where one might encounter two with the same name. For now, it seems that this function merely returns the first header with a given value. Might be worth noting that in the docs.


       /**
       * get a header value
       * @param key to find the value for
       * @return header value
       */
       public String getHeader(String key) {
       String[] headers = getHeaders();
       for (int i = 0; i < headers.length; i++) {
       String s = headers;
       if (s.startsWith(key+":")) {
       return headers.split(key+": ")[1]; //_NPE THROWN HERE_
       }
       }
       return null;
       }
      


      Suggested Fix:

       /**
       * get a header value
       * @param key to find the value for
       * @return header value
       */
       public String getHeader(String key) {
       String[] headers = getHeaders();
       for (int i = 0; i < headers.length; i++) {
       String s = headers;
       if (s.startsWith(key+":")) {
      
       //Check for an empty header
       String[] sHeader = headers.split(key+": ");
       if(sHeader.length > 1)
       return sHeader[1];
       else
       return null; //There's no header 'value' to return
       }
       }
       return null;
       }