Results 1 to 4 of 4

Thread: constructor exception

  1. #1

    Default constructor exception

    Hi,
    I have to use a bean (bean2) in the constructor of another bean (bean1), where bean2 is a property of the bean1, but I got a nullpointer exception thrown by the contructor, like the bean2 wasn't initialized. Looks like I can only use bean2 after the constructor. But what do I have to do to use it in the constructor as I got to.

    Thanks for the help

  2. #2
    Join Date
    Nov 2004
    Location
    Hilversum - The Netherlands
    Posts
    1,054

    Default

    I have left my crystal ball at home today so I think you have to post more information. Please post a small part of you app context where bean1 and 2 are created.

  3. #3

    Default constructor exception

    ok
    here is the essencial java code:

    public class IndexUsuario extends JFrame implements InterfaceIndexUsuario {
    private InterfaceControleLogin controleLogin;
    private InterfaceControleAvaliacao controleAvaliacao;
    private InterfaceAluno aluno;

    // metodo set usado pelo Spring
    public void setInterfaceControleLogin (InterfaceControleLogin controleLogin) {
    this.controleLogin = controleLogin;
    }

    // metodo set usado pelo Spring
    public void setInterfaceControleAvaliacao (InterfaceControleAvaliacao controleAvaliacao) {
    this.controleAvaliacao = controleAvaliacao;
    }

    // metodo set usado pelo Spring
    public void setInterfaceAluno (InterfaceAluno aluno) {
    this.aluno = aluno;
    }

    // constructor
    public IndexUsuario (InterfaceAluno aluno) {
    this.aluno = aluno;

    //here controleAvaliacao is null...
    controleAvaliacao.anyMetod ();

    int larguraJanela = 400; //largura padrao
    int alturaJanela = 250; //altura padrao

    // seta a localizacao da janela
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    int x1 = (screenSize.width - larguraJanela) / 2;
    int y1 = (screenSize.height - alturaJanela) / 2;
    setBounds(x1, y1, larguraJanela, alturaJanela);
    }

    here is the xml config:

    <beans>

    <bean id="indexUsuario" class="visao.IndexUsuario">
    <constructor-arg><ref bean="aluno"/></constructor-arg>
    <property name="interfaceControleLogin"><ref bean="controleLogin"/></property>
    <property name="interfaceControleAvaliacao"><ref bean="controleAvaliacao"/></property>
    <property name="interfaceAluno"><ref bean="aluno"/></property>
    </bean>

    <bean id="aluno" class="modelo.Aluno">
    <property name="interfaceLogin"><ref bean="login"/></property>
    </bean>

    <bean id="controleAvaliacao" class="controle.ControleAvaliacao">
    <property name="interfaceAvaliacao"><ref bean="avaliacao"/></property>
    <property name="interfaceConexao"><ref bean="conexao"/></property>
    </bean>

    <bean id="login" class="modelo.Login"/>
    <bean id="conexao" class="controle.Conexao"/>

    <bean id="avaliacao" class="modelo.Avaliacao"/>

    <bean id="controleLogin" class="controle.ControleLogin">
    <property name="interfaceConexao"><ref bean="conexao"/></property>
    </bean>

    </beans>


    Thanks again for the help

  4. #4
    Join Date
    Nov 2004
    Location
    Hilversum - The Netherlands
    Posts
    1,054

    Default

    The constructor of IndexUsuario is called before the setters are called, so controleAvaliacao.anyMetod (); will give a nullpointerexception because controleAvaliacio isn`t set.

    Why don`t you set all arguments with the constructor? (controleLogin, controleAvaliacao, luno)

    Personally I don`t like setter if a constructor is also possible. With a constructor I can guarantee that all my objects are in a consistent state, and I don`t have the obligation of dealing with setters. In some cases a setter is simple, but sometimes I could have enourmous consequences.... that is why I don`t introduce them if it isn`t required.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •