本文介绍了我如何修复Nullpinterexception?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在变量答案中获取字符串输入,将其转换为int,然后在arraylist中找到相应的值。但我得到NullPointerException for line answer = keyboard.nextLine();
I am trying to get string input in a variable answer, convert it to int and then find corresponding value in arraylist. But i am getting NullPointerException for line answer=keyboard.nextLine();
public void printCustomer(){ int iSize=customerList.size(); boolean loopControl=true; String answer=""; while(loopControl){ int loop; Customer cusTemp; System.out.printf("Which Customer is Ordering ?\n"); for(loop=0;loop<iSize;loop++){ cusTemp=customerList.get(loop); System.out.printf("% 2d. %-20s \n",loop+1,cusTemp.getName()); } answer=keyboard.nextLine(); int index=Integer.parseInt(answer)-1; if(index<iSize){ tempCustomer=customerList.get(index); loopControl=false; } else{ System.out.printf("\n Sorry, wrong option."); } } }推荐答案
看起来你的 keyboard 变量为null。它应该以这种方式初始化: It looks your keyboard variable is null. It should have been initialized this way: Scanner keyboard=new Scanner(System.in);
更多推荐
我如何修复Nullpinterexception?
发布评论