0

UPDATED

I found a problem using Gson library using nested classes. When Gson build the nested object leaves the reference of the inner class to the outer class this$0 to null making any reference to the outer class produce an NullPointerException (below a complete example). Is this known Gson issue with solution?

import com.google.gson.Gson;


public class Outer {


    protected Inner inner = null;


    public Inner getInner() {
        return inner;
    }

    protected void something(){

    }

    public void function(){

       inner.innerFunction();
    }

    public class Inner {
        public String name = null;

        protected void innerFunction(){

            something();//exception source
        }
    }
    public static void main(String[] args){
        String str = "{\"inner\":{\"name\"=\"test\"}}";
        Outer outer = (new Gson()).fromJson(str,Outer.class);
        System.out.println(outer.getInner().name);
        outer.function(); // exception here
    }
}
ender.an27
  • 703
  • 1
  • 13
  • 35
  • I don't know what `setAttributes` does, and assuming `Hashtable` is `java.util.Hashtable`, those other two lines cannot throw `NullPointerException`s. Post an MCVE if is reproducible. – Sotirios Delimanolis Nov 19 '15 at 16:55
  • @SotiriosDelimanolis is marked as duplicated? the question is refered as duplicated do not speak about inner and outer class just about null pointers in general. And the code triggers the exception all three lines! – ender.an27 Nov 19 '15 at 16:59
  • The duplicate is known as a canonical post. It explains what a `NullPointerException` is and how to find it and fix it. We're not your personal debuggers. – Sotirios Delimanolis Nov 19 '15 at 17:00
  • If you're seeing a `NullPointerException` that you can't explain post all and only the code necessary to reproduce it. We're not going to start guessing. – Sotirios Delimanolis Nov 19 '15 at 17:00
  • Well then marked as incomplete, but not duplicated. This is not related to this NullPointerException post – ender.an27 Nov 19 '15 at 17:02
  • You'd think so, but you'd be wrong. That's what canonicals are. – Sotirios Delimanolis Nov 19 '15 at 17:02
  • You are wrong! I think I found the problem. The object has been constructed by Gson who do leve the this$0 pointer to `null`. Again you are wrong to mark this as doplicated! – ender.an27 Nov 19 '15 at 17:32
  • I don't know how else to tell you this. If you have a specific problem, post it. You posted an incomplete question. We're not here to debug for you. If we can't reproduce your problem, the best we can do is point you to a place that shows you how to find the problem and fix it. – Sotirios Delimanolis Nov 19 '15 at 17:34
  • If you have a problem with this, take it to Meta. – Sotirios Delimanolis Nov 19 '15 at 17:34
  • You are wrong! I think I found the problem. The object has been constructed by Gson who do leve the this$0 pointer to `null`. Again you are wrong to mark this as duplicated! – ender.an27 Nov 19 '15 at 17:35
  • Something none of us could've found with the information provided in your question. – Sotirios Delimanolis Nov 19 '15 at 17:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95604/discussion-between-ender-an27-and-sotirios-delimanolis). – ender.an27 Nov 19 '15 at 17:38
  • The question was incomplete! I agree with this since the very beginning! if you had not marked as duplicated I have made complete it. But you unfairly marked as incompleate! which is my point! – ender.an27 Nov 19 '15 at 17:44
  • You can still edit your question. We can still vote to reopen. Relax. – Sotirios Delimanolis Nov 19 '15 at 17:49
  • @ender.an27 yep, looks like its incomplete. Don't quite regard it as a duplicate just yet... don't have enough information to make that judgement on account of the question being incomplete :) The information missing, really, is which line the error is springing from, and if there is additional code that is being executed to cause the error. If the question is completed, I'd be happy to vote for re-opening. – jrharshath Nov 19 '15 at 17:51
  • @jrharshath I updated the question! – ender.an27 Nov 19 '15 at 18:08

0 Answers0