Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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();




Java
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.");
         }
     }    
     
   }
Posted
Updated 28-Sep-15 10:30am
v2

1 solution

It looks your keyboard variable is null. It should have been initialized this way:
Java
Scanner keyboard=new Scanner(System.in);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900