| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 717 人关注过本帖
标题:写存储对象的类时,出现NullPointerException异常
只看楼主 加入收藏
ixcBoy
Rank: 2
等 级:论坛游民
帖 子:7
专家分:20
注 册:2013-4-5
结帖率:0
收藏
已结贴  问题点数:10 回复次数:5 
写存储对象的类时,出现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
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:5 
Store类
private T[] store = null;

public void write (T t)
{
    store[pointer] = t;    //就没找到你实例化store的代码,你这是 null[pointer] = t;
    pointer++;
}
2013-04-05 20:45
ixcBoy
Rank: 2
等 级:论坛游民
帖 子:7
专家分:20
注 册:2013-4-5
收藏
得分:0 
回复 2楼 yhlvht
求解决方法,谢谢~
2013-04-05 20:47
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:0 
定义的时候就实例化呗
private T[] store = new T[长度int型];  //长度嘛,自己看着办呗,最小为1
2013-04-06 06:30
Kingbox_tang
Rank: 7Rank: 7Rank: 7
来 自:天津师范大学
等 级:黑侠
威 望:3
帖 子:146
专家分:677
注 册:2012-11-27
收藏
得分:5 
嗯嗯 ,同意


旨在提高编程水平,学有所用,学有所成,学有所为。
2013-04-06 11:19
ixcBoy
Rank: 2
等 级:论坛游民
帖 子:7
专家分:20
注 册:2013-4-5
收藏
得分:0 
回复 4楼 yhlvht
图片附件: 游客没有浏览图片的权限,请 登录注册
创建泛型数组不行啊。
2013-04-06 20:50
快速回复:写存储对象的类时,出现NullPointerException异常
数据加载中...
 
   



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

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