如何在线程“main”中解决此probem异常Test.main中的java.lang.NullPointerException(Test.java:342) [英] How to solve this probem Exception in thread "main" java.lang.NullPointerException at Test.main(Test.java:342)
本文介绍了如何在线程“main”中解决此probem异常Test.main中的java.lang.NullPointerException(Test.java:342)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public static void main( String [] args){
Scanner scanner = new 扫描器(System.in);
System.out.print( 输入要搜索的成员ID: );
String search = scanner.nextLine();
for ( int i = 0 ; i< v.length; i ++){
if (search.equals(v [i] .getMemID())){ // 字符串比较
System.out.println(v [i] .toString());
System.out.print( 输入你的名字);
String memName = scanner.nextLine();
v [i] .setmemName(memName);
}
}
}
解决方案
使用Eclipse进行调试 - Lars Vogel教程 [ ^ ]
当
String search = scanner.nextLine();
产生异常,然后必须scanner == null
或scanner.nextLine()== null
。
调试器中的简短介绍会给你答案。
你可以
- 使用try / catch
- 使用if语句
- 确保数据不为空
玩得开心!
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter member id to search : ");
String search = scanner.nextLine();
for (int i = 0; i < v.length; i++) {
if (search.equals(v[i].getMemID())) { // String comparison
System.out.println(v[i].toString());
System.out.print("Enter your Name");
String memName = scanner.nextLine();
v[i].setmemName(memName);
}
}
}
解决方案
Debugging with Eclipse - Tutorial by Lars Vogel[^]
When
String search = scanner.nextLine();
produces an Exception, then mustscanner == null
orscanner.nextLine() == null
.
A Short look in the debugger would give you the answer.
you can
- use try/catch
- use an if-statement
- make sure the data is not null
Have fun!
这篇关于如何在线程“main”中解决此probem异常Test.main中的java.lang.NullPointerException(Test.java:342)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文