如何从另一个类访问对象

时间:2016-02-18 17:50:25

标签: java

我试图在LibraryCard中使用toString方法来访问名称和电子邮件。但是,当我使用owner.getName()时,它给我一个空指针异常错误。我也试图让卡片成为某些人。

public class Student{

  public String name;
  public String email;

  public Student(){
    name = "UNASSIGNED";
    email = "UNASSIGNED"; 
  }

  public String getEmail(){ 
    return email;
  }

  public String getName(){
    return name;
  }

  public void setEmail(String newEmail){
    email = newEmail;
  }

  public void setName(String newName){
    name = newName;
  }  
}

public class LibraryCard{

  public Student owner;
  public int borrowCount;

  public LibraryCard(){
    owner = null;
    borrowCount = 0;    
  }

  public void checkOut(int bookNum){
    borrowCount += bookNum;    
  }

  public int getNumberOfooks(){
    return borrowCount;
  }

  public Student getOwnerName(){
    return owner;   
  }

  public void setOwner(Student object){
    owner = object; 
  }

  public String toString(){
    return "\nCard1 Info:\n\t Owner Name: " + owner.getName() + "\n\t Email: " + owner.getEmail() + "\n\t Books borrowed: " + borrowCount ; 
  }
}

和测试人员

public class LibraryCardTester{

  public static void main(String args[]){

    Student student1 = new Student();

    student1.setName("James Johnson");
    student1.setEmail(student1.getName()+"@gmail.edu");
    System.out.println("Name: " + student1.getName());
    System.out.println("Email: " +student1.getEmail());

    Student student2 = new Student();

    student2.setName("Barack Obama");
    student2.setEmail(student1.Name()+"@gmail.edu");
    System.out.println("Name: " + student2.getName());
    System.out.println("Email: " +student2.getEmail());

    LibraryCard card1 = new LibraryCard();
    card1.checkOut(3);

    LibraryCard card2 = new LibraryCard();
    card2.checkOut(2);

    LibraryCard card3 = new LibraryCard();
    card3.checkOut(5);

    System.out.println(card1.toString());
    System.out.println(card2.toString());
    System.out.println(card3.toString());

  }
}

6 个答案:

答案 0 :(得分:1)

在我看来,你没有在图书馆卡片上设置所有者:

card1.setOwner(student1);

您的构造函数将所有者设置为null,因此除非您使用setOwner()方法设置所有者,否则所有者将始终为null,这意味着您在空对象上调用getName()方法导致NullPointerException

  public LibraryCard(){
    owner = null;
    borrowCount = 0;    
  }

答案 1 :(得分:0)

您好,您从未使用setOwner,因此库中的所有者始终为空

答案 2 :(得分:0)

你写

   public LibraryCard(){
    owner = null; // NULL!
    borrowCount = 0;    
  }

永远不要使用setOwner(Student object)当然你有NullPointerException。

使用类似:

LibraryCard card1 = new LibraryCard();
card1.setOwner(student1);
card1.checkOut(3);

LibraryCard card2 = new LibraryCard();
card1.setOwner(student2);
card2.checkOut(2);

LibraryCard card3 = new LibraryCard();
card1.setOwner(student2);
card3.checkOut(5);

答案 3 :(得分:0)

在我看来,你没有设置卡的名称,然后初始化为null,给你空指针异常..

       public LibraryCard(){
        owner = null; //<==== here you set it to null, but never change it
        borrowCount = 0;    
        }

您需要设置owner

  card1.setOwner(student1);

我建议你在创建对象时在构造函数中初始化变量。如果你不知道谁拥有它,你似乎会创建一张卡片?

...示例

   public LibraryCard(String _owner){
      owner = _owner;
      borrowCount = 0;    
   }

答案 4 :(得分:0)

在LibraryCard类中添加owner = new Student();

答案 5 :(得分:0)

通过设置为所有者将学生对象设置为卡片并尝试一次