我正在为类项目创建一个正则表达式解析器,我遇到了一个我无法弄清楚如何修复的问题。每当遇到带括号的组时,都应该在groups
ArrayList中添加一个占位符,然后可以从String中捕获一个组。问题是它每次尝试添加到此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)
这是stacktrace中提到的tokenize()
中的代码行:
segments.add(new Segment(value.substring(0, value.indexOf(")")+1), true));
这是stacktrace中提到的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
构造函数的方法,但我看不出任何可行的方法。
答案 0 :(得分:5)
你的代码是一个问题22
这一行:
input = new Regex(regexInput.getText());
将创建Regex对象,然后分配它。
但是,在创建Regex实例时,您尝试在实际完成之前引用Regex.input实例,因此它实际上尚未分配,实际上仍为null。
一个相当快速的解决方法是在构造对象后调用tokenize
方法。因此,从构造函数中删除tokenize
调用,然后运行类似:
input = new Regex(regexInput.getText());
input.tokenize(...);