You are viewing a plain text version of this content. The canonical link for it is here.
Posted to mime4j-dev@james.apache.org by "Andrzej Rusin (JIRA)" <mi...@james.apache.org> on 2009/11/03 10:31:59 UTC

[jira] Created: (MIME4J-139) DelegatingFieldParser NPE problem

DelegatingFieldParser NPE problem
---------------------------------

                 Key: MIME4J-139
                 URL: https://issues.apache.org/jira/browse/MIME4J-139
             Project: JAMES Mime4j
          Issue Type: Bug
    Affects Versions: 0.6
         Environment: jre 1.6.11
            Reporter: Andrzej Rusin
            Priority: Minor


We have custom ContentHandlers that extend AbstractContentHandler and add some custom Fields that extend AbstractField.

We were getting:

java.lang.NullPointerException
 at org.apache.james.mime4j.field.DelegatingFieldParser.parse(DelegatingFieldParser.java:51)
 at org.apache.james.mime4j.field.AbstractField.parse(AbstractField.java:171)
 at org.apache.james.mime4j.field.AbstractField.parse(AbstractField.java:63)
 at org.apache.james.mime4j.message.MessageBuilder.field(MessageBuilder.java:101)
 at org.apache.james.mime4j.parser.MimeStreamParser.parse(MimeStreamParser.java:121)
 at org.apache.james.mime4j.message.Message.<init>(Message.java:141)
 at org.apache.james.mime4j.message.Message.<init>(Message.java:100)


always AFTER the MimeStreamParser got invoked with our content handlers (so, not on our parsing, but eg. later, on normal 
new Message(InputStream).

Looked like we are currupting something inside MimeStreamParser.

Then I noticed that DelegatingFieldParser.defaultParser is defined as:

private FieldParser defaultParser = UnstructuredField.PARSER;

while other field parsers have:

static final FieldParser PARSER = new FieldParser() {...}



So I changed DelegatingFieldParser.defaultParser to static final  and the NPEs do not occur anymore.

I do not undestand exactly why this happens, but that are the facts.

Is there any reason why DelegatingFieldParser.defaultParser can't be static final?



-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (MIME4J-139) DelegatingFieldParser NPE problem

Posted by "Markus Wiederkehr (JIRA)" <mi...@james.apache.org>.
    [ https://issues.apache.org/jira/browse/MIME4J-139?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12778166#action_12778166 ] 

Markus Wiederkehr commented on MIME4J-139:
------------------------------------------

@Stefano: thanks for the explanation.

This is what seems to happen chronologically in Stefanos DefaultFieldParserTest:

 1) static initializer of DelegatingFieldParser
 2) static initializer of DefaultFieldParser
 3) static initializer of AbstractField
 4) constructor of DelegatingFieldParser (invoked by AbstractField)
   4.1) variable defaultParser is null (probably because UnstructuredField has not been completely initialized)
 5) static initializer of UnstructuredField
 6) constructor of DelegatingFieldParser (invoked by DefaultFieldParserTest)
  6.1) variable defaultParser is not null (refers to an UnstructuredField instance)
 7) Unit tests invokes AbstractField.parse()
 8) NPE because of defaultParser being null in 3.1

Now what's funny is if you change defaultParser to private static the variable defaultParser is still null in 4.1!

But after initialization has completed the variable points to the right object and the test passes.

> DelegatingFieldParser NPE problem
> ---------------------------------
>
>                 Key: MIME4J-139
>                 URL: https://issues.apache.org/jira/browse/MIME4J-139
>             Project: JAMES Mime4j
>          Issue Type: Bug
>    Affects Versions: 0.6
>         Environment: jre 1.6.11
>            Reporter: Andrzej Rusin
>            Priority: Minor
>   Original Estimate: 0.08h
>  Remaining Estimate: 0.08h
>
> We have custom ContentHandlers that extend AbstractContentHandler and add some custom Fields that extend AbstractField.
> We were getting:
> java.lang.NullPointerException
>  at org.apache.james.mime4j.field.DelegatingFieldParser.parse(DelegatingFieldParser.java:51)
>  at org.apache.james.mime4j.field.AbstractField.parse(AbstractField.java:171)
>  at org.apache.james.mime4j.field.AbstractField.parse(AbstractField.java:63)
>  at org.apache.james.mime4j.message.MessageBuilder.field(MessageBuilder.java:101)
>  at org.apache.james.mime4j.parser.MimeStreamParser.parse(MimeStreamParser.java:121)
>  at org.apache.james.mime4j.message.Message.<init>(Message.java:141)
>  at org.apache.james.mime4j.message.Message.<init>(Message.java:100)
> always AFTER the MimeStreamParser got invoked with our content handlers (so, not on our parsing, but eg. later, on normal 
> new Message(InputStream).
> Looked like we are currupting something inside MimeStreamParser.
> Then I noticed that DelegatingFieldParser.defaultParser is defined as:
> private FieldParser defaultParser = UnstructuredField.PARSER;
> while other field parsers have:
> static final FieldParser PARSER = new FieldParser() {...}
> So I changed DelegatingFieldParser.defaultParser to static final  and the NPEs do not occur anymore.
> I do not undestand exactly why this happens, but that are the facts.
> Is there any reason why DelegatingFieldParser.defaultParser can't be static final?

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (MIME4J-139) DelegatingFieldParser NPE problem

Posted by "Oleg Kalnichevski (JIRA)" <mi...@james.apache.org>.
    [ https://issues.apache.org/jira/browse/MIME4J-139?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12773240#action_12773240 ] 

Oleg Kalnichevski commented on MIME4J-139:
------------------------------------------

I can't see how #getParser could possibly return null, other than due to a bug in JVM. Anyway, we ought to make both parsers and defaultParser instance variables final just in case.

Oleg

> DelegatingFieldParser NPE problem
> ---------------------------------
>
>                 Key: MIME4J-139
>                 URL: https://issues.apache.org/jira/browse/MIME4J-139
>             Project: JAMES Mime4j
>          Issue Type: Bug
>    Affects Versions: 0.6
>         Environment: jre 1.6.11
>            Reporter: Andrzej Rusin
>            Priority: Minor
>   Original Estimate: 0.08h
>  Remaining Estimate: 0.08h
>
> We have custom ContentHandlers that extend AbstractContentHandler and add some custom Fields that extend AbstractField.
> We were getting:
> java.lang.NullPointerException
>  at org.apache.james.mime4j.field.DelegatingFieldParser.parse(DelegatingFieldParser.java:51)
>  at org.apache.james.mime4j.field.AbstractField.parse(AbstractField.java:171)
>  at org.apache.james.mime4j.field.AbstractField.parse(AbstractField.java:63)
>  at org.apache.james.mime4j.message.MessageBuilder.field(MessageBuilder.java:101)
>  at org.apache.james.mime4j.parser.MimeStreamParser.parse(MimeStreamParser.java:121)
>  at org.apache.james.mime4j.message.Message.<init>(Message.java:141)
>  at org.apache.james.mime4j.message.Message.<init>(Message.java:100)
> always AFTER the MimeStreamParser got invoked with our content handlers (so, not on our parsing, but eg. later, on normal 
> new Message(InputStream).
> Looked like we are currupting something inside MimeStreamParser.
> Then I noticed that DelegatingFieldParser.defaultParser is defined as:
> private FieldParser defaultParser = UnstructuredField.PARSER;
> while other field parsers have:
> static final FieldParser PARSER = new FieldParser() {...}
> So I changed DelegatingFieldParser.defaultParser to static final  and the NPEs do not occur anymore.
> I do not undestand exactly why this happens, but that are the facts.
> Is there any reason why DelegatingFieldParser.defaultParser can't be static final?

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (MIME4J-139) DelegatingFieldParser NPE problem

Posted by "Markus Wiederkehr (JIRA)" <mi...@james.apache.org>.
    [ https://issues.apache.org/jira/browse/MIME4J-139?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12773267#action_12773267 ] 

Markus Wiederkehr commented on MIME4J-139:
------------------------------------------

A JVM bug could be possible since Andrzej specified JRE 1.6.11 as the execution environment..

@Andrzej: Could you please upgrade to the latest JRE 6 Update 17 and check if the problem persists?

> DelegatingFieldParser NPE problem
> ---------------------------------
>
>                 Key: MIME4J-139
>                 URL: https://issues.apache.org/jira/browse/MIME4J-139
>             Project: JAMES Mime4j
>          Issue Type: Bug
>    Affects Versions: 0.6
>         Environment: jre 1.6.11
>            Reporter: Andrzej Rusin
>            Priority: Minor
>   Original Estimate: 0.08h
>  Remaining Estimate: 0.08h
>
> We have custom ContentHandlers that extend AbstractContentHandler and add some custom Fields that extend AbstractField.
> We were getting:
> java.lang.NullPointerException
>  at org.apache.james.mime4j.field.DelegatingFieldParser.parse(DelegatingFieldParser.java:51)
>  at org.apache.james.mime4j.field.AbstractField.parse(AbstractField.java:171)
>  at org.apache.james.mime4j.field.AbstractField.parse(AbstractField.java:63)
>  at org.apache.james.mime4j.message.MessageBuilder.field(MessageBuilder.java:101)
>  at org.apache.james.mime4j.parser.MimeStreamParser.parse(MimeStreamParser.java:121)
>  at org.apache.james.mime4j.message.Message.<init>(Message.java:141)
>  at org.apache.james.mime4j.message.Message.<init>(Message.java:100)
> always AFTER the MimeStreamParser got invoked with our content handlers (so, not on our parsing, but eg. later, on normal 
> new Message(InputStream).
> Looked like we are currupting something inside MimeStreamParser.
> Then I noticed that DelegatingFieldParser.defaultParser is defined as:
> private FieldParser defaultParser = UnstructuredField.PARSER;
> while other field parsers have:
> static final FieldParser PARSER = new FieldParser() {...}
> So I changed DelegatingFieldParser.defaultParser to static final  and the NPEs do not occur anymore.
> I do not undestand exactly why this happens, but that are the facts.
> Is there any reason why DelegatingFieldParser.defaultParser can't be static final?

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (MIME4J-139) DelegatingFieldParser NPE problem

Posted by "Stefano Bagnara (JIRA)" <mi...@james.apache.org>.
    [ https://issues.apache.org/jira/browse/MIME4J-139?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12773299#action_12773299 ] 

Stefano Bagnara commented on MIME4J-139:
----------------------------------------

IMO this is not a JRE bug, but simply a tricky java language specification behaviour.

It seems easy to reproduce:
------------
public class DefaultFieldParserTest extends TestCase {
	
    private static final DefaultFieldParser parser = new DefaultFieldParser();
    
	public void testTest() throws MimeException {
		AbstractField.parse("Something: some\r\n");
	}

}
------------
java.lang.NullPointerException
	at org.apache.james.mime4j.field.DelegatingFieldParser.parse(DelegatingFieldParser.java:51)
	at org.apache.james.mime4j.field.AbstractField.parse(AbstractField.java:171)
	at org.apache.james.mime4j.field.AbstractField.parse(AbstractField.java:90)
	at org.apache.james.mime4j.field.DefaultFieldParserTest.testTest(DefaultFieldParserTest.java:12)


IMHO it simply something to do with initialization and statics. Is the standard chicken vs egg issue.
When from static code you call constructors from other classes you have to remember that private variable instances have not yet initialized yet. Usually this kind of bug is eved more difficult to find because you don't get an NPE but you simply set a value in the instance and then the class initialization will overwrite your value. At least here we see a clear NPE.

Let's analyze it:
---
    public DelegatingFieldParser() {
    	System.out.println("I: "+defaultParser+"|"+parsers);
    	try {
			throw new Exception();
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
---

and here is the output:
-----
java.lang.Exception
	at org.apache.james.mime4j.field.DelegatingFieldParser.<init>(DelegatingFieldParser.java:36)
I: null|{}
	at org.apache.james.mime4j.field.DefaultFieldParser.<init>(DefaultFieldParser.java:24)
	at org.apache.james.mime4j.field.AbstractField.<clinit>(AbstractField.java:38)
	at org.apache.james.mime4j.field.DelegatingFieldParser.<init>(DelegatingFieldParser.java:30)
	at org.apache.james.mime4j.field.DefaultFieldParser.<init>(DefaultFieldParser.java:24)
	at org.apache.james.mime4j.field.DefaultFieldParserTest.testTest(DefaultFieldParserTest.java:11)
-----
So when DefaultFieldParser is initialized in fact we have a nested initialization
During initialization DefaultFieldParser needs UnstructuredField.PARSER , so UnstructuredField must be initialized and it extends AbstractField. AbstractField declares a static final DefaultFieldParser 

Mixing static and not static code is a PITA, and this NPE is just a tip of the iceberg of issues related with class initialization:
http://java.sun.com/docs/books/jvms/second_edition/html/Concepts.doc.html#19075

> DelegatingFieldParser NPE problem
> ---------------------------------
>
>                 Key: MIME4J-139
>                 URL: https://issues.apache.org/jira/browse/MIME4J-139
>             Project: JAMES Mime4j
>          Issue Type: Bug
>    Affects Versions: 0.6
>         Environment: jre 1.6.11
>            Reporter: Andrzej Rusin
>            Priority: Minor
>   Original Estimate: 0.08h
>  Remaining Estimate: 0.08h
>
> We have custom ContentHandlers that extend AbstractContentHandler and add some custom Fields that extend AbstractField.
> We were getting:
> java.lang.NullPointerException
>  at org.apache.james.mime4j.field.DelegatingFieldParser.parse(DelegatingFieldParser.java:51)
>  at org.apache.james.mime4j.field.AbstractField.parse(AbstractField.java:171)
>  at org.apache.james.mime4j.field.AbstractField.parse(AbstractField.java:63)
>  at org.apache.james.mime4j.message.MessageBuilder.field(MessageBuilder.java:101)
>  at org.apache.james.mime4j.parser.MimeStreamParser.parse(MimeStreamParser.java:121)
>  at org.apache.james.mime4j.message.Message.<init>(Message.java:141)
>  at org.apache.james.mime4j.message.Message.<init>(Message.java:100)
> always AFTER the MimeStreamParser got invoked with our content handlers (so, not on our parsing, but eg. later, on normal 
> new Message(InputStream).
> Looked like we are currupting something inside MimeStreamParser.
> Then I noticed that DelegatingFieldParser.defaultParser is defined as:
> private FieldParser defaultParser = UnstructuredField.PARSER;
> while other field parsers have:
> static final FieldParser PARSER = new FieldParser() {...}
> So I changed DelegatingFieldParser.defaultParser to static final  and the NPEs do not occur anymore.
> I do not undestand exactly why this happens, but that are the facts.
> Is there any reason why DelegatingFieldParser.defaultParser can't be static final?

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Resolved: (MIME4J-139) DelegatingFieldParser NPE problem

Posted by "Markus Wiederkehr (JIRA)" <mi...@james.apache.org>.
     [ https://issues.apache.org/jira/browse/MIME4J-139?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Markus Wiederkehr resolved MIME4J-139.
--------------------------------------

       Resolution: Fixed
    Fix Version/s: 0.7
         Assignee: Markus Wiederkehr

Changed defaultParser into a private static final variable; this should resolve the issue.

> DelegatingFieldParser NPE problem
> ---------------------------------
>
>                 Key: MIME4J-139
>                 URL: https://issues.apache.org/jira/browse/MIME4J-139
>             Project: JAMES Mime4j
>          Issue Type: Bug
>    Affects Versions: 0.6
>         Environment: jre 1.6.11
>            Reporter: Andrzej Rusin
>            Assignee: Markus Wiederkehr
>            Priority: Minor
>             Fix For: 0.7
>
>   Original Estimate: 0.08h
>  Remaining Estimate: 0.08h
>
> We have custom ContentHandlers that extend AbstractContentHandler and add some custom Fields that extend AbstractField.
> We were getting:
> java.lang.NullPointerException
>  at org.apache.james.mime4j.field.DelegatingFieldParser.parse(DelegatingFieldParser.java:51)
>  at org.apache.james.mime4j.field.AbstractField.parse(AbstractField.java:171)
>  at org.apache.james.mime4j.field.AbstractField.parse(AbstractField.java:63)
>  at org.apache.james.mime4j.message.MessageBuilder.field(MessageBuilder.java:101)
>  at org.apache.james.mime4j.parser.MimeStreamParser.parse(MimeStreamParser.java:121)
>  at org.apache.james.mime4j.message.Message.<init>(Message.java:141)
>  at org.apache.james.mime4j.message.Message.<init>(Message.java:100)
> always AFTER the MimeStreamParser got invoked with our content handlers (so, not on our parsing, but eg. later, on normal 
> new Message(InputStream).
> Looked like we are currupting something inside MimeStreamParser.
> Then I noticed that DelegatingFieldParser.defaultParser is defined as:
> private FieldParser defaultParser = UnstructuredField.PARSER;
> while other field parsers have:
> static final FieldParser PARSER = new FieldParser() {...}
> So I changed DelegatingFieldParser.defaultParser to static final  and the NPEs do not occur anymore.
> I do not undestand exactly why this happens, but that are the facts.
> Is there any reason why DelegatingFieldParser.defaultParser can't be static final?

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (MIME4J-139) DelegatingFieldParser NPE problem

Posted by "Markus Wiederkehr (JIRA)" <mi...@james.apache.org>.
    [ https://issues.apache.org/jira/browse/MIME4J-139?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12773083#action_12773083 ] 

Markus Wiederkehr commented on MIME4J-139:
------------------------------------------

I think it should not be a problem to make defaultParser a static final member of DelegatingFieldParser. After all it gets initialized with UnstructuredField.PARSER which is a static final variable itself..

But it would still be good to understand how and why the problem occurs. I simply don't see how the local variable 'parser' can ever be null in DelegatingFieldParser line 51. Method getParser() is not overwritten and defaultParser does not seem to get modified anywhere.

Any ideas anyone?

> DelegatingFieldParser NPE problem
> ---------------------------------
>
>                 Key: MIME4J-139
>                 URL: https://issues.apache.org/jira/browse/MIME4J-139
>             Project: JAMES Mime4j
>          Issue Type: Bug
>    Affects Versions: 0.6
>         Environment: jre 1.6.11
>            Reporter: Andrzej Rusin
>            Priority: Minor
>   Original Estimate: 0.08h
>  Remaining Estimate: 0.08h
>
> We have custom ContentHandlers that extend AbstractContentHandler and add some custom Fields that extend AbstractField.
> We were getting:
> java.lang.NullPointerException
>  at org.apache.james.mime4j.field.DelegatingFieldParser.parse(DelegatingFieldParser.java:51)
>  at org.apache.james.mime4j.field.AbstractField.parse(AbstractField.java:171)
>  at org.apache.james.mime4j.field.AbstractField.parse(AbstractField.java:63)
>  at org.apache.james.mime4j.message.MessageBuilder.field(MessageBuilder.java:101)
>  at org.apache.james.mime4j.parser.MimeStreamParser.parse(MimeStreamParser.java:121)
>  at org.apache.james.mime4j.message.Message.<init>(Message.java:141)
>  at org.apache.james.mime4j.message.Message.<init>(Message.java:100)
> always AFTER the MimeStreamParser got invoked with our content handlers (so, not on our parsing, but eg. later, on normal 
> new Message(InputStream).
> Looked like we are currupting something inside MimeStreamParser.
> Then I noticed that DelegatingFieldParser.defaultParser is defined as:
> private FieldParser defaultParser = UnstructuredField.PARSER;
> while other field parsers have:
> static final FieldParser PARSER = new FieldParser() {...}
> So I changed DelegatingFieldParser.defaultParser to static final  and the NPEs do not occur anymore.
> I do not undestand exactly why this happens, but that are the facts.
> Is there any reason why DelegatingFieldParser.defaultParser can't be static final?

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (MIME4J-139) DelegatingFieldParser NPE problem

Posted by "Andrzej Rusin (JIRA)" <mi...@james.apache.org>.
    [ https://issues.apache.org/jira/browse/MIME4J-139?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12773397#action_12773397 ] 

Andrzej Rusin commented on MIME4J-139:
--------------------------------------

@Markus: my friend had very similar problems on JRE 6 Update 16 when he was creating our custom Handlers and Fields. 
My target env is still .11, so even if I upgrade on local and it helps, the problem will persist in the target env.

@Stefano: I think you are right.

Another hint which may or may not be helpful: I am running on local from inside Eclipse, so it may or may not be a classloader problem.

But, if only the fields staticness is consistent, the problem disappears.

> DelegatingFieldParser NPE problem
> ---------------------------------
>
>                 Key: MIME4J-139
>                 URL: https://issues.apache.org/jira/browse/MIME4J-139
>             Project: JAMES Mime4j
>          Issue Type: Bug
>    Affects Versions: 0.6
>         Environment: jre 1.6.11
>            Reporter: Andrzej Rusin
>            Priority: Minor
>   Original Estimate: 0.08h
>  Remaining Estimate: 0.08h
>
> We have custom ContentHandlers that extend AbstractContentHandler and add some custom Fields that extend AbstractField.
> We were getting:
> java.lang.NullPointerException
>  at org.apache.james.mime4j.field.DelegatingFieldParser.parse(DelegatingFieldParser.java:51)
>  at org.apache.james.mime4j.field.AbstractField.parse(AbstractField.java:171)
>  at org.apache.james.mime4j.field.AbstractField.parse(AbstractField.java:63)
>  at org.apache.james.mime4j.message.MessageBuilder.field(MessageBuilder.java:101)
>  at org.apache.james.mime4j.parser.MimeStreamParser.parse(MimeStreamParser.java:121)
>  at org.apache.james.mime4j.message.Message.<init>(Message.java:141)
>  at org.apache.james.mime4j.message.Message.<init>(Message.java:100)
> always AFTER the MimeStreamParser got invoked with our content handlers (so, not on our parsing, but eg. later, on normal 
> new Message(InputStream).
> Looked like we are currupting something inside MimeStreamParser.
> Then I noticed that DelegatingFieldParser.defaultParser is defined as:
> private FieldParser defaultParser = UnstructuredField.PARSER;
> while other field parsers have:
> static final FieldParser PARSER = new FieldParser() {...}
> So I changed DelegatingFieldParser.defaultParser to static final  and the NPEs do not occur anymore.
> I do not undestand exactly why this happens, but that are the facts.
> Is there any reason why DelegatingFieldParser.defaultParser can't be static final?

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.