0

I got some null pointer exceptions when I try to access the fields from an injected object. I'm going to show a simple example. I'm using lokbok annotations @Getter and @Setter.

I have a simple class.

public class MyClass {

    /*
     * Field
     */
    private @Getter @Setter String myName;

    public MyClass(){

    }
}

Then I another class

public class AnotherClass {

        /*
         * Field
         */
        private MyClass myClass;

        public AnotherClass(){
             ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
             myClass = context.getBean("myClass", MyClass.class);
             ((ConfigurableApplicationContext) context).close();

             myClass.setMyName("Hello");
        }
    }

If I access the field myName from a third class, like this.

public class ThirdClass {

        /*
         * Field
         */
        private MyClass myClass;

        public ThirdClass(){
             ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
             myClass = context.getBean("myClass", MyClass.class);
             ((ConfigurableApplicationContext) context).close();

             System.out.println(myClass.getMyName());
        }
    }

It will print out null as output. I have created my Beans.xml file like this:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- Create my class -->
    <bean id="myClass" class="MyClass"></bean>


</beans>

Questions:

Should it behave like that?

Have I missed something if I don't want to have null?

Software being used:

  • JavaFX 8
  • Java 8
  • Gluon-Ignite Spring 1.0.2
  • Lombok 1.18.8
NotZack
  • 518
  • 2
  • 9
  • 22
euraad
  • 2,467
  • 5
  • 30
  • 51
  • If you want 2 objects to "be connected", they need to be created by the same context object (or context using the original one as parent). Calling `close` will result in `IllegalStateException`s when trying to use the later though. – fabian Aug 19 '19 at 16:59
  • @fabian How can I connect the same context object in this case? I thought DI would do the job for me so the fields would have the same address. – euraad Aug 19 '19 at 17:03
  • @fabian I found a solution. Please give me a reply if its a good solution or it can be done better? :) – euraad Aug 19 '19 at 18:31
  • `AnotherClass` and `ThirdClass` should be (prototype) beans. If you do need to manually fill the `myClass` fields instead of injecting the bean, it should also be possible to inject the context... – fabian Aug 19 '19 at 20:37
  • Please don't edit in solutions. Instead, talk to @fabian, have the question reopened so you can add an answer below if you believe it's different than the duplicated answers. – Blue Aug 20 '19 at 07:48
  • @fabian i don't understand. Can you show an example ? – euraad Aug 20 '19 at 09:56
  • There are a ton of tutorials out there. (Comments are insufficient to provide an example anyways.) E.g. [for injecting beans to other beans](https://www.baeldung.com/spring-xml-injection), [Tutorial explaining difference between prototype and singleton](https://www.tutorialspoint.com/spring/spring_bean_scopes.htm) – fabian Aug 20 '19 at 11:19

0 Answers0