2017-02-24 200 views
-3

爲什麼這不起作用?我使用SimpleDateFormat解析字符串,但是這在try {}中的行上產生錯誤。解析字符串日期錯誤

String timerDate = "28 05:24:50"; // timer will stop when reached this date 

public String getTimer() { 
    SimpleDateFormat format = new SimpleDateFormat("dd HH:mm:ss"); 

    Date currentDate = Calendar.getInstance().getTime(); 
    Date stopDate = null; 
    try { 
     stopDate = format.parse(timerDate); 
    } catch (ParseException e) { 
     e.printStackTrace(); 
     return ""; 
    } 

(有更多的代碼繼續,但它是不相關的,不能解析

異常消息:。

java.lang.NullPointerException 
    at java.text.SimpleDateFormat.parse(Unknown Source) ~[?:1.8.0_121] 
    at java.text.DateFormat.parse(Unknown Source) ~[?:1.8.0_121] 
    at com.thefuzzyhead.octopia.util.general.MOTDManager.getTimer(MOTDManager.java:98) ~[?:?] 
    at com.thefuzzyhead.octopia.util.general.MOTDManager.<init>(MOTDManager.java:22) ~[?:?] 
    at com.thefuzzyhead.octopia.Main.onEnable(Main.java:37) ~[?:?] 
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot-1.8.jar:git-Spigot-eb87f2b-ee6d0fa] 
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:335) [spigot-1.8.jar:git-Spigot-eb87f2b-ee6d0fa] 
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot-1.8.jar:git-Spigot-eb87f2b-ee6d0fa] 
    at org.bukkit.craftbukkit.v1_8_R1.CraftServer.loadPlugin(CraftServer.java:356) [spigot-1.8.jar:git-Spigot-eb87f2b-ee6d0fa] 
    at org.bukkit.craftbukkit.v1_8_R1.CraftServer.enablePlugins(CraftServer.java:316) [spigot-1.8.jar:git-Spigot-eb87f2b-ee6d0fa] 
    at net.minecraft.server.v1_8_R1.MinecraftServer.q(MinecraftServer.java:402) [spigot-1.8.jar:git-Spigot-eb87f2b-ee6d0fa] 
    at net.minecraft.server.v1_8_R1.MinecraftServer.k(MinecraftServer.java:370) [spigot-1.8.jar:git-Spigot-eb87f2b-ee6d0fa] 
    at net.minecraft.server.v1_8_R1.MinecraftServer.a(MinecraftServer.java:325) [spigot-1.8.jar:git-Spigot-eb87f2b-ee6d0fa] 
    at net.minecraft.server.v1_8_R1.DedicatedServer.init(DedicatedServer.java:211) [spigot-1.8.jar:git-Spigot-eb87f2b-ee6d0fa] 
    at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java:505) [spigot-1.8.jar:git-Spigot-eb87f2b-ee6d0fa] 
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_121] 
+0

發佈您正在看到的StackTrace。 –

+0

https://hastebin.com/agetuporus.css MOTDManager是類,線98是在嘗試。 – Stephen

+0

timerDate最可能爲空,而不是您聲稱它包含的字符串。使用你的調試器。 http://ideone.com/4i4WEz –

回答

0

您的問題是timerDateparse()調用之前尚未確定。

確保timerDate在解析之前被初始化,或者防止其爲空。