当前位置: 代码迷 >> Java相关 >> Exception in thread "main" java.lang.NullPointerException
  详细解决方案

Exception in thread "main" java.lang.NullPointerException

热度:988   发布时间:2006-12-29 16:36:28.0
Exception in thread "main" java.lang.NullPointerException

import java.util.*;

class MyKey
{
private String name;
private int age;
public MyKey(String name,int age)
{
this.name = name;
this.age = age;
}
public String toString()
{
return new String(name + "," + age);
}
public boolean equals(Object obj)
{
if(name.equals(obj) && age == obj)
{
return true;
}
else
{
return false;
}
}
public int hashCode()
{
return name.hashCode() + age;
}
}

class Thashtable
{
public static void main(String[] args)
{
Hashtable ht = new Hashtable();
ht.put(new MyKey("jimmy",20),new Integer(1));
ht.put(new MyKey("jimm",21),new Integer(2));
ht.put(new MyKey("jim",22),new Integer(3));
Enumeration e = ht.keys();
while(e.hasMoreElements())
{
MyKey key = (MyKey)e.nextElement();
System.out.println(key.toString() + "=");
System.out.println( ht.get(key).toString());
}
}
}

执行Exception in thread "main" java.lang.NullPointerException
at test.Thashtable.main(Thashtable.java:19)
那为大侠帮忙修改一下

搜索更多相关的解决方案: Exception  thread  java  lang  

----------------解决方案--------------------------------------------------------

import java.util.*;

class MyKey
{
private String name;
private int age;
public MyKey(String name,int age)
{
this.name = name;
this.age = age;
}
public String toString()
{
return new String(name + "," + age);
}
public boolean equals(Object obj)
{
if(name.equals(obj) && age == obj)
{
return true;
}
else
{
return false;
}
}
public int hashCode()
{
return name.hashCode() + age;
}
}


class Thashtable
{
public static void main(String[] args)
{
Hashtable ht = new Hashtable();
ht.put(new MyKey("jimmy",20),new Integer(1));
ht.put(new MyKey("jimm",21),new Integer(2));
ht.put(new MyKey("jim",22),new Integer(3));
Enumeration e = ht.keys();
while(e.hasMoreElements())
{
MyKey key = (MyKey)e.nextElement();
System.out.println(key.toString() + "=");
System.out.println(ht.toString());//此处写错 ,改后 }
}
}


[此贴子已经被作者于2006-12-29 17:33:46编辑过]


----------------解决方案--------------------------------------------------------
这样修改是不对的!

这样修改虽然不抱NullPointerException,但是结果不对
jimmy,20={jimmy,20=1, jim,22=3, jimm,21=2}
jim,22={jimmy,20=1, jim,22=3, jimm,21=2}
jimm,21={jimmy,20=1, jim,22=3, jimm,21=2}
结果应该是
jimmy,20=1
jim,22=3
jimm,21=2


----------------解决方案--------------------------------------------------------

你错在哪里,错在你的equals方法没有定义好,按你那个定义,没有东西能匹配

你用我写的替换你写的就可以了 ,其它的不用改
[CODE]public boolean equals(Object obj)
{
if(obj instanceof MyKey){
MyKey m=(MyKey)obj;
if(m.age==this.age&&m.name.equals(this.name)){
return true;
}
else
return false;
}else
return false;
}[/CODE]


----------------解决方案--------------------------------------------------------

当你用ht.get()的时候,你传入对象会去匹配hashtable里面的对象,它会调用equals方法,看里面有没有对象和它相等,

你以前定义的话,传入的肯定不会相等

这一点以后你要注意,如果你不知道如果去重写equals方法,干脆就不要写,用Object的方法


----------------解决方案--------------------------------------------------------
  相关解决方案