-1

Hi everyone i am new to Java and OOP so please be gentle with me, i will try to make this question make as much sense as i can. I have a method that takes arraylists and i want to get elements from that arraylist and set them to a new object under my Student class

i think the issue is where i am making the object Student stu1 = new Student();. Do i have to do this in every method to be able to access it? where should i create this object so i can add to it from the addStudent method and retrieve info from it in other methods?

public void addStudent(ArrayList<String> id, ArrayList<String>  firstName,ArrayList<String> lastName) {



                 // setting the stu1 objects id firstname and lastname variables
            Student stu1 = new Student(); 
            stu1.setId(id.get(0));
            stu1.setFirstname(firstName.get(0));
            stu1.setLastname(lastName.get(0));

and in my student class

public class Student {
      String id;
      String firstname;
      String lastname;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getFirstname() {
        return firstname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
    public String getLastname() {
        return lastname;
    }
    public void setLastname(String lastname) {
        this.lastname = lastname;
    } 
    public void saystuff() {

        System.out.println(id + firstname + lastname);

when call staystuff() in my main method it returns null null null, obviously i am doing something wrong setting the variables or it is because i have to make the new stu1 object again under the main method? how can i access stu1 from the static main method because it was created in a non static method.

main method

```
public static void main(String[] args) throws IOException  {

    Student stu1 = new Student(); 
    stu1.saystuff();

  • You need to call the setters for id, firstname and lastname before attempting to print the uninitialized fields (they're all `null`). – Elliott Frisch Jul 30 '19 at 04:00
  • i did but still null – LearningJavaguy Jul 30 '19 at 04:07
  • so many empty lines, line with more than 280 characters, indentation, ... very hard to read that code – user85421 Jul 30 '19 at 04:12
  • @LearningJavaguy In `main`, you have two lines - one after the other. `Student stu1 = new Student();` - which creates a new instance of the `Student` class with `null` fields. And, `stu1.saystuff();` which displays your new instance (with `null` fields). You need to call the setters between those two statements (or add a constructor which sets the fields on instantiation). Or call some method to get the `Student` instance you really want, but your `addStudent` method isn't called (so it's irrelevant). – Elliott Frisch Jul 30 '19 at 04:31
  • @ElliottFrisch thanks, but they were already set in the addStudent method. i can see them being set in the debugger. do i have to set them again or is there a way from me to call them from main? – LearningJavaguy Jul 30 '19 at 04:33
  • At first glance, `addStudent` ***could*** return the `Student` (instead of being `void`). Where / how are you calling `addStudent`? – Elliott Frisch Jul 30 '19 at 04:38

1 Answers1

0

i found the answer.

at the top of my main class i declared the new object and made it static

public class main {
    public static Student stu1 = new Student();