它一直保持零状态?

时间:2019-03-15 19:30:59

标签: java spring javabeans

我正在使用Spring Framework构建一个项目。在bean中,我创建了类Model,然后在类Menu中使用了它(获取和设置方法)。它一直显示NULL EXCEPTION,我已经测试过了,我的班级是null。为什么,我使用bean创建了它...?救命。在课程Menu line 61

bean:

<?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 id="controller"
    class="com.videogame.invasion.controller.Controller">
    <property name="model" ref="model"></property>
    <property name="frame" ref="frame"></property>
</bean>


<bean id="frame" class="com.videogame.invasion.view.Frame">
    <property name="container" ref="container"></property>
</bean>
**<bean id="container" class="com.videogame.invasion.view.Menu">**
    <property name="play" ref="play"></property>
    <property name="creditsView" ref="creditsView"></property>
    <property name="highScoreView" ref="highScoreView"></property>
    **<property name="model" ref="model"></property>**
</bean>
<bean id="creditsView"
    class="com.videogame.invasion.view.CreditsView">
</bean>
<bean id="highScoreView"
    class="com.videogame.invasion.view.HighScoreView">
</bean>
<bean id="play" class="com.videogame.invasion.view.play.Play">
    <property name="shipsFactory" ref="shipsFactory"></property>
</bean>

<bean id="shipsFactory"
    class="com.videogame.invasion.view.play.ShipsFactory">
    <property name="enemyShip" ref="enemyShip"></property>
    <property name="playerShip" ref="playerShip"></property>
</bean>
<bean id="playerShip"
    class="com.videogame.invasion.view.play.PlayerShip">
</bean>
<bean id="enemyShip"
    class="com.videogame.invasion.view.play.EnemyShip">
</bean>



<bean id="model" class="com.videogame.invasion.model.Model">
    <property name="save" ref="save"></property>
    <property name="load" ref="load"></property>
    <property name="loadHighScore" ref="loadHighScore"></property>
    <property name="saveHighScore" ref="saveHighScore"></property>
</bean>
<bean id="save" class="com.videogame.invasion.model.SaveGame"></bean>
<bean id="load" class="com.videogame.invasion.model.LoadGame"></bean>
<bean id="loadHighScore"
    class="com.videogame.invasion.model.LoadHighScore">
</bean>
<bean id="saveHighScore"
    class="com.videogame.invasion.model.SaveHighScore">
</bean>

类模型,当我启动它时,向我展示System.out.println(“ AAAAAA”)巫婆意味着该类已创建。

菜单:

package com.videogame.invasion.view;


public class Menu extends JPanel implements ActionListener{


private Controller controller;
private Model model;
private CreditsView creditsView;
private HighScoreView highScoreView;
private Play play;

private JButton newGame, loadGame, highScore, credits, exit, back, save;
private int buttonHeight = 20;
private int buttonWidth = 120;

private Image image;


public Menu() {

    setLayout(null);

    setButtons();
}

public void actionPerformed(ActionEvent e) {

    if (e.getSource() == newGame) {

    } else if (e.getSource() == loadGame) {

    } else if (e.getSource() == highScore) {

        System.out.println("test AP MENU");

        if (model == null) {
            System.out.println("model is null");
        }else {
            **model.test();** LINE 61 !!
        }

    } else if (e.getSource() == credits) {

    } else if (e.getSource() == exit) {

    }   

}

public Model getModel() {
    return model;
}

public void setModel(Model model) {
    this.model = model;
}

}

型号:

package com.videogame.invasion.model;

public class Model {
    private LoadGame load;
    private SaveGame save;
    private SaveHighScore saveHighScore;
    private LoadHighScore loadHighScore;

    public Model() {
        System.out.println("AAAAAA");
    }

    public void test() {
        System.out.println("TEST");
    }
}

0 个答案:

没有答案
相关问题