Application error using vector java.lang.Nullpointerexception

Asked

Viewed 111 times

1

Good morning.

I’m trying to perform a proposed exercise in a Java course that I’m doing but I’m not getting it. This is a simple program that simulates the reservation of a hostel using vectors.

I need that, after selecting the amount of rooms that will be reserved, the program request the customer’s name(Student).

However when I run the program and type the client name it appears the following error:

Exception in thread "main" java.lang.NullPointerException
at application.Program.main(Program.java:25)

I believe the error is inside the block for in the stretch s[i].setName(n); class Main

But I couldn’t fix it.

Below are the codes(classes Program, Hostel and Student I’m using in this Java project:

package application;

import java.util.Scanner;

import entities.Hostel;
import entities.Student;

public class Program {

    public static void main(String[] args) {
        Hostel h[];
        h = new Hostel[10];

        Scanner sc = new Scanner(System.in);

        System.out.print("How many rooms will be rented?: ");
        int room = sc.nextInt();
        Student[] s = new Student[room];
        sc.nextLine();

        for(int i =0; i<s.length; i++) {
            System.out.println("RENT #"+i);
            System.out.print("Name :");
            String n = sc.nextLine();
            s[i].setName(n);
        }
        sc.close();
    }
}

package entities;

public class Hostel {
    private int room;
    private char situation;


    public int getRoom() {
        return room;
    }
    public void setRoom(int room) {
        this.room = room;
    }
    public char getSituation() {
        return situation;
    }
    public void setSituation(char situation) {
        this.situation = situation;
    }


}

package entities;

public class Student {
    private String name;
    private String email;

    public Student(String name, String email) {
        this.name = name;
        this.email = email;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }

}

1 answer

1


Check the null pointer on line 25. That’s it.

You need to instantiate the variable s, guy:

s[i] = new Student();

Remember that in your class Student there is no constructor without the parameters, it is important to create there.

Example:

public Student(){
}

public static void main(String[] args) {
    Hostel h[];
    h = new Hostel[10];

    Scanner sc = new Scanner(System.in);

    System.out.print("How many rooms will be rented?: ");
    int room = sc.nextInt();
    Student[] s = new Student[room];
    sc.nextLine();

    for(int i =0; i<s.length; i++) {
        System.out.println("RENT #"+i);
        System.out.print("Name :");
        String n = sc.nextLine();
        s[i] = new Student();
        s[i].setName(n);
    }
    sc.close();
}
  • You can use Debug to confirm which of the two indexes are null. It is quite simple. Any doubt, we are there to/ help.

  • Filipe, thanks for the return. I changed the code according to your orientation, but still I’m in doubt. See the code below: System.out.print("How Many Rooms will be rented?: "); int room = sc.nextInt(); Student s; sc.nextLine(); for(int i =0; i<s.length; i++) { System.out.println("RENT #"+i); System.out.print("Name :"); String name = sc.nextLine(); System.out.println("Email :"); String email = sc.nextLine(); s[i] = new Student(name, email); } sc.close();

  • Good, but you have an empty object that is the "Student s". There is nothing in it, so it will give error. It wasn’t the way I put it, take a closer look hehe :)

  • Okay, your code worked the way you proposed it. But if I were to use a constructor method passing the name and email parameters? What would it be like? &#xA; System.out.print("How many rooms will be rented?: ");&#xA; int room = sc.nextInt();&#xA; sc.nextLine();&#xA; Student s[];&#xA; &#xA; for(int i =0; i<s.length; i++) {&#xA; System.out.println("RENT #"+i);&#xA; System.out.print("Name :");&#xA; String name = sc.nextLine();&#xA; System.out.println("Email :");&#xA; String email = sc.nextLine();&#xA; s[i] = new Student(name, email);&#xA; }&#xA;

  • I would just like to understand why any citizen has denied my answer, and the goal was just to help. Instead of negative, I could propose a better answer.

  • Douglas, just instantiate with the parameters.. new Student("Person name", "personal [email protected]");

  • 1

    Who says there are only gentlemen in England? Votes downs are applied 90% by operators (idem moderators) who do not even answer the question where they applied them, and in the vast majority, for futile reasons, and this operates independently whether the code works or not, but even worse, there are also those who vote up, for equally futile reasons, And many codes that don’t work, that haven’t tested, and that won’t do it. It’s unfortunate, but it’s a fact. I think the down could only be applied upon a comment.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.