[Logo] Terracotta Discussion Forums
  [Search] Search   [Recent Topics] Recent Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
[Expert]
NPE accessing fields in outer class  XML
Forum Index -> Terracotta Platform
Author Message
fanguad

journeyman

Joined: 08/26/2008 06:06:46
Messages: 21
Offline

I'll start out with the top of the stack trace
Code:
 java.lang.NullPointerException
 	at org.nekocode.modelhelperinterface.process.ProcessSpecNode.access$100(ProcessSpecNode.java:53)
 	at org.nekocode.modelhelperinterface.process.ProcessSpecNode$ProcessSpecNodeAllenRelationMap.getSuccessorLinks(ProcessSpecNode.java:673)
 


Line 53 is the class definition. Line 673 is inside an inner class that is accessing a field of the outer class (the same thing happens if I try to access a method of the outer class). It looks like this access() method is part of the instrumented code. Any ideas?

I tried to replicate this behavior in a small test case, but failed.
tgautier

seraphim

Joined: 06/05/2006 12:19:26
Messages: 1781
Offline

Can you post your tc-config.xml?

This could be an uninstrumented gotcha, if the outer class is not intentionally being clustered along with the inner class, and/or it is not instrumented.

http://www.terracotta.org/web/display/docs/Gotchas

[WWW]
fanguad

journeyman

Joined: 08/26/2008 06:06:46
Messages: 21
Offline

Both inner and outer are instrumented, and I'm pretty sure the outer class is being clustered.

Is it possible this could occur if I didn't acquire a read lock before accessing the values?
tgautier

seraphim

Joined: 06/05/2006 12:19:26
Messages: 1781
Offline

Yes definitely.
[WWW]
fanguad

journeyman

Joined: 08/26/2008 06:06:46
Messages: 21
Offline

Ok, I've tried acquiring a read lock on the object and its outer class.
Code:
 class Outer {
     class Inner {
         void methodA {
             getParent().toString();
         }
         Outer getParent() {
             return Outer.this;
         }
     }
 }
 

I have a TC lock (read) on getParent, but the method still returns null, causing an NPE in methodA. Any ideas?
tgautier

seraphim

Joined: 06/05/2006 12:19:26
Messages: 1781
Offline

Locks only matter if you have synchronization and the object is clustered. Maybe you can post a sample that demonstrates the problem?
[WWW]
fanguad

journeyman

Joined: 08/26/2008 06:06:46
Messages: 21
Offline

I added an explicit pointer from the inner object to the outer object and the null pointer went away, so I guess the problem was that the outer object wasn't clustered.

Why is this possible? Isn't there an implicit pointer from the inner object to the outer?
tgautier

seraphim

Joined: 06/05/2006 12:19:26
Messages: 1781
Offline

Should be - can you post code that demonstrates the problem?
[WWW]
fanguad

journeyman

Joined: 08/26/2008 06:06:46
Messages: 21
Offline

Unfortunately, I haven't been able to duplicate this behavior in a small test case. In the small cases, TC behaves as I expect it to (a pointer from the inner class to the outer class). That probably means there's some other factor influencing this behavior.

Here's my test case, anyway:
Code:
 import java.util.HashMap;
 import java.util.Map;
 
 public class InnerClassExample {
 	
 	static InnerClassExample example;
 	
 	public static void main(String[] args) {
 		if (example == null) {
 			example = new InnerClassExample();
 			System.out.println("initializing root");
 			OuterClass outer = new OuterClass();
 			example.addToRoot("inner", outer.inner);
 		}
 		
 		System.out.println(example.root.get("inner"));
 	}
 	
 	public Map root;
 	InnerClassExample() {
 		root = new HashMap();
 	}
 
 	void addToRoot(String key, Object value) {
 		root.put(key, value);
 	}
 
 	private static class OuterClass {
 		private final String name;
 		private InnerClass inner;
 
 		public OuterClass() {
 			name = "outer";
 			inner = new InnerClass();
 		}
 
 		public String toString() {
 			return name;
 		}
 
 		private class InnerClass {
 
 			private String innerName;
 
 			public InnerClass() {
 				innerName = "inner";
 			}
 
 			public String toString() {
 				return name + " " + innerName;
 			}
 		}
 	}
 }
 

and tc-config.xml
Code:
 <?xml version="1.0" encoding="UTF-8"?>
 <con:tc-config xmlns:con="http://www.terracotta.org/config">
   <servers>
     <server host="%i" name="terracotta_example">
       <data>terracotta/server-data</data>
       <logs>terracotta/server-logs</logs>
       <statistics>terracotta/cluster-statistics</statistics>
     </server>
     <update-check>
       <enabled>false</enabled>
     </update-check>
   </servers>
   <clients>
     <logs>terracotta/client-logs</logs>
     <statistics>terracotta/client-statistics/%D</statistics>
   </clients>
   <application>
     <dso>
       <instrumented-classes>
         <include>
           <class-expression>*..*</class-expression>
         </include>
       </instrumented-classes>
       <roots>
         <root>
           <field-name>InnerClassExample.example</field-name>
         </root>
       </roots>
       <locks>
         <autolock auto-synchronized="true">
           <method-expression>void InnerClassExample.addToRoot(java.lang.String, java.lang.Object)</method-expression>
           <lock-level>write</lock-level>
         </autolock>
         <autolock auto-synchronized="true">
           <method-expression>void InnerClassExample.main(..)</method-expression>
           <lock-level>write</lock-level>
         </autolock>
       </locks>
     </dso>
   </application>
 </con:tc-config>
 
 
Forum Index -> Terracotta Platform
Go to:   
Powered by JForum 2.1.7 © JForum Team