Question: I having this error in my Java code (using a widgetview class given by instructor) and was curious if anyone can help me fix this
I having this error in my Java code (using a widgetview class given by instructor) and was curious if anyone can help me fix this error I am having.
Error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at StringAnalysis$2.actionPerformed(StringAnalysis.java:40)
Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class StringAnalysis {
static StringSet sSet;
public static void main(String[] args) {
//Create instance of WidgetView
WidgetView wv = new WidgetView();
//Create Labels, Fields, Buttons
JLabel sInput = new JLabel("Enter a String");
JButton submit = new JButton("Push to include String");
JTextField inputStr = new JTextField(10);
JLabel numStr = new JLabel("Number of Strings: 0");
JLabel numChar = new JLabel("Number of Characters: 0");
//Add to WidgetView
wv.add(sInput);
wv.add(inputStr);
wv.add(submit);
wv.add(numStr);
wv.add(numChar);
submit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sSet.addString(inputStr.getText());
inputStr.setText("");
numStr.setText("Number of Strings: " + sSet.getNumberofString());
numChar.setText("Number of Characters:" + sSet.getNumberOfChar());
}
});
inputStr.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sSet.addString(inputStr.getText());
inputStr.setText("");
numStr.setText("Number of Strings: " + sSet.getNumberofString());
numChar.setText("Number of Characters:" + sSet.getNumberOfChar());
}
});
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
