一个java代码在2个不同的PC上表现不同

时间:2016-06-08 11:57:41

标签: java jaxb java-7 environment

我有简单的JAXB解组代码。

package com.example;
import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

public class Main {

    public static void main(String[] args) {

        try {
        StringBuilder xml = new StringBuilder();
        xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
            .append("<item><pubDate>Thu, 12 May 2016 08:44:05 +0000</pubDate></item>");

        JAXBContext jaxbContext = JAXBContext.newInstance(Item.class);
        Unmarshaller jaxbMarshaller = jaxbContext.createUnmarshaller();

        StreamSource ss = new StreamSource(new StringReader( xml.toString()));
        Item example = (Item) jaxbMarshaller.unmarshal(ss);
        System.out.println(example);
      } catch (Exception e) {
        System.out.println("exception "+e);
            e.printStackTrace();
          }
    }
}

项目类:

package com.example;

import java.util.Date;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement(name = "item")
@XmlAccessorType(XmlAccessType.NONE)
public class Item {

    @XmlElement(name = "pubDate")
    @XmlJavaTypeAdapter(MyDateFormatAdapter.class)
    private Date pubDate;

    public String toString() {
        return "Item:"+this.pubDate.toString();
    }

    public Date getPubDate() {
        return pubDate;
    }

    public void setPubDate(Date pubDate) {
        this.pubDate = pubDate;
    }


}

和MyDateFormatAdapter类

package com.example;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class MyDateFormatAdapter extends XmlAdapter<String, Date> {

    private static final String FORMAT = "EEE, dd MMM yyyy HH:mm:ss Z";

    public Date unmarshal(String v) throws Exception {
        DateFormat dateFormat = new SimpleDateFormat(FORMAT);
        return dateFormat.parse(v);
    }

    public String marshal(Date d) throws Exception { 
        DateFormat dateFormat = new SimpleDateFormat(FORMAT);
        String formattedDate = dateFormat.format(d);
        return formattedDate; 

    }

}

因此,在第一台电脑上(Windows 7,jdk 1.7)代码在第二台电脑(Windows 7,jdk 1.7)的第System.out.println(example);行发生NullPointerException失败,它运行正常。返回Item:Thu May 12 10:44:05 CEST 2016 我无法弄清楚这种行为的原因是什么。也许有些假设?

更新: 当我删除注释@XmlJavaTypeAdapter(MyDateFormatAdapter.class)时,代码将在笔记本电脑上工作并抛出相同的异常

 exception java.lang.NullPointerException
    java.lang.NullPointerException
        at com.example.Item.toString(Item.java:21)
        at java.lang.String.valueOf(String.java:2849)
        at java.io.PrintStream.println(PrintStream.java:821)
        at com.example.Main.main(Main.java:27)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

1 个答案:

答案 0 :(得分:1)

您的MyDateFormatAdapter取决于JVM的默认平台区域设置。

如果该语言环境不是英语语言环境,则无法为日期模式Thu解析EEE。 在这种情况下,dateFormat.parse会抛出ParseException。 Unmarshaller捕获该异常,但pubDate成员将初始化为null。

因此,Item.toString会引发NPE,因为它依赖于非空pubDate值。

解决方案:在创建DateFormat时指定区域设置。

public class MyDateFormatAdapter extends XmlAdapter<String, Date> {

    private static final DateFormat dateFormat = 
        new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);

    @Override public Date unmarshal(String v) throws Exception {
        return dateFormat.parse(v);
    }

    @Override public String marshal(Date d) throws Exception { 
        return dateFormat.format(d);
    }
}