我是java和mvc的新手,并尝试通过将业务逻辑和前端分开来进行示例 这个小应用程序可以执行以下操作: 我们有一个textField和label。 如果您在textField上写一些东西,那么您将在标签上成为文本...它将计算出哪个字母的出现率最高。
我正在尝试运行该应用程序,但是我成为线程“ JavaFX Application Thread”中的异常java.lang.RuntimeException:java.lang.reflect.InvocationTargetException
踪迹:
"C:\Program Files\Java\jdk1.8.0_181\bin\java.exe" "-javaagent:C:\Users\khale\AppData\Local\JetBrains\IntelliJ IDEA 2019.1.2\lib\idea_rt.jar=62820:C:\Users\khale\AppData\Local\JetBrains\IntelliJ IDEA 2019.1.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_181\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\rt.jar;C:\Users\khale\OneDrive\Dokumente\NetBeansProjects\ShowCharWithHighestOccurrence\target\classes" com.mycompany.showcharwithhighestoccurrence.MainApp
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$KeyHandler.process(Scene.java:3964)
at javafx.scene.Scene$KeyHandler.access$1800(Scene.java:3910)
at javafx.scene.Scene.impl_processKeyEvent(Scene.java:2040)
at javafx.scene.Scene$ScenePeerListener.keyEvent(Scene.java:2501)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:217)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:149)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleKeyEvent$352(GlassViewEventHandler.java:248)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(GlassViewEventHandler.java:247)
at com.sun.glass.ui.View.handleKeyEvent(View.java:546)
at com.sun.glass.ui.View.notifyKey(View.java:966)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
... 29 more
Caused by: java.lang.NullPointerException
at com.mycompany.showcharwithhighestoccurrence.javafxFrontend.FXMLController.showCharWithHighestOccurence(FXMLController.java:34)
... 39 more
Scene.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200.0" prefWidth="320.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="com.mycompany.showcharwithhighestoccurrence.javafxFrontend.FXMLController">
<children>
<TextField fx:id="textField" layoutX="14.0" layoutY="90.0" onKeyReleased="#showCharWithHighestOccurence" prefWidth="209.0" />
<Label fx:id="outputLabel" layoutX="14.0" layoutY="112.0" prefHeight="32.0" prefWidth="293.0" text="" />
</children>
</AnchorPane>
MainApp.java
package com.mycompany.showcharwithhighestoccurrence;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class MainApp extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/fxml/Scene.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add("/styles/Styles.css");
scene.onKeyTypedProperty();
stage.setTitle("JavaFX and Maven");
stage.setScene(scene);
stage.show();
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
FXMLController.java(控制器)
package com.mycompany.showcharwithhighestoccurrence.javafxFrontend;
import java.net.URL;
import java.util.*;
import com.mycompany.showcharwithhighestoccurrence.javafxBusinessLogic.BusinessLogic;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
public class FXMLController implements Initializable {
@FXML
private TextField textField;
@FXML
private Label outputLabel;
private BusinessLogic businessLogic;
@FXML
private void showCharWithHighestOccurence(KeyEvent event) {
String input, output = "";
businessLogic.hello();
if (textField.getText() != null) {
input = textField.getText();
if (businessLogic.giveFinalResult(input) != null) {
output = businessLogic.giveFinalResult(input);
if (output != null) {
outputLabel.setText(output);
}
}
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
BusinessLogic.java
package com.mycompany.showcharwithhighestoccurrence.javafxBusinessLogic;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class BusinessLogic {
public void hello(){
System.out.println("helllllo");
}
public String giveFinalResult(String text) {
String input = " ";
String output = " ";
if (text != null) {
input = text.toLowerCase().replaceAll("\\s+", "");
int[] freq = new int[input.length()];
int i, j, max, lastFreq;
lastFreq = 0;
//Converts given string into character array
char inputChars[] = input.toCharArray();
List<Character> maxChars = new ArrayList<>();
//Count each word in given string and store in array freq
for (i = 0; i < inputChars.length; i++) {
freq[i] = 1;
for (j = i + 1; j < inputChars.length; j++) {
if (inputChars[i] == inputChars[j] && inputChars[i] != ' ' && inputChars[i] != '0') {
freq[i]++;
//Set string[j] to 0 to avoid printing visited character
inputChars[j] = '0';
}
}
}
//Determine maximum occurring characters
if (freq.length > 0) {
lastFreq = freq[0];
}
for (i = 0; i < freq.length; i++) {
max = freq[0];
if (freq[i] == lastFreq) {
max = lastFreq;
}
if (max == lastFreq && max < freq[i]) {
lastFreq = freq[i];
maxChars.add(inputChars[i]);
for (Character c : maxChars) {
if (c != null) {
output = "The most frequent letter is " + c + " with: " + freq[i] + "occurences";
}
}
}
if (lastFreq < freq[i]) {
maxChars.clear();
maxChars.add(inputChars[i]);
for (Character c : maxChars) {
if (c != null) {
output = "The most frequent letter is " + c + " with: " + freq[i] + "occurences";
}
}
}
if (max > 1 && max == freq[i]) {
maxChars.add(inputChars[i]);
Iterator iterator = maxChars.iterator();
while (iterator.hasNext()) {
output = "The most frequent letter are " + maxChars.get(0).toString() + ", "
+ iterator.next().toString() + " with: " + freq[i] + "occurences";
}
}
}
return output;
}
}
答案 0 :(得分:3)
发生这种情况是因为您没有在FXMLController中初始化您的业务逻辑字段
替换
Private BusinessLogic businessLogic;
通过
private BusinessLogic businessLogic = new BusinessLogic();