构图错误

时间:2016-04-15 02:20:08

标签: java composition

我试图运行" loadProgram"通过" Room"位于主板类类。我输入了

theRoom.getTheComputer()getMotherboard()loadProgram。("视窗&#34);但是我得到了

线程中的异常" main"显示java.lang.NullPointerException     在Main.main(Main.java:12)< 5内部调用>

主要

public class Main {
    public static void main(String[] args) {


    Keyboard theKeyboard = new Keyboard("Test1", "Razer");
    Motherboard theMotherboard = new Motherboard("RX-58","Asus",4, 6, "v2.44");

    Computer theComputer = new Computer(theKeyboard, theMotherboard);
    Room theRoom = new Room(theComputer);

    theRoom.getTheComputer().getMotherboard().loadProgram("Windows");

  }
}

public class Room {

private Computer theComputer;

public Room(Computer theComputer) {
    this.theComputer = theComputer;
}

public Computer getTheComputer() {
    return theComputer;
}
}

计算机

public class Computer {
private Keyboard theKeyboard;
private Motherboard motherboard;

public Computer(Keyboard theKeyboard, Motherboard theMotherboard) {
    this.theKeyboard = theKeyboard;
}

public Keyboard getTheKeyboard() {
    return theKeyboard;
}

public Motherboard getMotherboard(){
    return motherboard;
}
}

主板

public class Motherboard {

private String model;
private String manufacturer;
private int ramSlots;
private int cardSlots;
private String bios;

public Motherboard(String model, String manufacturer, int ramSlots, int cardSlots, String bios) {
    this.model = model;
    this.manufacturer = manufacturer;
    this.ramSlots = ramSlots;
    this.cardSlots = cardSlots;
    this.bios = bios;
}


public void loadProgram(String programName){
    System.out.println("Program " + programName + " Is now loading... " );
}

public String getModel() {
    return model;
}

public String getManufacturer() {
    return manufacturer;
}

public int getRamSlots() {
    return ramSlots;
}

public int getCardSlots() {
    return cardSlots;
}

public String getBios() {
    return bios;
}

}

0 个答案:

没有答案