0

It prints to console this

Michele,26,Italy John,25,America Gloria,20,null Alice,22,null Joe,18,null

How can i delete "nulls". I have to create two methods called setInfor one of them with 3 parameters the other one is 2. I guess the problem because of these methods. Could you help me out.

This was the assignment "Create a class 'Student' with three data members which are name, age and address. The constructor of the class assigns default values name as "unknown", age as '0' and address as "not available". It has two members with the same name 'setInfo'. First method has two parameters for name and age and assigns the same whereas the second method takes has three parameters which are assigned to name, age and address respectively. Print the name, age and address of 10 students."

public class Student {
    private String Name;
    private int age;
    private String adress;

    public Student(String name, int age) {
        this.Name = name;
        this.age =age ;
    }
    public Student() {
        this.Name = "Unknown";
        this.age = 0;
        this.adress = "Not available";
    }
    public  Student(String Name, int age, String adress) {
        this.Name = Name;
        this.age = age;
        this.adress = adress;
    }

    public void setName(){
        this.Name= Name;
    }
    public String getName(){
        return Name;
    }
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAdress() {
        return adress;
    }

    public void setAdress(String adress) {
        this.adress = adress;
    }


    public void setInfo(){
        System.out.printf("%s,%s,%s",Name,age,adress);}

    public void setInfo(String name,int age ){
        System.out.printf("%s,%s,%s",Name,age);
    }}    

   
      public class TestStudent {

    public static void main(String[] args){

        Student Stu1[]= new Student[10];
        Student Stu2[]= new Student[10];

        Stu2[0] =new Student("Michele",26,"Italy");
        Stu2[0].setInfo();
        System.out.println();

        Stu2[1]= new Student("John",25,"America");
        Stu2[1].setInfo();
        System.out.println();

       Stu1[2]=new Student("Gloria",20);
        Stu1[2].setInfo();
        System.out.println();

        Stu1[3]=new Student("Alice",22);
        Stu1[3].setInfo();
        System.out.println();

        Stu1[4]=new Student("Joe",18);
        Stu1[4].setInfo();
        System.out.println();

        }
    }
Sena
  • 37
  • 5
  • 1
    You created an array `Student Stu1[]= new Student[10];` Array created, but 10 Students not created and assigned in the array. Calling `Stu1[0].setInfo()` throws NullPointerException, because the array contains only nulls. – Ansar Ozden May 27 '21 at 19:50

1 Answers1

0

As mentioned in the comments, you have not inserted the values inside the Stu1 array and hence cannot access Stu1[0].

To insert values into the Stu1 array, you need Student objects. You will need appropriate constructor for that.

In the student class, you can define the constructor like this:

public Student(String name, int age, String adress) {
        this.Name = name;
        this.age = age;
        this.adress = adress;
}

Use this constructor to create objects of the Student class and enter the necessary values.

Now in the main method: Creating objects and inserting values:

Student Stu1[]= new Student[10];
Student student1 = new Student("Michele",26,"Italy"); // creates an object using above constructor with 3 parameters
Stu1[0] = student1;

... // and so on

  • I've changed the code. Now it works but i'm trying to delete "null" s. Could you please help – Sena May 28 '21 at 11:47
  • The constructor that we added earlier was for 3 parameters. You have a constructor with 3 parameters (name and age), which you intend to use. You are assigning the name and age but the adress field still says the same as when the object is created, ie null, which you can see in the output. In the constructor with 2 parameters, initialize the adress field you want. eg. `String adress = "";` this will print "". Other thing you could do, although not recommended, is make a new method to print : _set_info_: where you print only desired values – Kalinda Sharma May 28 '21 at 16:45