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");
You can still make Spring inject dependencies without adding MainApp
as a bean explicitly.
Enable annotation driven config. Add to the application-context.xml
following
<context:annotation-config />
Make Spring wire dependencies with
MainApp obj=new MainApp();
context.getAutowireCapableBeanFactory().autowireBean(obj);