-1

Getting a Null Pointer Exception that I dont understand since Im new to java.

package com.atm;

import com.atm.IUserAction.ActionType;

/**
 * The ATM implementation class.
 * 
 * @author mshields
 * 
 */
public class AtmImpl {

    private static final IAtmHelper ATM =  AtmUtils.getAtmHelper();

    /**
     * This method is invoked when a bank card is inserted into the ATM. The card
     * is ejected immediately upon completion of this method, which represents the
     * user's ATM session.
     * 
     * @param args
     *          the bank card number is the only argument.
     */
    public static void main(String args[]) {

        IUserAction pinResponse = ATM.getUserResponse("Please enter your 4-digit PIN.", ActionType.STRING);

the last line is where im getting the error. I thought it is initialized when I said

private static final IAtmHelper ATM =  AtmUtils.getAtmHelper();

the IAtmHelper is basically empty:

public class AtmUtils {

    public static IAtmHelper getAtmHelper() 
    {
        // this dummy impl is just included to satisfy compile-time errors
        return null;
    }
}

here is the top of the ATMHelper class that is used:

package com.atm;

import java.math.BigDecimal;
import java.util.List;

import com.atm.IUserAction.ActionType;

/**
 * Interface that defines ATM utility methods.
 * 
 * @author mshields
 * 
 */
public interface IAtmHelper {

here is the function

public IUserAction getUserResponse(String argMessage,
            ActionType... argValidActionTypes);

they dont really do anthing since this isnt a real porject, but more like a test. I understand how to do all of it basically, but i cant get around this null pointer in the beginning of the main. thanks

user3571618
  • 203
  • 1
  • 2
  • 5
  • possible duplicate of [What is a Null Pointer Exception?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception) – Radiodef Apr 26 '14 at 01:31

1 Answers1

1

It is throwing a null pointer because you are returning a null object:

public static IAtmHelper getAtmHelper() 
    {
        // this dummy impl is just included to satisfy compile-time errors
        return null; // <-- I am causing your NPE
    }

You probably want to return an instance of an AtmHelper from within the function like this:

  public static IAtmHelper getAtmHelper() 
    {
        return new AtmHelper();
    }
Woot4Moo
  • 23,987
  • 16
  • 94
  • 151