Reputation: 237
The code I have, creates a set buttons but are vertically aligned
Now what I wanna do is add a JLabel to this panel but when I create an new JPanel, I get an error when I run it, but code doesn't give me any conflicts. And I would like to rearrange the buttons in a flowlayout if possible.
my full code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public final class CharSearch extends Box {
int i = 0;
int error = 0;
static JPanel panel;
String original = "Dinosaur";
String secret = new String(new char[original.length()]).replace('\0', '-');
public CharSearch() {
super(BoxLayout.Y_AXIS);
for (char i = 'A'; i <= 'Z'; i++) {
String buttonText = new Character(i).toString();
JButton button = getButton(buttonText);
add(button);
}
JLabel label = new JLabel(secret);
JPanel panel = new JPanel();
panel.add(label);
}
public JButton getButton(final String text) {
final JButton button = new JButton(text);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (original.indexOf(text) != -1) {
JOptionPane.showConfirmDialog(null,
"Your word does contain" + text);
} else {
JOptionPane.showConfirmDialog(null, "There is no" + text);
}
// If you want to do something with the button:
button.setText("Clicked"); // (can access button because it's
// marked as final)
}
});
return button;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setContentPane(new CharSearch());
frame.pack();
frame.setVisible(true);
new CharSearch();
}
});
}
}
and this is the error I get when I run it
Exception
is:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JFrame.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at org.informatica.com.CharSearch$2.run(CharSearch.java:50)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Upvotes: 1
Views: 5635
Reputation: 28687
You are adding an undefined JPanel object to your JFrame, resulting in a NullPointerException
, as is noted in the Container#add(Component)
documentation.
frame.add(panel);
You must instantiate panel
before adding it to your JFrame's content pane container.
Upvotes: 4
Reputation: 6783
For the NullPointerException
it's because of this line:
frame.add(panel);
While you've created a global variable panel
you are not instantiating it before adding it to the frame
Upvotes: 3