0

I'm trying to run "loadProgram" located in the motherboard class through the "Room" class. I typed

theRoom.getTheComputer().getMotherboard().loadProgram("Windows"); But I get

Exception in thread "main" java.lang.NullPointerException at Main.main(Main.java:12)<5 internal calls>

Main

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

  }
}

Room

public class Room {

private Computer theComputer;

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

public Computer getTheComputer() {
    return theComputer;
}
}

Computer

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

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

}

PrQ
  • 43
  • 1
  • 7
  • Time to debug your NPE. This is super common, and so you should be able search on NPE debugging techniques, tools you can use to find your solution. Closed as yet another NPE duplicate. – Hovercraft Full Of Eels Apr 15 '16 at 02:22
  • One word of advice: avoid over-use of chained calls as you're currently doing. For one it makes it that much difficult to debug your NPE. Separate that code into several lines to simplify finding out which line holds the key. – Hovercraft Full Of Eels Apr 15 '16 at 02:23
  • @HovercraftFullOfEels I'm just staring out and I just wanted to know how I could fix it. – PrQ Apr 15 '16 at 02:29
  • Read the links. Seriously, especially [this one](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it). Also read the [help] and go through the [tour]. Understand this is not a "help me fix my program" site, but a answer repository for common questions. This question has been asked to death on this site, and one more won't help any future visitors. – Hovercraft Full Of Eels Apr 15 '16 at 02:40

0 Answers0