执行过程中出现这个问题“Exception in thread "main" java.lang.NullPointerException”
packagecom.mengyun;importjava.io.BufferedReader;importjava.io.IOException;importjava....
package com.mengyun;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Test18 {
public static void main(String[] args) throws IOException {
// TODO 自动生成的方法存根
Map map=new HashMap();
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
Yinghan yh1=new Yinghan("苹果","Apple");
Yinghan yh2=new Yinghan("香蕉","Banana");
Yinghan yh3=new Yinghan("橘子","Orange");
Yinghan yh4=new Yinghan("菠萝","Pineapple");
Yinghan yh5=new Yinghan("梨","Pear");
map.put("苹果",yh1);;
map.put("香蕉",yh2);
map.put("橘子",yh3);
map.put("菠萝",yh4);
map.put("梨", yh5);
Iterator it=map.keySet().iterator();
while(it.hasNext())
{
String key=it.next().toString();
Yinghan yh=(Yinghan)map.get(key);
System.out.println(yh.getHan()+" "+yh.getYing());
}
Translation tl=new Translation();
System.out.println("请选择翻译类型:\n1.汉译英\n2.英译汉");
String x=bf.readLine();
if(x.equals("1"))
{
System.out.println("请输入:");
String han=bf.readLine();
tl.hanYing(han);
}else if(x.equals("2"))
{
System.out.println("请输入:");
String ying=bf.readLine();
tl.yingHan(ying);
}
}
}
class Translation
{
private Map map=null;
public Translation()
{
map=new HashMap();
}
public void hanYing(String han)
{
Yinghan yh1=(Yinghan)map.get(han);
System.out.println(yh1.getYing());
}
public void yingHan(String ying)
{
Yinghan yh2=(Yinghan)map.get(ying);
System.out.println(yh2.getHan());
}
}
class Yinghan
{
private String han;
public String getHan() {
return han;
}
public void setHan(String han) {
this.han = han;
}
public String getYing() {
return ying;
}
public void setYing(String ying) {
this.ying = ying;
}
private String ying;
public Yinghan(String han,String ying)
{
this.han=han;
this.ying=ying;
}
} 展开
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Test18 {
public static void main(String[] args) throws IOException {
// TODO 自动生成的方法存根
Map map=new HashMap();
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
Yinghan yh1=new Yinghan("苹果","Apple");
Yinghan yh2=new Yinghan("香蕉","Banana");
Yinghan yh3=new Yinghan("橘子","Orange");
Yinghan yh4=new Yinghan("菠萝","Pineapple");
Yinghan yh5=new Yinghan("梨","Pear");
map.put("苹果",yh1);;
map.put("香蕉",yh2);
map.put("橘子",yh3);
map.put("菠萝",yh4);
map.put("梨", yh5);
Iterator it=map.keySet().iterator();
while(it.hasNext())
{
String key=it.next().toString();
Yinghan yh=(Yinghan)map.get(key);
System.out.println(yh.getHan()+" "+yh.getYing());
}
Translation tl=new Translation();
System.out.println("请选择翻译类型:\n1.汉译英\n2.英译汉");
String x=bf.readLine();
if(x.equals("1"))
{
System.out.println("请输入:");
String han=bf.readLine();
tl.hanYing(han);
}else if(x.equals("2"))
{
System.out.println("请输入:");
String ying=bf.readLine();
tl.yingHan(ying);
}
}
}
class Translation
{
private Map map=null;
public Translation()
{
map=new HashMap();
}
public void hanYing(String han)
{
Yinghan yh1=(Yinghan)map.get(han);
System.out.println(yh1.getYing());
}
public void yingHan(String ying)
{
Yinghan yh2=(Yinghan)map.get(ying);
System.out.println(yh2.getHan());
}
}
class Yinghan
{
private String han;
public String getHan() {
return han;
}
public void setHan(String han) {
this.han = han;
}
public String getYing() {
return ying;
}
public void setYing(String ying) {
this.ying = ying;
}
private String ying;
public Yinghan(String han,String ying)
{
this.han=han;
this.ying=ying;
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询