Skip to content

Commit

Permalink
fix: NPE caused by PoemScreen
Browse files Browse the repository at this point in the history
 Fixed an NPE caused by PoemScreen when not connected to the network.
  • Loading branch information
Wudji committed Jan 18, 2024
1 parent c8c5b25 commit f2438c7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 46 deletions.
44 changes: 21 additions & 23 deletions src/main/java/com/wudji/lessonlist/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
import com.wudji.lessonlist.utils.ExceptionManager;
import com.wudji.lessonlist.utils.FileControl;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.*;
import java.util.Timer;
import java.util.TimerTask;

Expand All @@ -20,8 +19,8 @@ public class MainActivity {
public static WindowConfig globalConfig = FileControl.getWindowConfig();
public static Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
public static String base_version = "v1.1.2";
public static String product_version = "v1.2.4-poem-suggestion-tjyz";
public static PoemScreen poemScreen;
public static String product_version = "v1.2.5-poem-suggestion-tjyz";
public static PoemScreen poemScreen = new PoemScreen(new Point(1,1));
public static MainWindow window;
public static WelcomeScreen welcomeScreen;
public static NoticeScreen noticeScreen;
Expand All @@ -42,25 +41,8 @@ public static void main(String[] args){
Timer timer = new Timer();

if (globalConfig.isEnablePoemSuggestion()){
Thread thread = new Thread(() -> {
Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler());

poemScreen = new PoemScreen(window.getLocation());
noticeScreen.updatePosition(poemScreen.getHeight());

timer.schedule(new TimerTask() {
@Override
public void run() {
refreshPoemInfo(poemScreen);
}

private void refreshPoemInfo(PoemScreen poemScreen){

poemScreen.updatePoemInfo();
}
}, 3600000,3600000);
});
thread.start();
poemScreen = new PoemScreen(window.getLocation());
noticeScreen.updatePosition(poemScreen.getHeight());
}

// 欢迎界面
Expand Down Expand Up @@ -95,6 +77,22 @@ private void updateA() {
window.update();
}
},1,1000);

Thread thread = new Thread(() -> {
Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler());
timer.schedule(new TimerTask() {
@Override
public void run() {
refreshPoemInfo(poemScreen);
}

private void refreshPoemInfo(PoemScreen poemScreen){

poemScreen.updatePoemInfo();
}
}, 3600000,3600000);
});
thread.start();
}
}
class CustomExceptionHandler implements Thread.UncaughtExceptionHandler{
Expand Down
55 changes: 34 additions & 21 deletions src/main/java/com/wudji/lessonlist/Screens/PoemScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,19 @@ public void updatePoemInfo(){
}

// 标签行解析
JSONArray suggestReasonArray = JSONObject.parseObject(poemDataString).getJSONObject("data").getJSONArray("matchTags");
StringBuilder suggestReasonBuilder = new StringBuilder();
for (Object suggestObj : suggestReasonArray) {
suggestReasonBuilder.append(suggestObj.toString()).append(" | ");
try{
JSONArray suggestReasonArray = JSONObject.parseObject(poemDataString).getJSONObject("data").getJSONArray("matchTags");
StringBuilder suggestReasonBuilder = new StringBuilder();
for (Object suggestObj : suggestReasonArray) {
suggestReasonBuilder.append(suggestObj.toString()).append(" | ");
}
JLabel reasonLabel = new JLabel("诗词标签: " + suggestReasonBuilder.toString());
reasonLabel.setFont(FileControl.getFont(Font.PLAIN,(int)(MainActivity.globalConfig.getNoticeFontSize() * 0.6)));
panel.add(reasonLabel);
}
catch (Exception ignored){

}
JLabel reasonLabel = new JLabel("诗词标签: " + suggestReasonBuilder.toString());
reasonLabel.setFont(FileControl.getFont(Font.PLAIN,(int)(MainActivity.globalConfig.getNoticeFontSize() * 0.6)));
panel.add(reasonLabel);

// 添加信息按钮
JButton refreshButton = new JButton("🔎 诗词信息");
Expand All @@ -111,21 +116,29 @@ public void updatePoemInfo(){

private NoticeLine[] resultResolve(){
NoticeLine[] poemInfo = new NoticeLine[2];
// 诗歌句子解析
JSONObject fullData = JSONObject.parseObject(poemDataString);
// System.out.println(fullData.getString("status"));
if(Objects.equals(fullData.getString("status"), "success")){
JSONObject poemData = fullData.getJSONObject("data");
JSONObject originData = poemData.getJSONObject("origin");

poemInfo[0] = new NoticeLine(poemData.getString("content"),"bold",0,0,0,0);
poemInfo[1] = new NoticeLine("——" + originData.getString("author") + "《"+ originData.getString("title") +"》","italic",0,0,0,0);

}else if(Objects.equals(fullData.getString("status"), "error")){
poemInfo[0] = new NoticeLine("诗词获取失败,何故?","bold",0,0,0,0);
poemInfo[1] = new NoticeLine("——错误代码:" + fullData.getInteger("errCode").toString() + ";错误信息"+ fullData.getString("errMessage") +"。","italic",0,0,0,0);
try{

// 诗歌句子解析
JSONObject fullData = JSONObject.parseObject(poemDataString);
// System.out.println(fullData.getString("status"));
if(Objects.equals(fullData.getString("status"), "success")){
JSONObject poemData = fullData.getJSONObject("data");
JSONObject originData = poemData.getJSONObject("origin");

poemInfo[0] = new NoticeLine(poemData.getString("content"),"bold",0,0,0,0);
poemInfo[1] = new NoticeLine("——" + originData.getString("author") + "《"+ originData.getString("title") +"》","italic",0,0,0,0);

}else if(Objects.equals(fullData.getString("status"), "error")){
poemInfo[0] = new NoticeLine("诗词获取失败,何故?","bold",0,0,0,0);
poemInfo[1] = new NoticeLine("——错误代码:" + fullData.getInteger("errCode").toString() + ";错误信息"+ fullData.getString("errMessage") +"。","italic",0,0,0,0);
}
return poemInfo;
}
catch (Exception ignored){
poemInfo[0] = new NoticeLine("本次诗词获取失败","bold",0,0,0,0);
poemInfo[1] = new NoticeLine("请检查你的网络连接,或禁用每日诗词功能","italic",0,0,0,0);
return poemInfo;
}
return poemInfo;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ private static String sendGetRequestWithHeader(String urlStr, String[]... header
in.close();
} else {
response.append("GET request failed. Response Code: ").append(responseCode);
ExceptionManager.showErrorDialog(new Throwable(response.toString()));
ExceptionManager.showErrorDialog(new Exception(response.toString()));
}
} catch (Exception e) {
response.append("An error occurred: ").append(e.getMessage());
ExceptionManager.showErrorDialog(new Throwable(response.toString()));
// ExceptionManager.showErrorDialog(new Exception(response.toString()));
}

return response.toString();
Expand Down

0 comments on commit f2438c7

Please sign in to comment.