ERP_By_WeiDong 2010-04-24 16:47
浏览 220
已采纳

Java编译能通过,但是运行的时候提示有错误

Java编译能通过,但是运行的时候提示有错误。

提示出现这样的错误:

 

Exception in thread "main" java.lang.ExceptionInInitializerError
    at org.idea.ok.Person.main(Person.java:32)
Caused by: java.lang.NullPointerException
    at org.idea.ok.Gender1.<init>(Gender1.java:28)
    at org.idea.ok.Gender1.<clinit>(Gender1.java:17)
    ... 1 more

 原程序如下:

package org.idea.ok;

/*
 *枚举类
 *既可以通过public访问权限的静态常量来访问实例;
 *也可以通过Gender1类的getInstance静态方法来访问实例变量。 
 */
 
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.NoSuchElementException;
import java.util.Map;
import java.util.HashMap;
 
public class Gender1 implements Serializable {
    public static final Gender1 FEMALE = new Gender1(new Character('F'), "FEMALE");
    public static final Gender1 MALE = new Gender1(new Character('M'), "MALE");
    private final Character sex;
    private final transient String description;
    private static Map<Character, Gender1> instancesBySex = 
        new HashMap<Character, Gender1>();

    
    private Gender1(Character sex, String description) {
        this.sex = sex;
        this.description = description;
        instancesBySex.put(sex, this);
    }
    
    public Character getSex() {
        return sex;
    }
    
    public String getDescription() {
        return description;
    }
    
    public static Collection getAllValues() {
        return Collections.unmodifiableCollection(instancesBySex.values());
    }
    
    public static Gender1 getInstance(Character sex) {
        
        /*按照参数指定的性别缩写查找Gender1实例*/
        Gender1 result = (Gender1)instancesBySex.get(sex);
        if (result == null) {
            throw new NoSuchElementException(sex.toString());
        }
        return result;
    }
    
    public String toString() {
        return description;
    }
    
    private Object readResolve() {
        
        /*保证反序列化时直接返回Gender1类包含的静态实例*/
        return getInstance(sex);
    }
}
package org.idea.ok;

/*
 */
 
public class Person {
    private String name;
    private Gender1 gender;
    
    public Person(String name, Gender1 gender) {
        this.name = name;
        this.gender = gender;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public Gender1 getGender() {
        return gender;
    }
    
    public void setGender(Gender1 gender) {
        this.gender = gender;
    }
    
    public static void main(String[] args) {
        Person mary = new Person("Mary", Gender1.FEMALE);
        System.out.println("name = " + mary.getName() + "\t" + "gender = " + mary.getGender());
    }
}

 程序异常提示是:初始化的时候错误;但是造成这种异常的根源是Gender1中的代码的问题。这个我还真是找不出来了,麻烦谁能找出来的,小弟不胜感谢。

  • 写回答

1条回答 默认 最新

  • y7997883 2010-04-24 18:10
    关注

    private static Map instancesBySex =

    new HashMap();

    放到Gender1的开头就可以了,

    是加载顺序的问题,当运行到
    Person mary = new Person("Mary", Gender1.FEMALE);

    这行时,系统加载Gender1类,当加载到
    public static final Gender1 FEMALE = new Gender1(new Character('F'), "FEMALE");
    这行时,由于还没有初始化instancesBySex ,此时instancesBySex 为null,而上面用到了instancesBySex 的put方法,出现空指针。

    使用调试模式,一步一步执行,查看对象的属性就可以发现问题所在了。

    自认为instancesBySex 没有必要存在,
    public static Collection getAllValues() {

    return Collections.unmodifiableCollection(instancesBySex.values());

    }

    public static Gender1 getInstance(Character sex) {   
    
        /*按照参数指定的性别缩写查找Gender1实例*/   
        Gender1 result = (Gender1)instancesBySex.get(sex);   
        if (result == null) {   
            throw new NoSuchElementException(sex.toString());   
        }   
        return result;   
    }   
    

    可修改为
    public static Gender1[] getAllValues() {
    return new Gender1[]{Gender1.FEMALE,Gender1.MALE};
    }
    public static Gender1 getInstance(Character sex) {

        if(set.equals(new Character("F")) {
             return Gender1.FEMALE;
        } else {
             return Gender1.MALE;
        }
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 gwas 分析-plink 检查人口分层出现下面问题
  • ¥15 关于#matlab#的问题:需要 MATLAB 运行程序,对比四个多址通信方式(TDMA,FDMA,CDMA,OFDMA)的抗噪声性能
  • ¥15 有没有复现过PFENet的,如何解决?
  • ¥15 FDMA,OFDMA,TDMA 和 CDMA 系统容量对比仿真
  • ¥15 图神经网络的入侵检测
  • ¥15 工业数据采集技术+存储架构推荐
  • ¥15 GPT-SOVITS如何训练少数民族语言支持合成语音
  • ¥20 树莓派4b使用Camera Module 3时出现the system should be configured for the legacy camera stack问题
  • ¥200 GitHub开源程序配置在VScode调试
  • ¥15 爬虫保存的scv文件0kb