hashCode() and NullPointerException



我有两个问题:

1)为什么我在打电话时t.i.hashCode()电话时会NullPointerException

2)在哪些情况下调用和不调用hashCode()方法?(它是否仅在我们使用哈希相关类的对象的情况下才会收到调用?

O/P 为空。

线程"main"中的异常 java.lang.NullPointerException                    at Test.hashCode(EqualsHashcode.java:65)                    at java.util.Hashtable.put(Unknown Source)                    at EqualsHashcode.main(EqualsHashcode.java:20)
import java.util.*;
public class EqualsHashcode 
{
    public static void main(String[] args)
    {
        //System.out.println("Main Method");
        Test s = new Test("bharat",1);
        Test p = new Test("bharat",1);
        //System.out.println(s);
        //System.out.println(p);
        //System.out.println(s.equals(p));
        Test q = new Test("bharat",2);
        //System.out.println(q);
        //System.out.println(s.equals(q));
        Test r = new Test("bhara",1);
        //System.out.println(r);
        //System.out.println(s.equals(r));
        Hashtable ht = new Hashtable();
        ht.put(s,"1");
        ht.put(p,"1");
    }
}
class Test
{   Test t;
    String p;
    int i;
    Test(String s, int j)
    {
        p=s;
        i=j;
    }
    public String toString()
    {   
        System.out.println(p+".."+i);
        return p+" Hashcode is"+p.hashCode();
    }
    public boolean equals(Object o)
    {   
        if (this==o)
        {
            return true;
        }
else if ( o instanceof Test)
        {               
         t = (Test) o;
            if (this.p.equals(t.p) && this.i==t.i)
                            return true;
            else
                return false;
        }
else
        return false;
    }
    public int hashCode()
    {   
        System.out.println(t);
        System.out.println("Calling hashCode" + " "+ t.p.hashCode()+t.i);
        return t.p.hashCode()+t.i;
    }
}
我认为这就是

你想要的:

public class EqualsHashcode {
    public static void main(String[] args) {
        Test s = new Test("bharat", 1);
        Test p = new Test("bharat", 1);
        Test q = new Test("bharat", 2);
        Test r = new Test("bhara", 1);
        Hashtable ht = new Hashtable();
        ht.put(s, "1");
        ht.put(p, "1");
    }
}
class Test {
    String p;
    int i;
    Test(String s, int j) {
        p = s;
        i = j;
    }
    public String toString() {
        System.out.println(p + ".." + i);
        return p + " Hashcode is" + p.hashCode();
    }
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        } else if (o instanceof Test) {
            Test t = (Test) o;
            if (this.p.equals(t.p) && this.i == t.i) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    public int hashCode() {
        System.out.println(this);
        System.out.println("Calling hashCode" + " " + this.p.hashCode() + this.i);
        return this.p.hashCode() + this.i;
    }
}

相关内容

  • 没有找到相关文章

最新更新