0

The question is:

Create a class person with attribute name, age and gender. Create an array of the person to hold 10 person instances. Display the information stored on the person instance in the array.

I am not sure what the question says but I tried and error comes out like this:

Exception in thread "main" java.lang.NullPointerException at instance.main(instance.java:29)

here is my code:

import java.util.Scanner;
class person{
    String name= new String();
    int age;
    String gender= new String();
    void setInfo(String Name, int Age, String Gender){
        name= Name;
        age=Age;
        gender=Gender;
    }
    void showInfo(){
        System.out.println(name+" "+ age+" "+gender);
    }
}

public class instance {
    public static void main(String[] args) { 
        Scanner sc= new Scanner(System.in);
        String name= new String();
        int age;
        String gender= new String();
        person obj[]= new person[10];
       
        System.out.println("Enter Name age and gender of 10 persons");
        for(int i=0; i<obj.length; i++)
        { 
            name=sc.nextLine();
            age=sc.nextInt();
            gender=sc.nextLine();
            obj[i].setInfo(name, age, gender);
            
        }
        for(int i=0; i<obj.length; i++){
        obj[i].showInfo();
        }
    }
}
  • 3
    `person obj[]= new person[10];` creates an array of 10 elements that can hold `person` instance, but the elements are initialized to `null`. Therefore `obj[i]` is null, and you should initialize it with `obj[i] = new person();` before the call to `obj[i].setInfo(name, age, gender);` – Eran Dec 23 '21 at 07:01
  • Welcome to Stack Overflow. Please read [ask] and https://meta.stackoverflow.com/questions/334822 and https://meta.stackoverflow.com/questions/284236 and https://ericlippert.com/2014/03/05/how-to-debug-small-programs/. – Karl Knechtel Dec 23 '21 at 07:13
  • Does https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it answer your question? How about https://stackoverflow.com/questions/1922677/nullpointerexception-when-creating-an-array-of-objects ? – Karl Knechtel Dec 23 '21 at 07:15
  • 1
    Hint: the question title is the first thing people see about your question. It is very clear that you are here because you want others to help you. There is no need to put *that* into the question title. Look around, see how upvoted questions are worded: the give a precise request around the technical issue. – GhostCat Dec 23 '21 at 07:37
  • And: read about java naming conventions. Class names go UpperCase, and variable/field names go camelCase. And when you want to assign a parameter to a field, then the standard java pattern is to it like this `this.name = name`. And then: be consistent how/where you put your braces. You have to understand: you should write your code so that **humans** can read it easily. Therefore it is pretty important to follow "common standards" how to do things. And to follow them consistently. – GhostCat Dec 23 '21 at 07:39
  • 1
    And finally: learn what a **constructor** is in Java. Instead of *first* creating an object with `new()` to then call a method like `setInfo()` ... you should rather have a constructor that takes the required arguments, so you can go `Person x = new Person(name, age, gender)` – GhostCat Dec 23 '21 at 07:40

2 Answers2

2

You need to instantiate the class inside the array.

for (int i = 0; i < obj.length; i++) {
    name = sc.nextLine();
    age = sc.nextInt();
    gender = sc.nextLine();

    //see section below
    obj[i] = new person();
    
    obj[i].setInfo(name, age, gender);
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722
임정묵
  • 51
  • 3
  • thanks the error removed but it is only taking 20 inputs i.e. name and age instead of 30 inputs. why? **my Inputs** Claire 25 Female jack **and the error is:** _Exception in thread "main" java.util.InputMismatchException_ – Lekh Raj Awasthi Dec 23 '21 at 07:31
  • Do you have to enter input values ​​line by line? I don't understand the question exactly, but considering you mentioned 30... I think you can change this part as follows. age = sc.nextInt(); -->age = Integer.parseInt(sc.nextLine()); – 임정묵 Dec 23 '21 at 07:46
  • 1
    It worked. Our teacher did not say that or he did not reached there. Thanks. – Lekh Raj Awasthi Dec 23 '21 at 08:13
0
    static class person {
            String name= new String();
            int age;
            String gender= new String();

            // Constructor
            person(String Name, int Age, String Gender) {
                this.name = Name;
                this.age = Age;
                this.gender = Gender;
            }
    
            void setInfo(String Name, int Age, String Gender){
                name= Name;
                age=Age;
                gender=Gender;
            }
            void showInfo(){
                System.out.println(name+" "+ age+" "+gender);
            }
        }

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String name = new String();
        int age;
        String gender = new String();
        person[] obj = new person[10];

        for (int i = 0; i < obj.length; i++) {
            name = sc.next();
            age = sc.nextInt();
            gender = sc.next();
            //Initialize a new person object and assign its values and the add it to the array.
            person personObj = new person(name, age, gender); // This is where a constructor is used.
            obj[i] = personObj;
        }
        for (int i = 0; i < obj.length; i++) {
            obj[i].showInfo();
        }
    }

I've tried your code and found out the problem.

The problem is that anytime you create a class object, you must have to create a constructor to update any values of variables inside that class. If the class that you created in the same class and and will be used in main method then make that class static.

While reading input using Scanner, you've used sc.nextLine(); which causes InputMisMatchException. So, just use sc.next();