0

I'm trying to make a table list with names that I provide as input. But instead of just displaying the table with "object.getName();", I inserted a method from another class that is retrieving data from "another" class and displays them in a table. But the values are still null.

public class Sample {


    public static void main(String[] args) {
        addclass add = new addclass();
        table tab = new table();

        add.addclass();
        tab.table();
    }

}

public class table {
    addclass add = new addclass();

    public void table(){
        System.out.print("Name: " + add.getName());
    }
}

public class addclass {
    private String name;
    Scanner input = new Scanner(System.in);

    public void addclass(){
        System.out.print("Name: ");
        setName(input.nextLine());
    }
    public void setName(String newName){
        this.name = newName;
    }
    public String getName(){
        return name;
    }
}
  • 2
    Without seeing the code from `addClass` (should really be called "AddClass") it's hard to tell what's going on. Please edit the question (link under the question) and add at least the constructor for `addClass` and the code for `getEmployeeName1` – Tibrogargan May 02 '19 at 02:21
  • I have edited the question. Is it clear now? – Aaron Ancheta May 02 '19 at 02:33
  • Welcome to Stack Overflow. Please provide a [Minimal, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve). This code is incomplete. It won't even compile. Lots of details are missing. Like what is that `main()` thing at the top? It's not in any class. And what is `tab` in your main method? It sounds like you're saying that you have code that runs. This code isn't close to being runnable. – CryptoFool May 02 '19 at 03:52
  • No, sorry, it's not. You're doing `new addClass()`, but `addClass` is a method in `table` not a class - this is not valid. Also, your `switch` statement is not valid. The code for `case 1` is not valid Java at all. Not enough to go on to even guess what your intention was sorry – Tibrogargan May 02 '19 at 03:52
  • @Steve Sorry I just put less code and focused on the main codes just to show what have I typed. It's because there are codes in between which are unnecessary to put here. I dont intend to put all of em as I only want to ask why my setters and getters on my table class isn't getting any value. They're just nulls. But when I insert the getter directly from my main class rather than calling the table class which has the getters, it shows the value. – Aaron Ancheta May 02 '19 at 12:23
  • Possible duplicate of [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Joakim Danielson May 02 '19 at 13:37

1 Answers1

0

It would appear that you are missing a call to the addClass() method in your table() class.

I've put together a simple demo based on your code above (while just making employeeName a String).

You'll notice I moved the instantiation of addClass() inside a method, and called the addClass() method there - which is what you were missing. See lines 19 and 20.

In addition, I strongly advise reading up on naming conventions.

achAmháin
  • 3,944
  • 3
  • 12
  • 39
  • Woah. I checked the code and tried to understand the concept. So, I need to call addClass in the TABLE class so that I'd input data there first before calling the table with the data? – Aaron Ancheta May 02 '19 at 13:24
  • @AaronAncheta well you need to call the `addClass()` method somewhere, otherwise it doesn't execute that method. – achAmháin May 02 '19 at 13:34
  • Oh. Sorry. I have. It's in my main class, I forgot to put it in the codes. Here's how it flows, Main class calls addClass(); --> addClass() asks for inputs with setters --> main class calls the getters of those setters. But getters show Nulls. – Aaron Ancheta May 02 '19 at 13:45
  • @AaronAncheta you need to create a [mcve], i.e. a runnable program I can run on my machine and find your error. I gave an example in my link of the getter working fine, otherwise I've no idea what your issue is. – achAmháin May 02 '19 at 13:47
  • Yeah. I figured too. Im trying to create a new sample. – Aaron Ancheta May 02 '19 at 13:54
  • I have created a new one. Please see the code above. – Aaron Ancheta May 02 '19 at 14:02
  • @AaronAncheta the problem is in `table()`, when you call `addclass add = new addclass();`, it just creates a new instance of `addclass`, so the name is therefore `null` in that instance. The quickest fix in your situation is to change the `name` variable to `static`, i.e. `private static String name;`. However, in reality, you probably want to do it some other way, like passing the name, once set, into a constructor. But you can experiment with it yourself. – achAmháin May 02 '19 at 14:26
  • Thanks for your help! I've come with a simpler way to display what I have to display instead. But your demo made things clearer to me. :) – Aaron Ancheta May 02 '19 at 14:38