0

I've a bean named textFileWriter to write string entities to HDFS. I've configured the spring bean in bean config file. While executing am getting NullPointerException. Please help me on this.

My bean configuration :-

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/hadoop"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:hdp="http://www.springframework.org/schema/hadoop" 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
    http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd">


    <hdp:configuration id="hadoopConfigBean">
        fs.defaultFS=${hdp.fs}
    </hdp:configuration>


    <beans:bean id="textFileWriter"
        class="org.springframework.data.hadoop.store.output.TextFileWriter">
        <beans:constructor-arg index="0" ref="hadoopConfigBean"></beans:constructor-arg>
        <beans:constructor-arg index="1"
            type="org.apache.hadoop.fs.Path" value="/user/mhduser"></beans:constructor-arg>
        <beans:constructor-arg index="2" type="org.springframework.data.hadoop.store.codec.CodecInfo" >
        <beans:null></beans:null>
        </beans:constructor-arg>
    </beans:bean>

    <context:property-placeholder location="hadoop-configs.properties" />
</beans:beans>

Main Class :-

public class MainApp {
    @Autowired
    TextFileWriter textFileWriter;

    public static void main(String[] args) {
        AbstractApplicationContext context = new ClassPathXmlApplicationContext(
                "/META-INF/spring/application-context.xml", MainApp.class); 
        System.out.println("Context loaded...");
        MainApp obj=new MainApp();
        obj.someMethod();

        context.registerShutdownHook();

    }

    private void someMethod() {
        try {
            textFileWriter.write("hi there");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

Am getting null pointer exception in this line :-

textFileWriter.write("hi there");
Sachin
  • 1,675
  • 2
  • 19
  • 42
  • 1
    Possible duplicate of [Why is my Spring @Autowired field null?](http://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null) – kryger Apr 21 '16 at 09:33

2 Answers2

1

@Autowired won't work here because you are using a new instance of class MainApp which is not managed by spring and that's why you are getting a NullPointerException

    MainApp obj=new MainApp();
    obj.someMethod();

A work around would be :

public class MainApp {

public MainApp(TextFileWriter textFileWriter){
  this.textFileWriter = textFileWriter;
}

public MainApp(){
} 

@Autowired
TextFileWriter textFileWriter;

public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext(
            "/META-INF/spring/application-context.xml"); 
     System.out.println("Context loaded...");

     TextFileWriter textFileWriter = (TextFileWriter )  context.getBean("textFileWriter");
     MainApp obj=new MainApp(textFileWriter );
      obj.someMethod();

     context.registerShutdownHook();

 }

 private void someMethod() {
     try {
         textFileWriter.write("hi there");
     } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     }
 }

}

Rafik BELDI
  • 4,140
  • 4
  • 24
  • 38
1

You can still make Spring inject dependencies without adding MainApp as a bean explicitly.

  1. Enable annotation driven config. Add to the application-context.xml following

    <context:annotation-config />
    
  2. Make Spring wire dependencies with

    MainApp obj=new MainApp();
    context.getAutowireCapableBeanFactory().autowireBean(obj);
    
Evgeny
  • 2,483
  • 1
  • 17
  • 24