Data recovery of an object [closed]

0

I have 2 screens in Swing, one that calls a method and registers the information in an array, and another screen that should fetch one of this information and display it, the problem is that since the objects are different, I can not get the data of the object that it registers.

Is there any way you can get the address of the object you are registering to use on the search screen and can extract the information from this array or do something like this?

NOTE: It has to be with array, I can not use db or external files.

An example of a Registration screen:

public void formularioCadastro() {

        Alunos cadastrarAlunos = new Alunos();
        // Cadastra os nomes numa Array
        cadastrarAlunos.setNome("Maria", 0); // Nome, posição da Array
        cadastrarAlunos.setNome("Lindovania", 1); // Nome, posição da Array
        cadastrarAlunos.setNome("Jose", 2); // Nome, posição da Array
        cadastrarAlunos.setNome("Josefina", 3); // Nome, posição da Array
        cadastrarAlunos.setNome("Maristela", 4); // Nome, posição da Array
}

Search Screen:

public void formularioBusca() {

        BuscarAluno buscarNome = new BuscarAluno();
        buscarNome.buscar("Jose");
}
What happens is the following, if you run this code, it will give NullPointerException, because the data registered in the "Register Screen" is in the "registerAlunos" object. In order to search the values that were registered there, you would have to use the same object, which in this case is not possible because they are two different screens (files).

    
asked by anonymous 21.02.2015 / 05:12

1 answer

1

I think it's worth unifying your Alunos classes and your BuscarAluno class, since they both seem to represent the same concept, which is your student database, even though this database is just an array in memory.

Once this is done, both screens have to use the same instance of your database (let's say it's the Alunos class). The simplest way to do this would be to instantiate it on the main screen and pass it as the parameter of the constructor of your classes representing the cadatsro and search screens.

If this is not clear enough, please edit your question by putting more code as it is difficult to help without having it. You can not guess how your code is.

    
22.02.2015 / 15:33