我如何修复Nullpinterexception?

编程入门 行业动态 更新时间:2024-10-26 18:23:46
本文介绍了我如何修复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?

本文发布于:2023-11-10 03:15:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574202.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Nullpinterexception

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!