使用Java中的Runnables枚举的nullPointerException(nullPointerException with enum using Runnables in Java)

编程入门 行业动态 更新时间:2024-10-27 15:24:05
使用Java中的Runnables枚举的nullPointerException(nullPointerException with enum using Runnables in Java)

我已经阅读了nullPointerExceptions的解释,我知道当被指向的值具有空值时,就好像它正在引用一个对象但是我无法理解为什么我得到一个枚举被用于某种程度像switch语句,但当然不是实际使用switch语句。 像我提到的那样,预期的行为应该像switch语句。 也许我错过了对枚举和NPE概念的基本理解。 我的代码如下。 (此代码适用于Enigma密码机)

void displayMenu() { System.out.println("MAIN MENU"); System.out.println(); Stream.of(menuChoice.values()).map(stream -> stream.ordinal() + ". " + stream.msg).forEach(System.out::println); System.out.println(); } public enum menuChoice { QUIT("Quit the Enigma", EnigmaMachine.instance.quitAction), ENCRPYT("Encrypt", EnigmaMachine.instance.encryptAction), DECRYPT("Decrypt", EnigmaMachine.instance.decryptAction); private String msg; public Runnable action; private menuChoice(String message, Runnable r) { this.msg = message; this.action = r; } } menuChoice getUserChoice() { System.out.print("Please enter your choice: "); int choice = s.nextInt(); return menuChoice.values()[choice]; }

在QUIT常量我使用runnable设置:

final Runnable quitAction = () -> { EnigmaMachine.instance.running = false; };

stacktrace是这样的:

Exception in thread "main" java.lang.ExceptionInInitializerError at enigmamachine.EnigmaMachine.displayMenu(EnigmaMachine.java:24) at enigmamachine.EnigmaMachine.<init>(EnigmaMachine.java:85) at enigmamachine.EnigmaMachine.main(EnigmaMachine.java:91) Caused by: java.lang.NullPointerException at enigmamachine.EnigmaMachine$menuChoice.<clinit>(EnigmaMachine.java:30) ... 3 more Java Result: 1

我的构造函数是这样的:

public EnigmaMachine() { this.running = true; while (this.running) { displayMenu(); getUserChoice().action.run(); } }

对象实例刚好位于我的代码底部的main函数之上。

static EnigmaMachine instance; public static void main(String[] args) { instance = new EnigmaMachine(); }

我不一定在寻找解决方案,但也许是对我正在处理的问题的更可靠的解释。 非常感谢。

I have read through the explanations of nullPointerExceptions and I know that it is when a value being pointed to has a null value as though it is referencing an object but I just cannot get my head around why I am getting one with an enum being used somewhat like a switch statement, but of course not actually using a switch statement. The expected behaviour, like I mentioned, should be like a switch statement. Perhaps I am missing a fundamental understanding of both the enum and NPE concepts. My code is as follows. (This code is for an Enigma cipher machine)

void displayMenu() { System.out.println("MAIN MENU"); System.out.println(); Stream.of(menuChoice.values()).map(stream -> stream.ordinal() + ". " + stream.msg).forEach(System.out::println); System.out.println(); } public enum menuChoice { QUIT("Quit the Enigma", EnigmaMachine.instance.quitAction), ENCRPYT("Encrypt", EnigmaMachine.instance.encryptAction), DECRYPT("Decrypt", EnigmaMachine.instance.decryptAction); private String msg; public Runnable action; private menuChoice(String message, Runnable r) { this.msg = message; this.action = r; } } menuChoice getUserChoice() { System.out.print("Please enter your choice: "); int choice = s.nextInt(); return menuChoice.values()[choice]; }

On the QUIT constant I am using a runnable that sets:

final Runnable quitAction = () -> { EnigmaMachine.instance.running = false; };

The stacktrace is like this:

Exception in thread "main" java.lang.ExceptionInInitializerError at enigmamachine.EnigmaMachine.displayMenu(EnigmaMachine.java:24) at enigmamachine.EnigmaMachine.<init>(EnigmaMachine.java:85) at enigmamachine.EnigmaMachine.main(EnigmaMachine.java:91) Caused by: java.lang.NullPointerException at enigmamachine.EnigmaMachine$menuChoice.<clinit>(EnigmaMachine.java:30) ... 3 more Java Result: 1

My constructor is like this:

public EnigmaMachine() { this.running = true; while (this.running) { displayMenu(); getUserChoice().action.run(); } }

Object instance is defined just above main function which is at the bottom of my code.

static EnigmaMachine instance; public static void main(String[] args) { instance = new EnigmaMachine(); }

I am not necessarily looking for a solution, but perhaps a more relatable explanation of what I am dealing with. Many thanks.

最满意答案

你得到这个问题只是因为EnigmaMachine.instance在这里调用QUIT("Quit the Enigma", EnigmaMachine.instance.quitAction)时仍然为null 。 您不应该在构造函数中调用displayMenu()因为它太快了,您应该将其称为避免此问题的方法。

您可以添加一个方法启动到您的类,如下所示:

public void start() { this.running = true; while (this.running) { displayMenu(); getUserChoice().action.run(); } }

然后在main方法中调用它作为下一个

instance = new EnigmaMachine(); instance.start();

You get this issue simply because EnigmaMachine.instance is still null when it is call here QUIT("Quit the Enigma", EnigmaMachine.instance.quitAction). You should not call displayMenu() inside your constructor as it is too soon, you should call it a method to avoid this issue.

You could add for example a method start to your class as below:

public void start() { this.running = true; while (this.running) { displayMenu(); getUserChoice().action.run(); } }

Then call it in the main method as next

instance = new EnigmaMachine(); instance.start();

更多推荐

本文发布于:2023-08-02 05:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1370982.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Runnables   Java   enum   nullPointerException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!