0

I am having a problem with a java program. Exception in thread "main"

1)Batman
Exception in thread "main" java.land.NullPointerException
at Superhero.printInfo(superhero.java:46)
at Superhero.main(Superhero.java:85)

is the error. This is the full code:

import java.util.Scanner;

public class Superhero{
    
    
String [] firstName = new String [5];
String [] lastName = {"Wayne","Kent","Diana","Authur","Quinzel"};
String [] email = {"batman@gmail.com", "superman@gmail.com", "wonder@gmail.com", "aquaman@gmail.com", "quinn@gmail.com"};

public Superhero() {
    firstName[0] = "Bruce";
    firstName[1] = "Clark";
    firstName[2] = "Wayne";
    firstName[3] = "Curry";
    firstName[4] = "Harleen";
    
    }
    
// Assing values of supeheros.
// @param entry - an superhero instance to be filled
/**

**/
public static Superhero SuperheroInfo(Superhero entry, Scanner inputs){
    
    // input first name
    System.out.print("Enter First Name: ");
    String input1 = inputs.next();
    
    // input last name
    System.out.print("Enter Last Name: ");
    String input2 = inputs.next();
    
    // input email
    System.out.print("Enter Email: ");
    String input3 = inputs.next();
    
    return entry;
}


// Print all information.
public static void printInfo(Superhero entry) {
    
    System.out.println("First Name: " + entry.firstName);
    System.out.println("Last Name: " + entry.lastName);
    System.out.println("Email: " + entry.email);
    
}

// Main method
public static void main(String[] args) {
    
    Superhero superhero1 = null;
    Superhero superhero2 = null;
    Superhero superhero3 = null;
    Superhero superhero4 = null;
    Superhero superhero5 = null;

    Scanner scanner = new Scanner(System.in);
    
    // Data entries for all superheros
    System.out.println("Superhero 1 Data Entry");
    superhero1 = SuperheroInfo(superhero1, scanner);
    
    System.out.println("\nSuperhero 2 Data Entry");
    superhero2 = SuperheroInfo(superhero2, scanner);
    
    System.out.println("\nSuperhero 3 Data Entry");
    superhero3 = SuperheroInfo(superhero3, scanner);
    
    System.out.println("\nSuperhero 4 Data Entry");
    superhero4 = SuperheroInfo(superhero4, scanner);
    
    System.out.println("\nSuperhero 5 Data Entry");
    superhero5 = SuperheroInfo(superhero5, scanner);
    
    // Print out all superhero information
    System.out.println("***********************");
    System.out.println("Classified Information");
    System.out.println("***********************");
    System.out.println("\n1) Batman");
    
    printInfo(superhero1);
    System.out.println("-------------------------");
    System.out.println("\n2) Superman");
    
    printInfo(superhero2);
    System.out.println("-------------------------");
    System.out.println("\n3) Wonderwoman");

    printInfo(superhero3);
    System.out.println("-------------------------");
    System.out.println("\n4) Aquaman");

    printInfo(superhero4);
    System.out.println("-------------------------");
    System.out.println("\n5) Harley Quinn");

    printInfo(superhero5);
    System.out.println("-------------------------");
    
    return;
}
    


}

Im just starting out on programming so its a bit confusing at times. Could really use some help, as I've been stuck on this for hours.

Im just adding words now coz the system wont let me post. Im just adding words now coz the system wont let me post. Im just adding words now coz the system wont let me post. Im just adding words now coz the system wont let me post. Im just adding words now coz the system wont let me post. Im just adding words now coz the system wont let me post. Im just adding words now coz the system wont let me post. Im just adding words now coz the system wont let me post. Im just adding words now coz the system wont let me post.

  • 2
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Yoshikage Kira May 07 '21 at 03:39
  • In function `SuperheroInfo`, you are taking input from user but are not saving in the `entry` object and return it as it is which is `null` – Yoshikage Kira May 07 '21 at 03:44

1 Answers1

0

This is because, in SuperHeroInfo you are not creating a SuperHero object and also why are you creating an array, of details, when you are reading input from the user. Try this:

import java.util.Scanner;

public class Superhero{
    
    
String firstName;
String lastName;
String email;

public Superhero(String firstName,  String lastName, String email) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
}
    
// Assing values of supeheros.
// @param entry - an superhero instance to be filled
/**

**/
public static Superhero SuperheroInfo(Superhero entry, Scanner inputs){
    
    // input first name
    System.out.print("Enter First Name: ");
    String input1 = inputs.next();
    
    // input last name
    System.out.print("Enter Last Name: ");
    String input2 = inputs.next();
    
    // input email
    System.out.print("Enter Email: ");
    String input3 = inputs.next();
    
    entry = new Superhero(input1, input2, input3);
    return entry;
}


// Print all information.
public static void printInfo(Superhero entry) {
    
    System.out.println("First Name: " + entry.firstName);
    System.out.println("Last Name: " + entry.lastName);
    System.out.println("Email: " + entry.email);
    
}

// Main method
public static void main(String[] args) {
    
    Superhero superhero1 = null;
    Superhero superhero2 = null;
    Superhero superhero3 = null;
    Superhero superhero4 = null;
    Superhero superhero5 = null;

    Scanner scanner = new Scanner(System.in);
    
    // Data entries for all superheros
    System.out.println("Superhero 1 Data Entry");
    superhero1 = SuperheroInfo(superhero1, scanner);
    
    System.out.println("\nSuperhero 2 Data Entry");
    superhero2 = SuperheroInfo(superhero2, scanner);
    
    System.out.println("\nSuperhero 3 Data Entry");
    superhero3 = SuperheroInfo(superhero3, scanner);
    
    System.out.println("\nSuperhero 4 Data Entry");
    superhero4 = SuperheroInfo(superhero4, scanner);
    
    System.out.println("\nSuperhero 5 Data Entry");
    superhero5 = SuperheroInfo(superhero5, scanner);
    
    // Print out all superhero information
    System.out.println("***********************");
    System.out.println("Classified Information");
    System.out.println("***********************");
    System.out.println("\n1) Batman");
    
    printInfo(superhero1);
    System.out.println("-------------------------");
    System.out.println("\n2) Superman");
    
    printInfo(superhero2);
    System.out.println("-------------------------");
    System.out.println("\n3) Wonderwoman");

    printInfo(superhero3);
    System.out.println("-------------------------");
    System.out.println("\n4) Aquaman");

    printInfo(superhero4);
    System.out.println("-------------------------");
    System.out.println("\n5) Harley Quinn");

    printInfo(superhero5);
    System.out.println("-------------------------");
    
    return;
}
}
Charchit Kapoor
  • 8,934
  • 2
  • 8
  • 24
  • SuperheroInfo could improved with `return new Superhero(input1, input2, input3);` and removing `entry` parameter – Hưng Chu May 07 '21 at 04:00