标题:写存储对象的类时,出现NullPointerException异常
取消只看楼主
ixcBoy
Rank: 2
等 级:论坛游民
帖 子:7
专家分:20
注 册:2013-4-5
结帖率:0
已结贴  问题点数:10 回复次数:2 
写存储对象的类时,出现NullPointerException异常
本人乃Java初学者一名,今天写了一个存储对象的泛型类时,出现了NullPointerException异常。

Store.java
程序代码:
package org.xcTeam.xcBoy.javaPractice.ClassStore;

/**

 * Created with IntelliJ IDEA.

 * User: Zengchen

 * Date: 13-4-4

 * Time: 下午9:59

 * To change this template use File | Settings | File Templates.

 */
public class Store <T>{
    public /*private*/ Store()
    {
        //throw new UnsupportedOperationException("不支持使用默认的构造器.");
    }

    public Store (T t)
    {
        store[0] = t;
        pointer++;
    }

    public T read ()
    {
        return store[pointer];
    }

    public T read (int n)
    {
        if (n >= 0 && n<=pointer)
            return store[n];
        else
            throw new ArrayIndexOutOfBoundsException("store" + "[" + n + "]" + "发生数组越界.");
    }

    @Override
    public int hashCode()
    {
        throw new UnsupportedOperationException("不支持获取hashCode,请使用int checkHashCode(int)方法.");
    }

    private int HashCode()
    {
        return super.hashCode();
    }

    public int hashCode(int pwd)
    {
        if (pwd == 990609)
            return super.hashCode();
        throw new UnsupportedOperationException("不支持获取hashCode,请使用int checkHashCode(int)方法.");
    }

    public boolean checkHashCode (int hashcode)
    {
        if (hashcode == this.hashCode)
            return true;
        else
            return false;
    }

    public void write (T t)
    {
                                                                                                                                                                                store[pointer] = t;
        pointer++;
    }

    private T[] store = null;
    private int pointer = 0;
    private int hashCode = this.HashCode();
}


Main.java
程序代码:
package org.xcTeam.xcBoy.javaPractice.ClassStore;

/**

 * Created with IntelliJ IDEA.

 * User: Zengchen

 * Date: 13-4-5

 * Time: 下午5:42

 * To change this template use File | Settings | File Templates.

 */
public class Main {
    public static void main (String[] args)
    {
        iStoreable i1 = new Example1();
        iStoreable i2 = new Example2();
        Store<iStoreable> store = new Store<iStoreable> ();
                                                                                                                                                                                store.write(i1);
        store.write(i2);
        System.out.println("Now print i1");
        store.read(0).print();
        System.out.println("Now print i2");
        store.read(1).print();
    }
}


iStoreable.java
程序代码:
package org.xcTeam.xcBoy.javaPractice.ClassStore;

/**

 * Created with IntelliJ IDEA.

 * User: Zengchen

 * Date: 13-4-5

 * Time: 下午6:01

 * To change this template use File | Settings | File Templates.

 */
public interface iStoreable {
    void print ();
}


Example1.java
程序代码:
package org.xcTeam.xcBoy.javaPractice.ClassStore;

/**

 * Created with IntelliJ IDEA.

 * User: Zengchen

 * Date: 13-4-5

 * Time: 下午6:02

 * To change this template use File | Settings | File Templates.

 */
public class Example1 implements iStoreable{
    public void print ()
    {
        System.out.println("This message is from the class Example1.");
    }
}


Example2.java
程序代码:
package org.xcTeam.xcBoy.javaPractice.ClassStore;

/**

 * Created with IntelliJ IDEA.

 * User: Zengchen

 * Date: 13-4-5

 * Time: 下午6:03

 * To change this template use File | Settings | File Templates.

 */
public class Example2 implements iStoreable{
    public void print ()
    {
        System.out.println("This message is from the class Example2.");
    }
}


后面的一个interface和两个类本想放进Store.java中的。
错误信息是:
Exception in thread "main" java.lang.NullPointerException
    at org.xcTeam.xcBoy.javaPractice.ClassStore.Store.write(Store.java:63)
    at org.xcTeam.xcBoy.javaPractice.ClassStore.Main.main(Main.java:17) <5 internal calls>


错误的行前加40个制表符标出

[ 本帖最后由 ixcBoy 于 2013-4-5 19:02 编辑 ]
搜索更多相关主题的帖子: 存储 异常 change color 
2013-04-05 18:57
ixcBoy
Rank: 2
等 级:论坛游民
帖 子:7
专家分:20
注 册:2013-4-5
得分:0 
回复 2楼 yhlvht
求解决方法,谢谢~
2013-04-05 20:47
ixcBoy
Rank: 2
等 级:论坛游民
帖 子:7
专家分:20
注 册:2013-4-5
得分:0 
回复 4楼 yhlvht
创建泛型数组不行啊。
2013-04-06 20:50



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-403095-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.633039 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved