JavaFX开发文本分析工具使用HBox报构造异常如何解决
问题根因
报错堆栈最底层的NullPointerException已经明确指向问题根源:你为HBox添加的子节点为null。
你在类成员初始化阶段就执行了HBox hbox = new HBox(button, button2, button3);,但此时三个Button变量仅完成声明、没有完成实例化,默认值都是null,JavaFX布局容器不允许添加null作为子节点,因此构造HBox时直接抛出异常,导致Application实例初始化失败。
修复方案
第一步 调整HBox初始化逻辑
修改HBox的初始化方式,避免传入未实例化的按钮:
原有错误的成员变量代码:
Button button, button2, button3; HBox hbox = new HBox(button, button2, button3);
修改为:
Button button, button2, button3; // 初始化为空HBox即可 HBox hbox = new HBox();
你原有start()方法中已经写了hbox.getChildren().addAll(button, button2, button3);,此时按钮已经完成实例化,可以正常添加到HBox中。
第二步 修复if判断语法错误
你代码中所有if判断后都多写了多余的分号,会导致判断逻辑完全失效:
// 错误写法:分号会直接结束if判断,后面的代码块无论条件是否满足都会执行 if (event.getSource() == button) ; { // 逻辑代码 }
把每个if后面的分号删除即可:
if (event.getSource() == button) { // 逻辑代码 }
否则点击任意按钮都会触发所有按钮的执行逻辑,无法实现分步操作的预期效果。
修改后核心代码示例
public class Text_Processor extends Application implements EventHandler<ActionEvent> { Button button, button2, button3; HBox hbox = new HBox(); public static void main(String[] args) throws Exception { launch(args); } @Override public void start(Stage primaryStage) throws Exception { primaryStage.setTitle("Word Analyzer"); button = new Button(); button.setText("Click First to Fetch The Raven Text"); button.setOnAction(this); button2 = new Button(); button2.setText("Click Here Next to parse the file"); button2.setOnAction(this); button3 = new Button(); button3.setText("Click Here Last to display results"); button3.setOnAction(this); hbox.getChildren().addAll(button, button2, button3); // 可添加间距优化布局显示 hbox.setSpacing(10); Scene scene = new Scene(hbox, 600, 100); primaryStage.setScene(scene); primaryStage.show(); } @Override public void handle(ActionEvent event) { if (event.getSource() == button) { System.out.println("Fetching Text From Source https://www.gutenberg.org/files/1065/1065-h/1065-h.htm"); URL url; try { url = new URL("https://www.gutenberg.org/files/1065/1065-h/1065-h.htm"); BufferedReader readr = new BufferedReader(new InputStreamReader(url.openStream())); BufferedWriter writer = new BufferedWriter(new FileWriter("Test_Download.txt")); String line; while ((line = readr.readLine()) != null) { writer.write(line); } readr.close(); writer.close(); } catch (IOException e) { System.out.println("Error: " + e.getMessage()); e.printStackTrace(); } } if (event.getSource() == button2) { System.out.println("Parsing text of your downloaded file..."); try { File input = new File("Test_Download.txt"); Document doc = Jsoup.parse(input, "UTF-8"); String body = doc.body().text(); BufferedWriter writer = new BufferedWriter(new FileWriter("Test_Download.txt")); writer.write(body); writer.close(); } catch (IOException e) { System.out.println("Error: " + e.getMessage()); e.printStackTrace(); } } if (event.getSource() == button3) { try { File file = new File("Test_Download.txt"); Scanner sc = new Scanner(file); int i = 0, indexOfWord = 0, count = 0; List<String> words = new ArrayList<String>(); List<Integer> wordCount = new ArrayList<Integer>(); while (sc.hasNext()) { String word = sc.next(); word = word.replaceAll("\\p{Punct}", " "); if (words.contains(word)) { indexOfWord = words.indexOf(word); count = wordCount.get(indexOfWord); count = count + 1; wordCount.add(indexOfWord, count); } else { words.add(i, word); wordCount.add(i, 1); i++; } } sc.close(); Integer max = Collections.max(wordCount); System.out.println("max word occurence is:" + max); System.out.println("|Count|Word"); System.out.println("|-----|-----------------|"); int no_of_elements = words.size(); while (max != 0) { for (int j = 0; j < no_of_elements; j++) { String word = words.get(j); count = wordCount.get(j); if (count == max) { System.out.printf("|%-4d", count); System.out.printf(" |" + word + "\n"); } } max--; } } catch (IOException e) { System.out.println("Error: " + e.getMessage()); e.printStackTrace(); } } } }
内容的提问来源于stack exchange,提问作者dheeke
相关产品推荐
相关产品推荐

