Retrieving data from an object

Asked

Viewed 133 times

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 in it, the problem is that as the objects are different, I can’t get the data of the object that registers.

Is there any way to get the address of the object you register to use on the search screen and be able to extract the information from that array or do something like?

OBS: it has to be with array, I can’t use db or external files.

An example of a Register 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 "Registration Screen" are in the object "register Students". In order to find the values that were registered there, you would have to use the same object, which in this case, is not possible because there are two different screens (files).

Here comes the question from above, how to recover this data or the reference of the object to make use as described above?

  • 4

    Put the code. Without it it is like trying to plug an outlet into a concrete block.

  • I edited the main post, with the example!

  • Are you sure you want to use an array? It looks like a list.

  • What’s in your class Alunos and in his class BuscarAluno? How your canvases are instantiated?

  • Patrick, I don’t know how to use lists, and neither can I, it’s a college paper. Victor, in the Students class has a get and set method, and in Buscaraluno a method that scans the array with a for and returns whether it exists in the array or not. Ai would be just an example, but the swing screens would be, a jFrame(Home Screen) that calls 2 Jinternalframe(Register and Search).

1 answer

1

I think it’s worth unifying your classes Alunos and its class BuscarAluno, because both seem to me to represent the same concept, which is your student database, even if that database is just an array in memory.

Once this is done, both screens have to use the same instance of their database (let’s say the class Alunos). The simplest way to do this would be to instantiate it on the main screen and pass it as parameter of the constructor of its classes that represent 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 you without having it. Can’t keep guessing how your code is.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.