main方法报空指针异常,请大家帮忙看看什么原因。
public class Computer { private Host host;
private Display display;
public void run(){
System.out.println("I am the computer, I am running.");
System.out.println(this.host.run());
System.out.println(this.display.run());
} public Host getHost() {
return host;
} public void setHost(Host host) {
this.host = host;
} public Display getDisplay() {
return display;
} public void setDisplay(Display display) {
this.display = display;
}}
public class Host { public String run(){
return "I am the host, I am running.";
}
}
public class Display { public String run(){
return "I am the Displayer, I am running.";
}
}
package ioc.test;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestIoc {
public static void main(String[] args){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Computer computer1 = (Computer)ac.getBean("computer1");
System.out.println("byName");
computer1.run();
Computer computer2 = (Computer)ac.getBean("computer2");
System.out.println("byType");
computer2.run();
Computer computer3 = (Computer)ac.getBean("computer3");
System.out.println("default");
computer3.run();
}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="computer1" class="ioc.test.Computer" autowire="byName"></bean>
<bean id="computer2" class="ioc.test.Computer" autowire="byType"></bean>
<bean id="computer3" class="ioc.test.Computer" autowire="default"></bean>
</beans>
就这些,大家帮忙看看,谢谢啦。
public class Computer { private Host host;
private Display display;
public void run(){
System.out.println("I am the computer, I am running.");
System.out.println(this.host.run());
System.out.println(this.display.run());
} public Host getHost() {
return host;
} public void setHost(Host host) {
this.host = host;
} public Display getDisplay() {
return display;
} public void setDisplay(Display display) {
this.display = display;
}}
public class Host { public String run(){
return "I am the host, I am running.";
}
}
public class Display { public String run(){
return "I am the Displayer, I am running.";
}
}
package ioc.test;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestIoc {
public static void main(String[] args){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Computer computer1 = (Computer)ac.getBean("computer1");
System.out.println("byName");
computer1.run();
Computer computer2 = (Computer)ac.getBean("computer2");
System.out.println("byType");
computer2.run();
Computer computer3 = (Computer)ac.getBean("computer3");
System.out.println("default");
computer3.run();
}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="computer1" class="ioc.test.Computer" autowire="byName"></bean>
<bean id="computer2" class="ioc.test.Computer" autowire="byType"></bean>
<bean id="computer3" class="ioc.test.Computer" autowire="default"></bean>
</beans>
就这些,大家帮忙看看,谢谢啦。
解决方案 »
- 请教高手一个关于EJB TimerService定时处理的问题。
- ★★★★★★ JAva修改后 class文件怎么没哟变啊 ★★★★★★
- 我是学java ee的,现在想学一个JavaScript的框架,有那么多,请大家推荐一个好吗,谢谢
- mvc接口问题
- JRadioButton 如何取得source呀
- subversion在公司adsl安装后(IP不固定,局域网),如何在家访问?
- 关于控制图片文件上传的问题
- <html:select>取值问题,急!!!!!各位朋友进来帮下忙,不甚感激!!!!
- 500分请教struts问题
- 哪位大侠可以告知小弟……,小弟初学……
- 求助:java.lang.Error:Unresolved compilation problems:
- 用过mule-esb的请进来一下
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="computer1" class="ioc.test.Computer" autowire="byName"></bean>
<bean id="computer2" class="ioc.test.Computer" autowire="byType"></bean>
<bean id="computer3" class="ioc.test.Computer" autowire="default"></bean>
<bean id="host" class="ioc.test.Host" autowire="byName"></bean>
<bean id="display" class="ioc.test.Display" autowire="byName"></bean>
</beans>
但是执行到
Computer computer3 = (Computer)ac.getBean("computer3");
System.out.println("default");
computer3.run();
注入方式是默认的时候还是会报空指针异常,暂时不知道怎么回事。