弹簧自动装配的NullPointerException

时间:2016-06-04 18:54:48

标签: java spring dependency-injection

我为Spring自动装配和获取NullPointerException写了一个练习题。 这是我的代码  界面:形状 公共界面Shape {     public void draw(); } 类:三角形     公共类Triangle实现Shape {     私人A点;     私人B点;     私人C点;     public Point getA(){         返回A;     }     public void setA(Point a){         A = a;     }     public Point getB(){         返回B;     }     public void setB(Point b){         B = b;     }     public Point getC(){         返回C;     }     public void setC(Point c){         C = c;     }     public void draw(){         System.out.println(" PointA(" + A.getX()+"," + A.getY()+")");         System.out.println(" PointB(" + B.getX()+"," + B.getY()+")");         System.out.println(" PointC(" + C.getX()+"," + C.getY()+")");     } } 分类:积分 公共课点{     private int x,y;     public int getX(){         返回x;     }     public void setX(int x){         this.x = x;     }     public int getY(){         回归y;     }     public void setY(int y){         this.y = y;     } } Spring XML <?xml version =" 1.0"编码=" 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-3.0.xsd">     < bean id =" triangle"类=" org.demo.javabrains.Triangle"自动装配="绰号">     < /豆腐>     < bean id =" A"类=" org.demo.javabrains.Point">         < property name =" x"值=#&34; 0" />         < property name =" y"值=#&34; 0" />     < /豆腐>     < bean id =" B"类=" org.demo.javabrains.Point">         < property name =" x"值=#&34; 10" />         < property name =" y"值=#&34; 20" />     < /豆腐>     < bean id =" C"类=" org.demo.javabrains.Point">         < property name =" x"值=#&34; 30" />         < property name =" y"值=#&34; 40" />     < /豆腐> < /豆类> 主类 公共类DrawingApp {     public static void main(String [] args){         ApplicationContext context = new ClassPathXmlApplicationContext(" spring.xml");         形状三角形=(形状)context.getBean("三角形");         triangle.draw();     } } 我不知道为什么我会得到NullPointer Exception 当我尝试运行此代码时,它会抛出异常 信息:从类路径资源[spring.xml]加载XML bean定义 线程" main"中的例外情况显示java.lang.NullPointerException     在org.demo.javabrains.Triangle.draw(Triangle.java:34)     在org.demo.javabrains.DrawingApp.main(DrawingApp.java:16) A点被赋值为null,不知道为什么

3 个答案:

答案 0 :(得分:1)

当您使用按物业名称自动装配时

<bean id="triangle" class="org.demo.javabrains.Triangle" autowire="byName" />

Spring查找与需要自动装配的属性同名的bean。例如,如果bean定义设置为autowire byName,并且它包含名为a的属性,即它具有setA(..)方法(不要与名为{{的字段混淆) 1}}),Spring查找名为A的bean定义,并使用它来设置属性。

正如@Tunaki在评论中正确指出的那样,您将属性名称与字段本身的名称混淆为a。无论如何,如果你使用A bean的小写名称,你会没事的:

Point

有关在Spring中自动装配协作者的更详细讨论,请查看https://yoursite.com

答案 1 :(得分:0)

Triangle .draw()方法使用Point类对象。您已在xml中声明了Point对象,但没有使用它来注入三角形bean定义。这就是它给出空指针异常的原因,因为它没有初始化。

Spring XML中的三角形bean应该像

<bean id="triangle" class="org.demo.javabrains.Triangle" autowire="byName">
 <property name="A" ref="A"/>
 <property name="B" ref="B"/>
 <property name="B" ref="B"/>
</bean>

答案 2 :(得分:0)

尽量不要将大写字母用于bean名称(第一个单词),也尽量不设置以大写字母开头的对象变量,这将导致空指针异常。

//These are the bean definitions
<bean name="MyClassA" id="Aclass" class="com.test.autowire.MyClassA" autowire="byName"></bean>
<bean name="bClass" id="bClass" class="com.test.autowire.MyClassB"></bean>



public class MyClassA {
private MyClassB bClass; 
//donot use BClass, use the first letter as lowercase eg: bClass

public MyClassB getbClass() {
    return bClass;
}

public void setbClass(MyClassB bClass) {
    this.bClass = bClass;
}

public void printInA(){
    bClass.print();
    bClass.disp();
}
}

public class MyClassB {
MyClassB(){
    System.out.println("THis is my Class B default Constrctr");
}
void print(){
    System.out.println("This is a print");
}
void disp(){
    System.out.println("This is disp methd");
}
}