Strange Swing Error when from calling RemoveAll() on subclass of JPanel - java

I'm working on a Sudoku puzzle generator and am running into some intermittent swing exceptions after/during a call to the RemoveAll() method of a JPanel. When I run in eclipse's debug mode, the exceptions do not appear. Here is the code for the class in question:
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
/**
* Represents a cell in the GUI Grid display
* #author alex
*
*/
public class CellGUI extends JPanel {
public CellGUI()
{
super();
this.setLayout(new GridLayout(3,3));
for(int i = 1;i <=9;i++)
{
add(new JLabel("" + i));
}
setVisible(true);
}
public void clear()
{
this.removeAll();
this.validate();
this.setLayout(new GridLayout(3,3));
for(int i = 1;i <= 9; i++)
{
add(new JLabel("" + i));
}
}
public void setValue(int newVal)
{
if (newVal == 0)
{
clear();
}
else
{
this.removeAll(); // this line appears to be the problem
//this.updateUI();
//this.setLayout(new FlowLayout());
//add(new JLabel("" + newVal));
}
}
}
It usually spits out this exception:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException
at javax.swing.LayoutComparator.compare(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at java.util.Collections.sort(Unknown Source)
at javax.swing.SortingFocusTraversalPolicy.enumerateAndSortCycle(Unknown Source)
at javax.swing.SortingFocusTraversalPolicy.getFirstComponent(Unknown Source)
at javax.swing.LayoutFocusTraversalPolicy.getFirstComponent(Unknown Source)
at javax.swing.SortingFocusTraversalPolicy.getDefaultComponent(Unknown Source)
at java.awt.FocusTraversalPolicy.getInitialComponent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(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.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.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.SequencedEvent.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.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.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)
I figure in my naivety I'm probably going about doing this the wrong way. Any ideas?
Edit: By request, I'm posting additional code.
This class calls the setValue method of the CellGUI:
public class GridGUI extends JPanel {
ArrayList<CellGUI> cells = new ArrayList<CellGUI>();
public GridGUI()
{
this.setLayout(new GridLayout(9,9));
for(int i = 0; i < 81;i++)
{
CellGUI cell = new CellGUI();
cells.add(cell);
add(cell);
}
}
public void updateGrid(Grid g)
{
for(int i = 0;i<81;i++)
{
cells.get(i).setValue(g.getValue(i));
}
}
}
The GridGUI class is created and managed by a class that extends JFrame:
public class GUI extends JFrame {
...
public GUI()
{
this.setLayout(new BorderLayout());
add(gridGUI,BorderLayout.CENTER);
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(isValidLabel);
panel.add(isSolvableLabel);
panel.setVisible(true);
add(panel, BorderLayout.SOUTH);
this.setSize(600, 600);
setVisible(true);
}
public void loadGrid(String path)
{
grid = new Grid(path);
grid.print();
gridGUI.updateGrid(grid);
updateLabels();
}
...
}
Which is called by my main class:
public static void main(String[] args)
{
GUI gui = new GUI();
gui.loadGrid(args[0]);
}

'Intermittent/occasional errors' in Swing apps. are often caused by updating the UI off the EDT. See Concurrency in Swing for more details.
Note that for better help sooner, post an SSCCE. If my suspicions above are the cause of the problem, then the problem is actually in code not shown.

So, looking at the stack-trace shows that the error occurs when the FocusManager is trying to get the first component. For this, your FocusTraversalPolicy tries to order the components in "layout order", which means roughly in columns and rows. For a GridLayout, this should be totally trivial. Let's look at the code of LayoutComparator. It throws a ClassCastException in some places:
if (a == null) {
// 'a' is not part of a Window hierarchy. Can't cope.
throw new ClassCastException();
}
a is either the containing Window here, or null if no such Window exists.
So, looks like your components are not yet totally registered in the Component tree. As the others said, this can occur if you do some changes in the GUI when not being in the AWT event dispatch thread.
To avoid this, wrap all changes to the GUI (including creating the components, like your constructor call above) in a call of EventQueue.invokeLater(...). (Sometimes invokeAndWait is more useful, and you also can use the same-named methods in SwingUtilities instead.) In your case, the change is quite simple:
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable() { public void run() {
GUI gui = new GUI();
gui.loadGrid(args[0]);
}});
}

Intermittent errors like this make me worry about calling Swing code off of the EDT, the Event Dispatch Thread, which is the main Swing thread and is responsible for Swing graphics and user interaction. Where and how are you calling the CellGUI#setValue(...) method? Could it be off of the EDT?

I don't see any reason to remove all the labels?
All you need to do us use setText(...) to change the text of the labels. No need to remove them all and then add them back.

Sometimes when you only intend a single thread, you get more and run into the updating from the wrong thread problem.
For me, it's happened when an application starts up and starts preparing a GUI.
Meanwhile, the mouse is sitting in the screen location where the GUI is going to be.
The GUI discovering the mouse is already in its space can happen asynchronously to the background work. Putting the mouse off in a corner of the screen makes the problem goes away.

Related

Creating JTextPane is leading to NullPointerException - OpenOffice Extension

I'm currently developing an Extension for OpenOffice. I'm using Java 1.6 and the OpenOffice SDK 4.1.2.
If I try to create a javax.swing.JTextPane, I get a NullpointerException in the Constructor of JTextPane.
public class Dialog extends javax.JFrame {
private final JTextPane jTextPane;
private final JTable jTable;
public Dialog() {
jTable = new JTable();
jTextPane = new JTextPane();
}
}
The Dialog is initialized in another Thread:
public class DialogManager {
private static JournalDialog journalDialog;
public void showDialog() {
Thread startThread = new Thread(new Runnable() {
#Override
public void run() {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
dialog = new Dialog();
...
}
}
}
}
}}
The creation of JTable works fine, but in the next line I get a Nullpointerexception
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.util.Hashtable.put(Unknown Source)
at javax.swing.JEditorPane.registerEditorKitForContentType(Unknown Source)
at javax.swing.JEditorPane.registerEditorKitForContentType(Unknown Source)
at javax.swing.JEditorPane.loadDefaultKitsIfNecessary(Unknown Source)
at javax.swing.JEditorPane.getKitTypeRegistry(Unknown Source)
at javax.swing.JEditorPane.getEditorKitClassNameForContentType(Unknown Source)
at javax.swing.JTextPane.<init>(Unknown Source)
at .gui.Dialog.<init>(Dialog.java:159)
at .gui.DialogManager$6$1.run(DialogManager.java:334)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.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)
I hope someone has an idea what causes this exception. I tried to run the extension in Java 1.7 and 1.8, but there is the same issue.
Best regards
Update 16.08.2016:
If I add a JTexPane over the Netbeans Palette into the Designer, it works. Only the initialisation in the constructor fails.
It may not be possible to use Swing for this task without crashing. Instead, use the com.sun.star.awt module. Complete examples are at http://api.libreoffice.org/examples/DevelopersGuide/examples.html#GraphicalUserInterfaces.
For more information, see Creating Dialogs at Runtime.
One more link: This example does use Swing. Try it to see if the same problem occurs.
My dirty solution is to initialize the JTextPane twice within a try-catch. Because on the second call i don't get an exception.

(java.lang.IllegalArgumentException) Error with gui

I am making a simple gui program in java. When i click run it gives me an error that looks like this:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: adding a window to a container
at java.awt.Container.checkNotAWindow(Unknown Source)
at java.awt.Container.addImpl(Unknown Source)
at javax.swing.JLayeredPane.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at javax.swing.JRootPane.setContentPane(Unknown Source)
at javax.swing.JFrame.setContentPane(Unknown Source)
at main.cool(main.java:31)
at main$1.run(main.java:43)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.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)
and here is my code:
import javax.swing.*;
import java.awt.Container;
import java.awt.event.*;
public class main extends JFrame implements ActionListener {
protected JButton click, fun;
public main()
{
click = new JButton("Click");
click.setActionCommand("click");
click.addActionListener(this);
add(click);
click.setSize(16, 16);
fun = new JButton("wow");
fun.setActionCommand("wow");
fun.addActionListener(this);
add(fun);
}
public static void cool()
{
JFrame frame = new JFrame("TEST!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main main = new main();
frame.setContentPane(main);
frame.setSize(128, 128);
frame.setVisible(true);
frame.setResizable(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run()
{
cool();
}
});
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if("click".equals(e.getActionCommand()))
{
System.out.println("oh right");
} else if ("wow".equals(e.getActionCommand()))
{
System.out.println("hi");
}
}
}
I believe the bug may be in the cool() method; with the setContentPane line. but not sure. Can anyone please help me.
Yes; the bug is triggered in the frame.setContentPanel() in your cool() method in your main class. Your main class should extend JPanel instead of extend JFrame.
Aside: avoid declaring local variables which match class names; and, avoid declaring class names which are the same as standard method names...so, rename your main class as Main (if you must, but try to be more descriptive...perhaps Application), and make your local variable name something other than main...perhaps m or application.
Your class main extends JFrame instead of JPanel, so you are calling frame.setContentPane() on another JFrame, and it doesn't make sense to have a JFrame inside a JFrame. Change the superclass to JPanel and the error will go away

Threads stopping, suspending, and resuming

I'm writing a code that involves JFrame, and a thread.
The thread should take the text from a text field and write it into text area.
I have 4 buttons as follows:
"Start" to start the thread.
"Stop" which stops the thread.
"Pause" which pause and continues the thread.
and "Exit" which stops the thread and exits the program.
I've created the thread and implemented "run()" function in frame's constructor, here it is:
WritingThread = new Thread(new Runnable() {
#Override
public void run() {
String s = WrittenText.getText();
while(true)
{
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < s.length(); j++)
{
WritingArea.append("" + s.charAt(j));
try {
Thread.sleep((int)ThreadSpeedSpinner.getValue());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
WritingArea.append("\n");
}
WritingArea.setText("");
}
}
});
and these are the buttons:
JButton btnStart = new JButton("Start");
btnStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(!WritingThread.isAlive())
WritingThread.start();
}
});
JButton btnStop = new JButton("Stop");
btnStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(WritingThread.isAlive())
WritingThread.stop();
}
});
btnPause = new JButton("Pause");
btnPause.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(!isPaused)
{
if(WritingThread.isAlive())
{
WritingThread.suspend();
btnPause.setText("Continue");
isPaused = true;
}
}
else
{
WritingThread.resume();
btnPause.setText("Pause");
}
}
});
JButton btnExit = new JButton("Exit");
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
WritingThread.stop();
System.exit(0);
}
});
I have two problems showing up:
When I use stop(), suspend(), or resume(), I get a warning says "The method from the type Thread is deprecated".
When I run the program, I start the thread, then stop it, then try to start it I have this exception
Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Unknown Source)
at com.HomeWork.HomeWork5$6.actionPerformed(HomeWork5.java:140)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$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)
I don't want direct answers, I want to understand why I'm getting these errors, and if there any resources I should go through.
P.S. I searched for an answer a lot and didn't get anything explaining this problem.
Thanks
Threads acquire lock on objects. And the most important part of multithreading
is to safely interweave the threads, so that all the threads can use the resource (object).
If not dealt with correctly, it leads to deadlock.
When you use stop(), you are killing the thread. That thread is gone forever. It
may lead the objects, that stopped thread had acquired, in a inconsistent state.
suspend() is deprecated, because once the thread is suspended other threads won't get the
resource, since the suspended thread holds a lock on it.
The image below describes how threads should be correctly used.
Use sleep(), wait(), and notify() for interleaving the threads efficiently.

JComboBox cascade

I am trying to design a three cascade JComboBox in JAVA:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class ThreeCascadeJComboBox {
private JComboBox combo1;
private JComboBox combo2;
private JComboBox combo3;
public static void main(String[] args) {
new ThreeCascadeJComboBox();
}
public ThreeCascadeJComboBox() {
JFrame v = new JFrame();
v.getContentPane().setLayout(new FlowLayout());
combo1 = new JComboBox();
loadCombo1();
combo1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
loadCombo2((String) combo1.getSelectedItem());
}
});
combo2 = new JComboBox();
loadCombo2((String) combo1.getSelectedItem());
combo2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
loadCombo3((String) combo2.getSelectedItem());
}
});
combo3 = new JComboBox();
loadCombo3((String) combo2.getSelectedItem());
v.getContentPane().add(combo1);
v.getContentPane().add(combo2);
v.getContentPane().add(combo3);
v.pack();
v.setVisible(true);
v.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
private void loadCombo1() {
combo1.addItem("letters");
combo1.addItem("numbers");
}
private void loadCombo2(String seleccionEnCombo1) {
combo2.removeAllItems();
if (seleccionEnCombo1.equals("letters")) {
combo2.addItem("A");
combo2.addItem("B");
combo2.addItem("C");
} else if (seleccionEnCombo1.equals("numbers")) {
combo2.addItem("1");
combo2.addItem("2");
combo2.addItem("3");
}
}
private void loadCombo3(String seleccionEnCombo2) {
combo3.removeAllItems();
if (seleccionEnCombo2.equals("A")) {
combo3.addItem("A-1");
combo3.addItem("A-2");
combo3.addItem("A-3");
} else if (seleccionEnCombo2.equals("B")) {
combo3.addItem("B-1");
combo3.addItem("B-2");
combo3.addItem("B-3");
} else if (seleccionEnCombo2.equals("C")) {
combo3.addItem("C-1");
combo3.addItem("C-2");
combo3.addItem("C-3");
} else if (seleccionEnCombo2.equals("1")) {
combo3.addItem("1-a");
combo3.addItem("1-b");
combo3.addItem("1-c");
} else if (seleccionEnCombo2.equals("2")) {
combo3.addItem("2-a");
combo3.addItem("2-b");
combo3.addItem("2-c");
} else if (seleccionEnCombo2.equals("3")) {
combo3.addItem("3-a");
combo3.addItem("3-b");
combo3.addItem("3-c");
}
}
}
But I get the next Exception when I select the numbers value in the jcombo1:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at es.mycompany.MyView.ThreeCascadeJComboBox.loadCombo3(ThreeCascadeJComboBox.java:78)
at es.mycompany.MyView.ThreeCascadeJComboBox.access$3(ThreeCascadeJComboBox.java:76)
at es.mycompany.MyView.ThreeCascadeJComboBox$2.actionPerformed(ThreeCascadeJComboBox.java:40)
at javax.swing.JComboBox.fireActionEvent(Unknown Source)
at javax.swing.JComboBox.contentsChanged(Unknown Source)
at javax.swing.JComboBox.intervalRemoved(Unknown Source)
at javax.swing.AbstractListModel.fireIntervalRemoved(Unknown Source)
at javax.swing.DefaultComboBoxModel.removeAllElements(Unknown Source)
at javax.swing.JComboBox.removeAllItems(Unknown Source)
at es.mycompany.MyView.ThreeCascadeJComboBox.loadCombo2(ThreeCascadeJComboBox.java:63)
at es.mycompany.MyView.ThreeCascadeJComboBox.access$1(ThreeCascadeJComboBox.java:62)
at es.mycompany.MyView.ThreeCascadeJComboBox$1.actionPerformed(ThreeCascadeJComboBox.java:30)
at javax.swing.JComboBox.fireActionEvent(Unknown Source)
at javax.swing.JComboBox.setSelectedItem(Unknown Source)
at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.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)
The exception is thrown because at that point seleccionEnCombo2 is null.
You could add a check for null in the combo2 ActionListener and it will work fine:
if (combo2.getSelectedItem() != null) {
loadCombo3((String) combo2.getSelectedItem());
}
The problem is that the ActionListener for combo1 is triggering an ActionEvent for combo2 which will not have any selected item (as it is empty). You could add a check:
if (combo2.getSelectedItem() != null) {
loadCombo3((String) combo2.getSelectedItem());
}
As the other posts have mentioned, the selected value for the combo is null in some cases. This is occurring because you probably do not realize the ActionListener for combo2 is getting called twice. The first time it gets called is during the call to removeAllElements. This is where the null value is coming from. The second time is what you are assuming in your code to be the only call--this is in response to both population of the combo box as well as user interaction.
When you load the second combo box, that fires the action event for that box (because an action has taken place [actions are not limited to selection]. The 2nd combo box's actionPerformed attempts to load the 3rd combo box based on the 2nd combo box's selection, and there isn't any. That's your null pointer, the non-existent selection from the second combo box.

Java Display message issue

I have a java program that creates a JFrame like this:
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
gui.setFrame(new gui(), 1000, 300);
}
});
I also have a class (gui.java) that implements setFrame:
public static void setFrame(final JFrame frame, final int width, final int height) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
f1=frame;
frame.setTitle("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.setVisible(true);
}
});
}
If the user tries to click Submit (a button I created) and the fields in the JFrame are not filled in then it throws an error. The code for the error message is:
submit.addMouseListener(new MouseListener(){
#Override
public void mouseClicked(MouseEvent e) {}
#Override
public void mouseEntered(MouseEvent e) {}
#Override
public void mouseExited(MouseEvent e) {}
#Override
public void mousePressed(MouseEvent e) {}
#Override
public void mouseReleased(MouseEvent e) {
//check to make sure all values filled in
if(chooser.getSelectedFile().toString()!=null&&saveChooser.getSelectedFile().toString()!=null)
parseFile.readFile(chooser.getSelectedFile(),saveChooser.getSelectedFile(),startSpanText.getText(),(String)col2.getSelectedItem(),(String)col3.getSelectedItem(),(String)col4.getSelectedItem(),(String)col5.getSelectedItem(),(String)col6.getSelectedItem());
else
JOptionPane.showMessageDialog(f1,"Bad");
}
});
//Note: f1 is a static version of the frame I initially received
The error I get is:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at gui$3.mouseReleased(gui.java:133)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$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)
What did I do wrong?
One of the variables is null. It could be:
chooser
chooser.getSelectedFile()
saveChooser
saveChooser.getSelectedFile()
parseFile
startSpanText
col2 to col6
Use the line number in the stack trace, and a debugger or traces in the code to know which one. My guess would be one of the selected files, since it makes no sense to call
chooser.getSelectedFile().toString() != null
Either there is no selected file, and it throws an NPE because getSelectedFile() returns null, or there is one, and its toString() will never be null.
Also, you shouldn't use a mouse listener to do something when a button is pressed. That's what an ActionListener is for. It will be simpler, and also work when the user presses the button with its keyboard.
Your exception message should point you to the answer. One of the items you are using in your mouseReleased method is null:
#Override
public void mouseReleased(MouseEvent e) {
//check to make sure all values filled in
if(chooser.getSelectedFile().toString()!=null &&
saveChooser.getSelectedFile().toString()!=null)
parseFile.readFile(chooser.getSelectedFile(), saveChooser.getSelectedFile(),
startSpanText.getText(), (String)col2.getSelectedItem(),
(String)col3.getSelectedItem(), (String)col4.getSelectedItem(),
(String)col5.getSelectedItem(), (String)col6.getSelectedItem());
else
JOptionPane.showMessageDialog(f1,"Bad");
}
Use a debugger to inspect which variables are set. Use the line number in the exception message to help you.

Categories

Resources