#Why does this return null and 0?

59 messages ยท Page 1 of 1 (latest)

rigid oyster
#

Sorry if I look dumb, I'm a newbie, it hasn't even been 1 month since I started.

class MainMenu extends Inventory{

    String name;
    int slot;

    MainMenu(String name, int slot){
        this.name = name;
        this.slot = slot;
    }

    @Override
    public String nameone() {
        return this.name;
    }

    @Override
    public int slotone() {
        return this.slot;
    }
}
royal pastureBOT
#

โŒ› This post has been reserved for your question.

Hey @rigid oyster! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

rigid oyster
tepid quiver
#

i assume nameone() returns null and slotone() returns 0? that only happens when you created the MainMenu with null and 0, or the constructor didn't run (leaving the fields at their default values - null and 0)

#

where in your code do you use that class, where does it return null to?

rigid oyster
rigid oyster
tepid quiver
rigid oyster
#

I'm just messing around

#

Do you want me to send you the code? It's only 67 lines

tepid quiver
#

yes

rigid oyster
#
abstract class Inventory{

    String name;
    int slot;

    Inventory(){
        this.name = this.nameone();
        this.slot = this.slotone();
    }

    void onClick(){
        System.out.println("Print clicked!");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getSlot() {
        return slot;
    }

    public void setSlot(int slot) {
        this.slot = slot;
    }

    public abstract String nameone();
    public abstract int slotone();

}
class MainMenu extends Inventory{

    String name;
    int slot;

    MainMenu(String name, int slot){
        this.name = name;
        this.slot = slot;
    }

    @Override
    public String nameone() {
        return this.name;
    }

    @Override
    public int slotone() {
        return this.slot;
    }
}

public class Main {

    // This is the main class

    public static void main(String[] args) {

        MainMenu mainMenu = new MainMenu("Test", 1);
        System.out.println(mainMenu.getName());
        System.out.println(mainMenu.getSlot());

    }
}```
#

I'm just messing around trying to learn

#

If you find the problem, can you explain it to me?

#

My understanding for inheritance is still vague, I don't know why.

#

I'm probably just dumb

tepid quiver
#

that is quite the silly problem

#

when you call new MainMenu, the MainMenu constructor gets invoked

rigid oyster
#

wait what

#

wdym gets invoked

#

could you elaborate further

tepid quiver
#

because there's no super() call, the java compiler automatically inserts a call to the Inventory constructor (with no arguments) at the top in MainMenu, so the Inventory constructor technically runs before the MainMenu constructor

#

the Inventory constructor calls nameone() and slotone() from MainMenu, which return name and slot, which are both still at their default value at that point in time

#

only after the Inventory constructor completes do both of these fields get set

rigid oyster
#

o

#

welp time to learn the super keyword

#

thanks

royal pastureBOT
# rigid oyster thanks

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

brittle birch
#

umm ๐Ÿค”

tepid quiver
#

you actually can't really do what you want to do with this setup, a limitation of the constructor mechanism is that super constructors have to run before child constructors

plain tendon
#

It shouldn't matter since it should be getting the data from MainMenu not Inventory

brittle birch
#

String name; will work ๐Ÿค”

plain tendon
#

So his sysout shouldn't be null

#

Oh nevermind, mis read

tepid quiver
brittle birch
#

Strings needs to be initialized with String name = "";

tepid quiver
#

^ not the point

tepid quiver
#

make them abstract instead of nameone and slotone

tepid quiver
#

String name; in a field is entirely valid and initializes to null

#

String name = ""; would also be valid, but won't solve the problem

rigid oyster
#

apply inheritance to the getter methods? how would I do that?

tepid quiver
rigid oyster
#

o

#

ty

tepid quiver
#

OR: put the initial name and slot constructor parameters in Inventory as well, instead of just MainMenu

rigid oyster
#

u didn't fully spoonfeed me thanks

royal pastureBOT
# rigid oyster u didn't fully spoonfeed me thanks

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

tepid quiver
# tepid quiver OR: put the initial name and slot constructor parameters in Inventory as well, i...
abstract class Inventory {
  // name and slot definition
  public Inventory(String name, int slot) {
    this.name = name;
    this.slot = slot;
  }
  // ....
}
class MainMenu extends Inventory {
  public MainMenu(String name, int slot) {
    super(name, slot); // call to Inventory constructor with name and slot
  }
  // in this case, you DONT need nameone and slotone anymore, you provided those in the constructor
}
// ...
royal pastureBOT
# rigid oyster Thanks ๐Ÿ˜„

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

rigid oyster
#

I got it working