views:

107

answers:

2
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!

+3  A: 

Luckily, the stack trace you provided can help you easily determine where your error occurred. My goal is to help you figure out by yourself so that in the future you know how to utilize the stack trace.

Let's work with what we know. We know you are getting a NullPointerException (usually means that you are using an object that has not yet been initialized, so therefore it is null) and we know its on line 154.

With that in mind, look at line 154 and see where you may have a null object. You should have enough information to figure it out.

Anthony Forloney
Thanks very much for the suggestions :)
D. Spigle
No problem. Having people guide me to the right answer was more beneficial than having people *give* me the right answer.
Anthony Forloney
+1 For guiding without taking over the assignment! ;-)
Jim Ferrans
if i could +1 I would!
D. Spigle
@D Spigle No worries, I hope you caught a sense as how to *read* a stacktrace, they will almost always give you all the information you need.
Anthony Forloney
A: 

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;
Starkey
appreciate the help!
D. Spigle