当前位置: 代码迷 >> Java相关 >> 写存储对象的类时,出现NullPointerException异常
  详细解决方案

写存储对象的类时,出现NullPointerException异常

热度:397   发布时间:2013-04-05 18:57:18.0
写存储对象的类时,出现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  

----------------解决方案--------------------------------------------------------
Store类
private T[] store = null;

public void write (T t)
{
    store[pointer] = t;    //就没找到你实例化store的代码,你这是 null[pointer] = t;
    pointer++;
}
----------------解决方案--------------------------------------------------------
回复 2楼 yhlvht
求解决方法,谢谢~
----------------解决方案--------------------------------------------------------
定义的时候就实例化呗
private T[] store = new T[长度int型];  //长度嘛,自己看着办呗,最小为1
----------------解决方案--------------------------------------------------------
嗯嗯 ,同意


----------------解决方案--------------------------------------------------------
回复 4楼 yhlvht
创建泛型数组不行啊。
----------------解决方案--------------------------------------------------------
  相关解决方案