0

I'm working on a Java based project that incorporates 3 different classes, each calling items out of the previous. Everything in this appears to be working except line 22 from Wedding.java.

Person.class - builds a person object with a few attributes

import java.util.Scanner;
import java.time.LocalDate;

public class Person {
    public String firstName = "";
    protected String lastName = "";
    protected LocalDate birthDate = LocalDate.now();
    
    Scanner sc = new Scanner(System.in);
    
    protected void setFirstName(String PersonInstance) {
        System.out.println("Enter Person" + PersonInstance + " First Name >>> ");
        this.firstName = sc.nextLine();
    }
    protected void setLastName(String PersonInstance) {
        System.out.println("Enter Person" + PersonInstance + " Last Name >>> ");
        this.lastName = sc.nextLine();
    }
    protected void setBirthDate(String PersonInstance) {
        System.out.println("Enter Person" + PersonInstance + " Date of Birth (yyyy-mm-dd) >>> ");
        this.birthDate = LocalDate.parse(sc.nextLine());
        
    }
    
    public String getFirstName() {
        return this.getFirstName();
    }
    
    public String getLastName() {
        return this.getLastName();
    }
    
    public LocalDate getBirthDate() {
        return this.birthDate;
    }
    
    public Person(String PersonInstance) {
        this.setFirstName(PersonInstance);
        this.setLastName(PersonInstance);
        this.setBirthDate(PersonInstance);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
    }

}//end

Couple.java - creates two Person objects

import blah.Person;

public class Couple {
public Person person1;
public Person person2;
    
    public Couple() {
        Person person1 = new Person("1");
        Person person2 = new Person("2");
    
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    }

}//end

And finally Wedding.java -- Sets a couple of class attributes/variables and creates a single Couple object.

import java.time.LocalDate;

import java.util.Scanner;
import blah.Couple;
public class Wedding {
    protected String Location;
    protected LocalDate weddingDate = LocalDate.now();
    protected Couple newCouple ;
    
    public Wedding() {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter Wedding Date (yyyy-mm-dd) >>> ");
        this.weddingDate = LocalDate.parse(sc.nextLine());
        System.out.println("Enter Location for Wedding >>> ");
        this.Location = sc.nextLine();
    }
    
    public void getWeddingDetails() {
        System.out.println("Location is : " + this.Location);
        System.out.println("Wedding date is : " + this.weddingDate);
        System.out.println(this.newCouple.person1.getFirstName());
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
    }

} //end

A NPE is returned when running this.newCouple.person1.getFirstName() (line 22 of Wedding.java). All other portions of this are working, so far. The Wedding object is created, and class variables set. The prompts from the Person objects are presented, and information stored. Just trying to retrieve information from the Person object from the Wedding class seems to cause an issue.

  • You need to instantiate the couple object with persons –  Nov 02 '20 at 18:55
  • I’m sorry, very new to Java programming. The Couple class creates two Person objects, Wedding creates a single Couple object. At least that is the goal. Could you give a little more detail? – Michael Discenza Nov 02 '20 at 20:30
  • 1. Take out the Scanner stuff in the Person class, 2. In Couple class change Person person1 = new Person("1"); to person1 = new Person("1");, same for person2 3. In the wedding constructor, add newCouple = new Couple() –  Nov 02 '20 at 20:41
  • Thank you for the response. What you provided, along with a few other modifications got everything working. – Michael Discenza Nov 03 '20 at 14:17

0 Answers0