Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1114231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:58:36+00:00 2026-05-17T02:58:36+00:00

package pkgPeople; import java.io.Serializable; import java.text.DecimalFormat; public class Person implements Serializable{ private String name;

  • 0
package pkgPeople;

import java.io.Serializable;
import java.text.DecimalFormat;

public class Person implements Serializable{
private String name;
private int height;
private int weight;
private BankAccount bankAccount;
private static int personCT=0;
private String noAccount;


//The Default constructor, a method
public Person()
  {
    personCT = personCT +1;
  }

// We have another constructor; this is also a method
public Person(int height,int weight, String name, BankAccount bankAccount)
   {
    personCT++;
    this.name = name;
    this.height = height;
    this.weight = weight;
   }

//Constructor for a person with a bank account
public Person(int height,int weight, String name, int acctID, double balance)
   {
    personCT++;
    this.bankAccount.setAcctID(acctID);
    this.bankAccount.setBalance(balance);
    this.name = name;
    this.height = height;
    this.weight = weight;

   }

//Mutator
public void setNoAccount() {
    this.bankAccount = null;
}

//Accessor
public String getNoAccount() {
    return noAccount;
}

//Accessor
public BankAccount getBankAccount() {
    return bankAccount;
}

//Mutator
public void setBankAccount(BankAccount bankAccount) {
    this.bankAccount = bankAccount;
}

//Accessor method, get...
public String getName() {
    return name;
}

public void setNoAccount(String noAccount) {
    this.noAccount = noAccount;
}

public static int getPersonCT()
   {
    return personCT;
   }

//Mutator method,  set...
public void setName(String name) {
    this.name = name;
}

public int getWeight() {
    return weight;
}

public void setWeight(int weight) {
    this.weight = weight;
}

//Method to set the height
public void setHeight(int ht)
   {
    height = ht;
   }

public int getHeight()
   {
    return height;
   }
//Method to calculate Body Mass Index
// BMI equals (Weight in Kg)/(height in meters squared)
public double calcBMI()
   {// all the variables in the next line are local variables 
    double num, den, heightInCm, heightInMeters,bmi;
    //The numerator is calculated
    num = weight * (1/2.2);  //1 pound = (1/2.2)Kg
    heightInCm = height * 2.54; // 1 inch = 2.54 cm
    heightInMeters = heightInCm/100; // 100cm = meter
    den = Math.pow(heightInMeters, 2);
    bmi = num/den;
    return bmi;

   }

//Method to determine the category for BMI
public String findBMICategory()
{
    double bmi;//local variable, it only exists when the method is running
    bmi = calcBMI();  // bmi = this.calcBMI(); would have been ok
    if (bmi>=30)
        {
        return "Obese";
        }
    else if ((bmi>=25) && (bmi<30))
        {
        return "Overweight";
        }
    else if ((bmi>=18.5) && (bmi<25))
        {
        return "Normal Weight";
        }
    else
       {
        return "Under Weight";
       }
}//end of findBMICategory


    public String toString()
    {
    DecimalFormat fmt = new DecimalFormat("###");
    DecimalFormat fmt1 = new DecimalFormat("#####");
    DecimalFormat fmt2 = new DecimalFormat("##.00");
    DecimalFormat fmt3 = new DecimalFormat("#####.##");

    String noAccount = "NO BANK ACCOUNT";



    {
    return "The name is " + name + ".\n"
           + "The height is " + fmt.format(height) + " inches.\n"
           + "The weight is " + fmt.format(weight) + " pounds.\n"
           + "The BMI is " + fmt2.format(calcBMI()) + ".\n"
           + "The " + fmt1.format(bankAccount.getAcctID()) + "bank account has " + fmt3.format(bankAccount.getBalance()) +"dollars in it.\n"
           + "Classification: " + findBMICategory()+ "\n\n";

    }

Here is my main that I’m atempting to run…

package pkgPeople;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class MakePeople {

//Create people and output them to a file

public static void main(String[] args) throws IOException{
   FileOutputStream file = new FileOutputStream("friends.dat");
   ObjectOutputStream outStream = new ObjectOutputStream(file);

   ArrayList <Person> people = new ArrayList<Person>();

   people.add(new Person(69, 165, "David Jones", (new BankAccount(12345, 2000.00))));
   people.add(new Person(62, 122, "Mary Decker", (new BankAccount(67890, 3020.50))));
   people.add(new Person(72, 250, "Jack Denner", (new BankAccount(33366, 2045.88))));
   people.add(new Person(66, 108, "Linda Hall", (new BankAccount(54321, 345.67))));
   people.add(new Person(68, 175, "Jim Broke", (new BankAccount(55555, 1.00))));
   people.add(new Person(67, 134, "Sally Kroll", (new BankAccount(44444, 129.00))));
   people.add(new Person(73, 200, "Fred Baker", (new BankAccount(66666, 666.66))));
   people.add(new Person(61, 110, "Jane Harris", (new BankAccount(22222, 211.99))));



   int index;
   //Print the people
   for(index = 0; index < people.size(); index++)
      {
       System.out.println(people.get(index));
      }
   outStream.writeObject(people);  //can serialize this way
 /*  
   //Could serialize as below
   for(index = 0; index < people.size(); index++)
      {
       outStream.writeObject(people.get(index));
      }

   */
   outStream.close();
   file.close();

}


}

Ok, so here is my People class. I’m getting the following error on line 154 when trying to run my main..

Exception in thread "main" java.lang.NullPointerException
at pkgPeople.Person.toString(Person.java:154)
at java.lang.String.valueOf(Unknown Source)
at java.io.PrintStream.println(Unknown Source)
at pkgPeople.MakePeople.main(MakePeople.java:32)

Now, here is the BankAccount class from where I’m trying to pull the AcctID and balances from…

package pkgPeople;

import java.io.Serializable;


public class BankAccount implements Serializable{

long acctID;
double balance;
String dateCreated;

public BankAccount(long acctID, double balance)
{
    this.acctID = acctID;
    this.balance = balance;
}


public long getAcctID() {
    return acctID;
}
public void setAcctID(long acctID) {
    this.acctID = acctID;
}
public double getBalance() {
    return balance;
}
public void setBalance(double balance) {
    this.balance = balance;
}
public String getDateCreated() {
    return dateCreated;
}
public void setDateCreated(String dateCreated) {
    this.dateCreated = dateCreated;
}

}

Any ideas as to why this is happening? Is it a constructor error?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T02:58:37+00:00Added an answer on May 17, 2026 at 2:58 am

    I bet bankAccount is null.

    In this constructor:

    // We have another constructor; this is also a method 
    public Person(int height,int weight, String name, BankAccount bankAccount) 
       { 
        personCT++; 
        this.name = name; 
        this.height = height; 
        this.weight = weight; 
       } 
    

    You do not do this line (which I bet you want to do):

    this.bankAccount = bankAccount;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

package GC; import java.util.Scanner; public class main { public static void main(String args[]) {
package vaannila; import java.util.ArrayList; import com.opensymphony.xwork2.ActionSupport; public class RegisterAction extends ActionSupport { private String
package org.apache.wicket.examples.guestbook; import java.util.Date; import org.apache.wicket.IClusterable; public class Comment implements IClusterable { private String
package src; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Command { public static
package test; import java.awt.*; import java.awt.event.*; import java.awt.geom.Ellipse2D; import java.awt.image.BufferedImage; import javax.swing.*; public class
package { import mx.controls.LinkButton; import flash.text.TextLineMetrics; public class multiLineLinkButton extends LinkButton { override protected
package javaapplication1; import java.io.*; /** * * @author simon */ public class Main {
package macroreader; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class MacroReader { public static
package matlab; import com.mathworks.toolbox.javabuilder.*; import com.eigenface.Eigenface; public class Test { public static void main(String[]
package jtextareatest; import java.io.FileInputStream; import java.io.IOException; import javax.swing.*; public class Jtextareatest { public static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.