I capture null value when I make a select to the database

2

Well, the problem is: I do not capture the value that exists in the database, especially in field codsubmenu , it is only capturing null values. I leave the code.

public class Menu implements Serializable{

    private int idMenu;
    private Menu codsubmenu;

    public int getIdMenu() {
        return idMenu;
    }

    public void setIdMenu(int idMenu) {
        this.idMenu = idMenu;
    }

    public Menu getCodsubmenu() {
        return codsubmenu;
    }

    public void setCodsubmenu(Menu codsubmenu) {
        this.codsubmenu = codsubmenu;
    }      

    while(rs.next()){
        Menu menu=new Menu(); 
        menu.setIdMenu(rs.getInt("idMenu")); //en esta línea si me captura el id del menu

        // me captura null 
        menu.setCodsubmenu(menu.getCodsubmenu());
    }  
}

However, when you change the last line to:

menu.setCodsubmenu(rs.getInt("codsubmenu"));

I underline the error in this way

incompatible types: int cannot be converted to Menu**
    
asked by Libra2880 19.01.2016 в 05:49
source

2 answers

1

Although I think there is a lack of data, this may help:

you say you capture null, and that's because you do menu.getCodsubmenu() ,

    // me captura null 
    menu.setCodsubmenu(menu.getCodsubmenu()); 

and this returns null because

public Menu getCodsubmenu() {
    return codsubmenu;
}

public void setCodsubmenu(Menu codsubmenu) {
    this.codsubmenu = codsubmenu;
}

is assigned and returning the same object that has not been initialized or assigned to another that was previously initialized:

private Menu codsubmenu;

getCodsubmenu() -> devuelve codesubmenu y menu.setCodsubmenu asigna el mismo objeto, al mismo objeto que no esta inicializado, quizas quiera mirar eso.

this message types: int cannot be converted to Menu** is therefore incompatible

public void setCodsubmenu(Menu codsubmenu) {
    this.codsubmenu = codsubmenu;
}      

this method expects a Menu but if its name in this method menu.setCodsubmenu(rs.getInt("codsubmenu")); follows some logical getInt it may be returning an int there the error, is that you can not assign an integer in setCodsubmenu wait a Menu to assign it to codsubmenu that is of type Menu ;

answered by 19.01.2016 в 15:31
0

You have the following: Menu codsubmenu . Your submenu code is an object of class Menu . Currently, you are setting it up as follows:

menu.setCodsubmenu(menu.getCodsubmenu());

But when you create an instance of Menu , the field codsubmenu since it does not have any initialization (at least because of what you present in your code) will have as default value null , therefore that line of code have no meaning.

Now, when you try to do this:

menu.setCodsubmenu(rs.getInt("codsubmenu"));

The compilation error is explicit: you are trying to assign a variable of type Menu the value of a int .

Perhaps your problem is of order when reading the data. Since you do not thoroughly explain your underlying problem, I believe that you should first read all the data, store Menu by Menu in a container such as a list or a map and then group them the way you want. By the way, as a personal note, you may also have a design problem in your data model, because a menu should not have the id of a child menu, but rather it should have the id of the parent menu, and the only menu that does not have father id is the root of all menus.

    
answered by 19.01.2016 в 15:32