gpt4 book ai didi

java - 一个java代码在两台不同的电脑上表现不同

转载 作者:行者123 更新时间:2023-12-01 09:49:25 25 4
gpt4 key购买 nike

我有简单的 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工作正常。返回 项目:2016 年 5 月 12 日星期四 10:44:05 CEST 我无法弄清楚这种行为的原因是什么。也许有一些假设?

更新:当我删除注释 @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)

最佳答案

您的 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);
}
}

关于java - 一个java代码在两台不同的电脑上表现不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37701854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com