得到一个空指针异常,我不懂,因为我是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);
最后一行是我得到错误的地方。当我说
时,我认为它已经初始化了private static final IAtmHelper ATM = AtmUtils.getAtmHelper();
IAtmHelper基本上是空的:
public class AtmUtils {
public static IAtmHelper getAtmHelper()
{
// this dummy impl is just included to satisfy compile-time errors
return null;
}
}
这是使用的ATMHelper类的顶部:
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 {
这是函数
public IUserAction getUserResponse(String argMessage,
ActionType... argValidActionTypes);
他们并没有真正做过anthing,因为这不是一个真正的项目,但更像是一个测试。我基本上理解如何做所有这些,但我不能在主要的开头绕过这个空指针。 感谢
答案 0 :(得分:1)
它正在抛出一个空指针,因为你要返回一个 null 对象:
public static IAtmHelper getAtmHelper()
{
// this dummy impl is just included to satisfy compile-time errors
return null; // <-- I am causing your NPE
}
您可能希望从函数中返回AtmHelper
的实例,如下所示:
public static IAtmHelper getAtmHelper()
{
return new AtmHelper();
}