0

I'm trying to use a class ("Screen") to handle all instances of System.out.print in my other classes. However I am getting a null pointer exception from my main method when I try to run my code to test it it.

I can't figure out what exactly is causing this error, any tips would be appreciated.

Main Class

public class AtmTest {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    //Screen.displayMessage(null);


    AtmTest test = new AtmTest();

}

public AtmTest(){
    //write way to start all processes
    new ATM();

}

}

This is the ATM class where I am first tyring to call the Screen class to display a message to the console, and where my code stops running.

public class ATM {

private boolean userAuthenticated;
private int currentAccountNumber;

private static final int BALANCE_INQUIRY = 1;
private static final int WITHDRAWAL = 2;
private static final int DEPOSIT = 3;
private static final int EXIT = 4;

private Keypad keypad;
private Screen screen;
private BankDatabase bankDatabase;

public ATM(){
//new Screen();
//screen.displayMessage("hey"); 
run();
}

public void run(){


    screen.displayMessageLine("Welcome!");

    authenticateUser();
    displayMainMenu();
    performTransactions();

}

This is the screen class that is supposed to control all outputs for the program.

public class Screen {



public Screen(){


}

public void displayMessage(String a){

    System.out.print(a);
}

public void displayMessageLine(String a){
    System.out.println(a);
}

public void displayDollarAmount(double a){
    System.out.println("$" + a); //make to two decimal places
}
cmurda
  • 3
  • 3

1 Answers1

0

Your screen variable in ATM class is NULL. You have to initialize it, in constructor or by changing the line private Screen screen; to something like: private Screen screen = new Screen().

lukeg
  • 4,189
  • 3
  • 19
  • 40