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";

    }


这是我要跑步的主要内容...

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();

}


}


好的,这是我的People课。尝试运行main时,我在第154行收到以下错误。

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)


现在,这是BankAccount类,我正在尝试从中提取AcctID和余额。

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;
}

}


有什么想法为什么会这样?这是构造函数错误吗?

谢谢!

最佳答案

我打赌bankAccount为空。

在此构造函数中:

// 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;
   }


您不要执行以下操作(我敢打赌您想这样做):

this.bankAccount = bankAccount;

关于java - java:NullPointerException,需要帮助,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3852573/

10-12 17:29