gpt4 book ai didi

java - 添加到 ArrayList 时出现 NullPointerException [长文]

转载 作者:行者123 更新时间:2023-12-01 16:52:34 27 4
gpt4 key购买 nike

我正在为一个类项目创建一个正则表达式解析器,但我遇到了一个不知道如何解决的问题。每当遇到带括号的组时,都应该向 groups ArrayList 添加一个占位符,然后可以从字符串中捕获组。问题在于,每当尝试添加到此 ArrayList 时,它都会抛出 NullPointerException。这是 Segment 构造函数,发生这种情况的地方:

public Segment(String value, boolean capture) throws ParseException {
values = new ArrayList<>();
parse(value);
minimum = 1;
maximum = 1;
matchNot = false;
captureGroup = capture;
if (capture) {
RegexGUI.input.groups.add(""); //error occurs on this line
}
}

请注意, RegexGUI.input 引用 Regex 的相关实例,其构造函数位于此处:

public Regex(String value) throws ParseException {
this.value = value;
segments = new ArrayList<>();
groups = new ArrayList<>();
anchorStart = false;
anchorEnd = false;
tokenize(value, true, false);
}

如您所见,groups 已在构造函数中明确初始化。另外, Segment 构造函数仅在 tokenize() 类中的 Regex 方法中调用,并且该方法仅由 Regex 构造函数调用,因此我不可能看到问题只是 groups 尚未初始化。

整个堆栈跟踪在这里:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at regexparser.Segment.<init>(Segment.java:39)
at regexparser.Regex.tokenize(Regex.java:57)
at regexparser.Regex.<init>(Regex.java:27)
at regexparser.RegexGUI.actionPerformed(RegexGUI.java:80)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

这是堆栈跟踪中提到的 tokenize() 中的代码行:

segments.add(new Segment(value.substring(0, value.indexOf(")")+1), true));

这是堆栈跟踪中提到的 actionPerformed() 中的代码行:

input = new Regex(regexInput.getText());

我不知道是否有必要发布,但这是每个类的实例和类变量:

Regex 类:

String value;
ArrayList<Segment> segments;
ArrayList<String> groups;
boolean anchorStart;
boolean anchorEnd;

Segment 类:

ArrayList<String> values;
int minimum;
int maximum;
boolean matchNot;
boolean captureGroup;

final static String UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
final static String LOWERCASE = "abcdefghijklmnopqrstuvwxyz";
final static String DIGITS = "0123456789";
final static String[] SETS = {UPPERCASE, LOWERCASE, DIGITS};
final static String WORD = UPPERCASE + LOWERCASE + DIGITS + "_";

final static String WHITESPACE = " \r\n\t\f";

RegexGUI 类:

Container contentPane;
JPanel panel;
GridBagLayout gridbag;
GridBagConstraints c;
JLabel regexPrompt, stringPrompt, result;
JTextField regexInput;
JTextArea stringInput;
JButton test;
static Regex input;
boolean match;

如果你们能给我任何帮助,我将不胜感激。我尝试查找可能导致此异常的原因,但我能找到的唯一答案是 groups 未初始化。我已经搜索了我的代码,试图找出在初始化 Segment 之前调用 groups 构造函数的任何方式,但我看不到任何可能的方式。

最佳答案

你的代码是第 22 条陷阱

这一行:

input = new Regex(regexInput.getText());

将创建 Regex 对象,然后分配它。

但是,在创建 Regex 实例时,您尝试在实际完成之前引用 Regex.input 实例,因此它尚未实际分配,并且实际上仍然为 null。

一个相当快速的修复方法是在构造对象后调用 tokenize 方法。因此,从构造函数中删除 tokenize 调用,然后运行如下所示的代码:

input = new Regex(regexInput.getText());
input.tokenize(...);

关于java - 添加到 ArrayList 时出现 NullPointerException [长文],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37038326/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com