一个java代码在2个不同的PC上表现不同 [英] one java code behave differ on 2 differ pc

查看:94
本文介绍了一个java代码在2个不同的PC上表现不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有简单的JAXB解编码。

I have simple JAXB unmarshalling code.

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类

and MyDateFormatAdapter class

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)代码在第二台电脑上的行 System.out.println(示例); 上的NullPointerException失败(Windows 7,jdk 1.7)它工作还可以。返回项目:星期四5月12日10:44:05 CEST 2016
我无法弄清楚这种行为的原因是什么。也许是一些假设?

So, on the first pc(windows 7, jdk 1.7) code fails with NullPointerException at line System.out.println(example); on the second pc(windows 7, jdk 1.7) it work ok. Returns Item:Thu May 12 10:44:05 CEST 2016 I can't figure out what is the reason of such behavior. Maybe some assumption?

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

UPDATED: When I'm removing annotation @XmlJavaTypeAdapter(MyDateFormatAdapter.class) the code become work as on laptop and throws the same exceptions

 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)


推荐答案

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

Your MyDateFormatAdapter depends on the default platform locale of the JVM.

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

If that locale is not an english locale it will not be able to parse Thu for date pattern EEE. In this case dateFormat.parse will throw a ParseException. The Unmarshaller catches that exception but then the pubDate member will be initialized to null.

因此您的 Item.toString 引发NPE,因为它依赖于非null pubDate 值。

As a consequence your Item.toString raises a NPE since it relies on a non null pubDate value.

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

Solution: Specify a locale when you create the 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);
    }
}

这篇关于一个java代码在2个不同的PC上表现不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆