Issue when executing spring bean

0 votes
asked Apr 21, 2016 by sachin

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");

2 Answers

+1 vote
answered Apr 21, 2016 by evgeny

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);
+1 vote
answered Nov 29 by rafik-beldi

@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(); } }

}

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
...