总是抛出空指针异常

杜显冲 2013-06-26 09:44:24

package dxc2013_6_23.test;
//读入一支球队的名字,输出该球队夺冠的年份列表。例如,读入“巴西”,应当输出1958 1962 1970 1994 2002读入“荷兰”,应当输出没有获得过世界杯
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class Test8 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队。如果该年没有举办世界杯,则输出:没有举办世界杯。
// WorldCup wp=new WorldCup();
Map<Integer,WorldCup> map=new HashMap<Integer, WorldCup>();
map.put(1930, new WorldCup(1,"乌拉圭"));
map.put(1934, new WorldCup(2,"意大利"));
map.put(1938, new WorldCup(3,"意大利"));
map.put(1950, new WorldCup(4,"乌拉圭"));
map.put(1954, new WorldCup(5,"德国"));
map.put(1958, new WorldCup(6,"巴西"));
map.put(1962, new WorldCup(7,"巴西"));
map.put(1966, new WorldCup(8,"英格兰"));
map.put(1970, new WorldCup(9,"巴西"));
map.put(1974, new WorldCup(10,"德国"));
map.put(1978, new WorldCup(11,"阿根廷"));
map.put(1982, new WorldCup(12,"意大利"));
map.put(1986, new WorldCup(13,"阿根廷"));
map.put(1990, new WorldCup(14,"德国"));
map.put(1994, new WorldCup(15,"巴西"));
map.put(1998, new WorldCup(16,"法国"));
map.put(2002, new WorldCup(17,"巴西"));
map.put(2006, new WorldCup(18,"意大利"));


System.out.println("请输入一个国家");
Scanner inpot=new Scanner(System.in);
String country=inpot.next();

Collection coll=map.values();
Iterator<?> it=coll.iterator();
while(it.hasNext()){
Object key=it.next();
if(map.get(key).equals(country)){
System.out.println(key);
}
}

}



}

//定义一个世界杯类
class WorldCup{
private int ss ;//年
private String country;//国家
public WorldCup(String country) {

this.country = country;
}

public WorldCup(int period, String country) {

this.ss = ss;
this.country = country;
}

public int getYrear() {
return ss;
}

public void setYrear(int yrear) {
this.ss = yrear;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

@Override
public String toString() {
return country;
}


}
...全文
317 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
dsl369 2013-06-28
  • 打赏
  • 举报
回复
调试很容易解决吧.....
十八道胡同 2013-06-27
  • 打赏
  • 举报
回复
引用 2 楼 u011173957 的回复:
呵呵 谢谢啦 搞定啦
调试时 异常信息会把出错的地方告诉你的
oh_Maxy 2013-06-27
  • 打赏
  • 举报
回复
String country=inpot.nextLine();
杜显冲 2013-06-26
  • 打赏
  • 举报
回复
呵呵 谢谢啦 搞定啦
lt3 2013-06-26
  • 打赏
  • 举报
回复
1: 把你的异常信息贴出来, 不要单纯贴代码; 2: 哪一行出现异常, 这是可以知道的; 3: 找到哪个是null, 然后出来(可以debug跟踪);

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧