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;
}
}
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 L. Constante
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();
– Douglas Vieira
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 :)
– Filipe L. Constante
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?

 System.out.print("How many rooms will be rented?: ");
 int room = sc.nextInt();
 sc.nextLine();
 Student s[];
 
 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);
 }

– Douglas Vieira
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.
– Filipe L. Constante
Douglas, just instantiate with the parameters.. new Student("Person name", "personal [email protected]");
– Filipe L. Constante
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.
– It Wasn't Me