Following is the application any pointers where the error is
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan
@SpringBootApplication
public class SpringAutoWApplication {
public static void main(String[] args) {
SpringApplication.run(SpringAutoWApplication.class, args);
DestClass dc = new DestClass();
dc.showTestWired();
}
}
import org.springframework.beans.factory.annotation.Autowired;
@Component
public class DestClass {
@Autowired
private TestWired testWired;
public void showTestWired() {
System.out.println(testWired.getTestInt());
}
}
Class to be autowired
import org.springframework.stereotype.Component;
@Component
public class TestWired {
int testInt = 234;
public int getTestInt() {
return testInt;
}
public void setTestInt(int testInt) {
this.testInt = testInt;
}
}
Error:
Exception in thread "main" java.lang.NullPointerException
at DestClass.showTestWired(DestClass.java:13)
the line where i am trying to call the print the int is returning a null pointer exception
Answer: After few hours of looking around this is the way it works, as I cannot post my answer I am doing it here
public class SpringAutoWApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SpringAutoWApplication.class, args);
DestClass dc = context.getBean(DestClass.class);
dc.showTestWired();
}
}