我正在寫一個java程序,它將int輸入作爲用戶的一個選項。根據輸入的程序會去不同的method.But當我運行代碼,它提供了以下錯誤代碼的我Scanner的問題
private Scanner reader;
public static void main(String[] args){
EchoClient c = new EchoClient();
int option;
System.out.println("Welcome");
System.out.println("Select one of the following options:");
System.out.println("1 - Iterative Server");
System.out.println("2 - Concurrent Server");
System.out.println("3 - Exit");
option = c.reader.nextInt(); // Line No 25
switch(option){
case 1:
c.iterative();//calls the iterative method
break;
case 2:
//some method
break;
case 3:
System.out.println ("bye bye");
break;
}//end of switch()
}//end of main()
public void iterative(){
String address;
String ip="";
try{
System.out.println ("Please enter your correct ip address");
BufferedReader getip= new BufferedReader (newInputStreamReader(System.in));
ip=getip.readLine();
}
catch (IOException error){
System.err.println("Error occured");
}
在第25行
Exception in thread "main" java.lang.NullPointerException
at lab1.EchoClient.main(EchoClient.java:25)
部分行:選項= C .reader.nextInt();
請嘗試以更好的方式格式化您的源代碼,以便那些想要幫助您的人不會浪費大部分時間。 – KNU