gpt4 book ai didi

java - 将字符串转换为 Joda DateTime 对象时出现问题

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

将字符串转换为 Joda DateTime 对象时出现问题。我不明白我做错了什么。我正在按照 StackOverflow 上其他线程的说明进行操作,但以下代码不起作用。它抛出这个异常:

Exception in thread "main" java.lang.NullPointerException
at org.joda.time.format.DateTimeFormatterBuilder$NumberFormatter.parseInto(DateTimeFormatterBuilder.java:1330)
at org.joda.time.format.DateTimeFormatterBuilder$Composite.parseInto(DateTimeFormatterBuilder.java:2741)
at org.joda.time.format.DateTimeFormatter.parseDateTime(DateTimeFormatter.java:853)
at co.restclient.ExpiringCookie.<init>(ExpiringCookie.java:41)
at co.restclient.ExpiringCookie.main(ExpiringCookie.java:29)

这是代码。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

/*
* This class stores a cookie value and can tell when its expired by its timestamp
*/
public class ExpiringCookie {

public static DateTimeFormatter fmt = DateTimeFormat.forPattern( "dd/MM/YYYY HH:mm:ss" );
public static File cookieJar = new File("cookie.xml");
public String singleCookie;
DateTime startTime;
DateTime currentTime;
boolean isExpired = false;

public static void main( String[] args ) {
ExpiringCookie ec = new ExpiringCookie( "xxxxxxx" );
System.out.println( "Age of cookie: " + ec.timePassed().getMillis() );
}

public ExpiringCookie( String cookie ) {
currentTime = new DateTime();
Properties properties = new Properties();
if ( cookieJar.exists() ) {
try {
properties.load( new FileInputStream( cookieJar ) );
new DateTime();
//startTime = DateTime.parse( properties.getProperty( "startTime" ), DateTimeFormat.forPattern( "dd/MM/YYYY HH:mm:ss" ) );
startTime = fmt.parseDateTime( properties.getProperty( "startTime" ) );
singleCookie = properties.getProperty( "cookie" );
} catch ( FileNotFoundException e ) {
e.printStackTrace();
} catch ( IOException e ) {
e.printStackTrace();
}
} else {
properties.setProperty( "startTime", fmt.print( currentTime ) );
properties.setProperty( "cookie", cookie );
startTime = new DateTime( currentTime.plusSeconds(-1) );
try {
properties.storeToXML( new FileOutputStream( cookieJar ), "Stores cookie for API requests" );
} catch ( FileNotFoundException e ) {
e.printStackTrace();
} catch ( IOException e ) {
e.printStackTrace();
}
}
}

public Duration timePassed() {
return new Duration( startTime, currentTime );
}

public boolean isExpired() {
return isExpired;
}

}

cookie.xml文件内容是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Stores cookie for API requests</comment>
<entry key="cookie">xxxxxxx</entry>
<entry key="startTime">03/01/2014 10:00:08</entry>
</properties>

最佳答案

第 41 行中的表达式 properties.getProperty( "startTime") 似乎返回 null

检查 JavaDoc 中的 Properties#getProperty

它说

Searches for the property with the specified key in this property list. If the key is not found in this property list, the default property list, and its defaults, recursively, are then checked. The method returns null if the property is not found.

因此,文件 data/cookie.xml 中的键 startTime 似乎没有值

我假设您使用最新的joda time版本2.3。 In line 1330 text.length() 被调用。正如我上面假设的,如果 properties.getProperty( "startTime") 返回 null,您将获得 NPE。

解决方案:确保文件data/cookie.xml中的键startTime有一个值

关于java - 将字符串转换为 Joda DateTime 对象时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20909932/

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