Jump to content

Problem with @autowired in Java

I am having a bit of a problem with @autowiring in java spring framework and i tried to write a small logic.

classa having some member variables and a display function (along with a run function if i want to run it as a thread) and the respective set and get methods for the member variables.It worked fine, created a bean with classpathxmlapplicationcontext and printed some values. Now a second Display class that contains a classa object and prints the values by invoking the display of the object.Now this new object of classa inside the new classs is autowired byname but when i try to run it i get Nullpointerexception for this object inside the new display class. why is that?

 

Here are the code and output(contains the exception):-

classa



package com.first.first;

import org.springframework.stereotype.Component;
 
@Component
public class classa implements Runnable{
private int A,B,C;
classa(){
System.out.printf("This class a constructor is called.\n");
}
classa(int a,int b,int c){
this.A=a;
this.B=b;
this.C=c;
}
public void setA(int a){
this.A=a;
}
public void setB(int b){
this.B=b;
}
public void setC(int c){
this.C=c;
}
public int getA(){
return A;
}
public int getC(){
return C;
}
public int getB(){
return B;
}
synchronized public void display_result(){
System.out.println("The values of the variables stored in the class: \n"+A+" "+B+" "+C+" \n");
}
public void run(){
display_result();
}
}

 

display class


package com.first.first;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
@Component
public class Display implements Runnable{
@Autowired private classa A;
public classa getA(){
return A;
}
public void setA(classa a){
this.A=a;
}
public void display(){
System.out.printf("The data contained in the object of classa type:\n");
}
public void run(){
display();
A.display_result();
}
}

Xml config file:


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

<!-- bean definitions here -->
<bean id="A" class="com.first.first.classa">
<property name="A" value="122"></property>
<property name="B" value="70"></property>
<property name="C" value="-89"></property>
</bean>
<bean id="B" class="com.first.first.classa">
<constructor-arg name="a" type="int" value="65"></constructor-arg>
<constructor-arg name="b" type="int" value="-23"></constructor-arg>
<constructor-arg name="c" type="int" value="9"></constructor-arg>
</bean>
<bean id="displayable" class="com.first.first.Display" autowire="byName"></bean>
</beans>

The main progrmme:


package com.first.first;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
@SpringBootApplication
public class FirstApplication {
 
public static void main(String[] args) {
SpringApplication.run(FirstApplication.class, args);
ClassPathXmlApplicationContext context1=new ClassPathXmlApplicationContext("config.xml");
classa obj1=(classa)context1.getBean("A");
// classa obj2=(classa)context1.getBean("B");
Display obj3=(Display) context1.getBean("displayable");
obj1.display_result();
obj3.run();
}
 
}

image.thumb.png.f529aab3bc69afa9dbe9becd37a11ae2.png

Link to comment
Share on other sites

Link to post
Share on other sites

first of all fix the naming, class names start with capitals, variables start with lowercase, then fix formatting

 

now i could be totally wrong but you have 2 bean definitions of the same class (A, B) but when youre autowiring you dont specify which one to use? - not applicable since youre using autowiring by name

 

 

from the looks of it, this is exactly what youre trying to do:

https://springframework.guru/autowiring-in-spring/

MSI GX660 + i7 920XM @ 2.8GHz + GTX 970M + Samsung SSD 830 256GB

Link to comment
Share on other sites

Link to post
Share on other sites

12 hours ago, Neftex said:

first of all fix the naming, class names start with capitals, variables start with lowercase, then fix formatting

 

now i could be totally wrong but you have 2 bean definitions of the same class (A, B) but when youre autowiring you dont specify which one to use?

 

 

from the looks of it, this is exactly what youre trying to do:

https://springframework.guru/autowiring-in-spring/my ba

12 hours ago, Neftex said:

first of all fix the naming, class names start with capitals, variables start with lowercase, then fix formatting

 

now i could be totally wrong but you have 2 bean definitions of the same class (A, B) but when youre autowiring you dont specify which one to use?

 

 

from the looks of it, this is exactly what youre trying to do:

https://springframework.guru/autowiring-in-spring/

My bad on the naming sry. Okay so i have tried using only one bean definition of A but didn't work but with autowire byname it should only pick bean a though

 

Link to comment
Share on other sites

Link to post
Share on other sites

so, did you figure out the reason yet? (hint it has to do with naming, as i already said)

 

working example is in attachments (removed stuff that isnt important for this)

src.zip

MSI GX660 + i7 920XM @ 2.8GHz + GTX 970M + Samsung SSD 830 256GB

Link to comment
Share on other sites

Link to post
Share on other sites

On 2/27/2024 at 11:43 PM, Neftex said:

so, did you figure out the reason yet? (hint it has to do with naming, as i already said)

 

working example is in attachments (removed stuff that isnt important for this)

src.zip 2.88 kB · 1 download

yeah i don't know what happened there but i named the classes a bit different to what u said and maid a new project it worked.Thanks for the help though

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×