2
回答
Exception in thread "main" java.lang.NullPointerException到底哪里是空指向了啊?
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   
  1. import java.util.*;
  2. class Word{
  3. private String enWord;
  4. private LinkedList<String> chParaphrase=new LinkedList<String>();
  5. public Word(String enWord){
  6. this.enWord=enWord;
  7. }
  8. public String getWord(){
  9. return enWord;
  10. }
  11. public void setParaphrase(String paraphrase){
  12. chParaphrase.add(paraphrase);
  13. }
  14. public String getParaphrase(){
  15. String retParaphrase="";
  16. Iterator<String> it=chParaphrase.iterator();
  17. while(it.hasNext()){
  18. retParaphrase+=it.next();
  19. retParaphrase+="\r\n";
  20. }
  21. return retParaphrase;
  22. }
  23. }
  24. class En2ChDict{
  25. Word[] word=new Word[5];
  26. HashMap<String,Word> hm=new HashMap<String,Word>();
  27. public En2ChDict(){
  28. word[0]=new Word("good");
  29. word[0].setParaphrase("n.善行,好处");
  30. word[0].setParaphrase("a.好的,优良的,上等的");
  31. word[0].setParaphrase("[p1].商品");
  32. word[1]=new Word("bad");
  33. word[1].setParaphrase("a.坏的");
  34. for(int i=0;i<word.length;i++){
  35. hm.put(word[i].getWord().toLowerCase(),word[i]);
  36. }
  37. }
  38. public String queryWord(String enWord){
  39. Word word=hm.get(enWord.toLowerCase());
  40. String chParaphrase;
  41. if(word!=null){
  42. chParaphrase=word.getParaphrase();
  43. }else{
  44. chParaphrase="未找到该单词!\r\n";
  45. }
  46. return chParaphrase;
  47. }
  48. }
  49. public class TestWord{
  50. public static void main(String args[])throws Exception{
  51. En2ChDict ecd=new En2ChDict();
  52. String enWord;
  53. while(true){
  54. enWord=new Scanner(System.in).next();
  55. if(enWord.equals("-1")){
  56. break;
  57. }
  58. System.out.println(ecd.queryWord(enWord));
  59. }
  60. }
  61. }
  62. 执行的时候提示:
  63. Exception in thread "main" java.lang.NullPointerException
  64.         at En2ChDict.<init>(TestWord.java:38)
  65.         at TestWord.main(TestWord.java:56)
举报
shenqi186
发帖于6年前 2回/1K+阅
顶部