AWT-EventQueue-0 error - java

I have written the following code. Basically I want to activate autocomplete (in a RSyntaxTextArea environment) if the entered character is not space. Everything is working smoothly, but the fact that sometimes I will get the AWT-EventQueue-0 error. The program will still run and function but the error message shows up.
I have other elements in my code, but I am quite sure it is from this bit of added code. Any particular reason for this? I have noticed this error happening when I move the mouse to a second screen.
public static CompletionProvider provider = createModelCompletionProvider();
public static AutoCompletion ac = new AutoCompletion(provider);
public static KeyListener ListenerTOntology = new KeyListener() {
#Override
public void keyPressed(KeyEvent e) {}
#Override
public void keyReleased(KeyEvent e) {}
#Override
public void keyTyped(KeyEvent e) {
dumpInfo("Typed", e);}
private void dumpInfo(String s, KeyEvent e) {
char temp = e.getKeyChar();
if(temp!=' ' )
ac.doCompletion();}
};
Error message:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at org.fife.ui.rtextarea.FoldIndicator.findOpenFoldClosestTo(FoldIndicator.java:162)
at org.fife.ui.rtextarea.FoldIndicator.access$300(FoldIndicator.java:60)
at org.fife.ui.rtextarea.FoldIndicator$Listener.mouseMoved(FoldIndicator.java:671)
at java.awt.AWTEventMulticaster.mouseMoved(AWTEventMulticaster.java:329)
at java.awt.Component.processMouseMotionEvent(Component.java:6550)
at javax.swing.JComponent.processMouseMotionEvent(JComponent.java:3339)
at java.awt.Component.processEvent(Component.java:6274)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4505)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:702)
at java.awt.EventQueue$4.run(EventQueue.java:700)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

So, I discovered what caused this. You need to use a CaretListener instead of KeyListener. Since RSyntaxTextArea does not contain a DocumentListner.

Related

How to change icon of label with mouse entered event?

Here is the code of mouse entered event of label 1 . And the exception is occuring .I have already worked on previous version but I don't why this happening now.
private void jLabel1MouseEntered(java.awt.event.MouseEvent evt) {
ImageIcon n_cig;
n_cig = new ImageIcon(getClass().getResource("masterproject/Cfull.png"));
jLabel1.setIcon(n_cig);
}
private void jLabel1MouseExited(java.awt.event.MouseEvent evt) {
ImageIcon n_cig;
n_cig = new ImageIcon(getClass().getResource("masterproject/No Cigarretes.png"));
jLabel1.setIcon(n_cig);
}
And the exceptions are here
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:205)
at masterproject.design.jButton1ActionPerformed(design.java:384)
at masterproject.design.access$300(design.java:15)
at masterproject.design$3.actionPerformed(design.java:148)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
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:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
The reason that you get a NullPointerException is because for some reason the image file that you're trying to specify cannot be located. So the getResource() method returns a null.
Create a utility function to handle these kind of exception in future
public ImageIcon createImageIcon(String path,
String description) {
java.net.URL imgURL = getClass().getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
Check using this function that wheather or not you are able to access the image or not

How can I show a JTable in a dialog box in NetBeans?

I have had a few problems trying to get this table to show in NetBeans. Here is the code that I am trying to use. I am aware that the loop is wrong and will fix that at a later date.
I get a runtime error with the following code and I can't work it out.
Object[][] data = null;
String[] columnNames = {"Type", "Grade", "Height", "Width", "Length", "Reinforced Bottom", "Reinforced Corners", "Sealable", "Quantity"};
JTable table = new JTable();
public void showTotalOrder(){
for(int i=0; i<boxes.size(); i++){
for(int j=0; j<1; j++){
data[i][j] = boxes.get(i).getName();
data[i][j+1] = boxes.get(i).getGrade();
data[i][j+2] = boxes.get(i).getHeight();
data[i][j+3] = boxes.get(i).getWidth();
data[i][j+4] = boxes.get(i).getLength();
data[i][j+5] = boxes.get(i).getReinforcedBottom();
data[i][j+6] = boxes.get(i).getReinforcedCorners();
data[i][j+7] = boxes.get(i).getSealable();
data[i][j+8] = boxes.get(i).getQuantity();
}
}
JOptionPane.showMessageDialog(this, table);
}
I believe the problem is with the last line of this method.
The error i receive is:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at coursework.FlexBox.showTotalOrder(FlexBox.java:116)
at coursework.FlexBox.currentOrderButtonActionPerformed(FlexBox.java:530)
at coursework.FlexBox.access$300(FlexBox.java:18)
at coursework.FlexBox$4.actionPerformed(FlexBox.java:435)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
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:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
I get a runtime error with the following code and I can't work it out.
Given this stack trace:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at coursework.FlexBox.showTotalOrder(FlexBox.java:116)
...
You get a NullPointerException because of data array is initialized to null:
Object[][] data = null;
Please have a read to:
What is a Null Pointer Exception, and how do I fix it?
Creating, Initializing, and Accessing an Array

How to only select one item from a number of JLists in Swing

So i have a GUI which creates Jlists when categories are created. I only want the user to be able to select one item from any jlist at a time. I also am retrieving information about that item selected, its position in a linked list for example. However with my code so far i get an error, but it doesn't seem to affect functionality, was wondering if anyone could help me work how why or how to fix it?
This is my code:
public void addListener(final JList jList, final int curListCount) {
jList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent evt) {
jLabel2.setText("");
if (isSelected == true) {
isSelected = false;
jList.clearSelection();
jLabel2.setText("");
}
String selectedString = (jList.getSelectedValue().toString());
jLabel2.setText(selectedString);
int selectedIndex = jList.getSelectedIndex();
isSelected = true;
System.out.println("Array position is: " + curListCount);
System.out.println("LinkedList position is: " + selectedIndex);
}
});
}
And the error that comes up is:
'Array position is: 3 LinkedList position is: 0 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at smartChart.wbt$1.valueChanged(wbt.java:57)
at javax.swing.JList.fireSelectionValueChanged(JList.java:1798)
at javax.swing.JList$ListSelectionHandler.valueChanged(JList.java:1812)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:184)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:164)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:211)
at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:405)
at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:415)
at javax.swing.DefaultListSelectionModel.removeSelectionIntervalImpl(DefaultListSelectionModel.java:576)
at javax.swing.DefaultListSelectionModel.clearSelection(DefaultListSelectionModel.java:420)
at javax.swing.JList.clearSelection(JList.java:2045)
at smartChart.wbt$1.valueChanged(wbt.java:54)
at javax.swing.JList.fireSelectionValueChanged(JList.java:1798)
at javax.swing.JList$ListSelectionHandler.valueChanged(JList.java:1812)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:184)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:154)
at javax.swing.DefaultListSelectionModel.setValueIsAdjusting(DefaultListSelectionModel.java:685)
at javax.swing.JList.setValueIsAdjusting(JList.java:2142)
at javax.swing.plaf.basic.BasicListUI$Handler.mouseReleased(BasicListUI.java:2796)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:290)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)'
Any ideas? Thanks in advance.
String selectedString = (jList.getSelectedValue().toString());
Looks like this line throws the exception.
You clear selection in the same listener and it in turn calls selection change.
Try to move the value obtaining to be before jList.clearSelection().
Also I would introduce a flag to show the selection change from user's click or from your code which clears selection. Set the flag isMyCode to true (default false) and skip processing if it's true.

Jlist - ClearSelection with observer pattern

This is my first post, I'm having an issue in my application, I'm programing in Java.
I need to implement the observer pattern, because I have a main window and I need to add elements. When I add a element then the main window should be updated. I put the implements and extends Observable and Observer in the right classes and in the main window, I also override the update method. But when the update executes it throws me an exception.
Here is the code and the exeption:
#Override
public void update(Observable o, Object arg) {
// Update Hotels
this.popularHoteles(unDepartamento.getListaHoteles());
}
private void popularHoteles(ArrayList<Hotel> Hoteles) {
listaHoteles.removeAll();
if (fechasCorrectas()) {
listaHoteles.setListData(Hoteles.toArray());
}
}
where "listaHoteles" is a Jlist item on my from.
when i execute the setListDadta i recive this exeption:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at
diaztaranto.interfaces.MenuPrincipal.listaHotelesValueChanged(MenuPrincipal.java:937)
at
diaztaranto.interfaces.MenuPrincipal.access$800(MenuPrincipal.java:20)
at
diaztaranto.interfaces.MenuPrincipal$9.valueChanged(MenuPrincipal.java:345)
at javax.swing.JList.fireSelectionValueChanged(JList.java:1798) at
javax.swing.JList$ListSelectionHandler.valueChanged(JList.java:1812)
at
javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:184)
at
javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:164)
at
javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:211)
at
javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:405)
at
javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:415)
at
javax.swing.DefaultListSelectionModel.removeSelectionIntervalImpl(DefaultListSelectionModel.java:576)
at
javax.swing.DefaultListSelectionModel.clearSelection(DefaultListSelectionModel.java:420)
at javax.swing.JList.clearSelection(JList.java:2045) at
diaztaranto.interfaces.MenuPrincipal.popularHoteles(MenuPrincipal.java:998)
at
diaztaranto.interfaces.MenuPrincipal.update(MenuPrincipal.java:1043)
at java.util.Observable.notifyObservers(Observable.java:159) at
java.util.Observable.notifyObservers(Observable.java:115) at
diaztaranto.dominio.Hotel.agregarComentario(Hotel.java:212) at
diaztaranto.interfaces.Evaluacion.botonEvaluarActionPerformed(Evaluacion.java:135)
at diaztaranto.interfaces.Evaluacion.access$000(Evaluacion.java:6)
at
diaztaranto.interfaces.Evaluacion$1.actionPerformed(Evaluacion.java:56)
at
javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at
javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
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:6505) at
javax.swing.JComponent.processMouseEvent(JComponent.java:3321) at
java.awt.Component.processEvent(Component.java:6270) at
java.awt.Container.processEvent(Container.java:2229) at
java.awt.Component.dispatchEventImpl(Component.java:4861) at
java.awt.Container.dispatchEventImpl(Container.java:2287) at
java.awt.Component.dispatchEvent(Component.java:4687) at
java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at
java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273) at
java.awt.Window.dispatchEventImpl(Window.java:2719) at
java.awt.Component.dispatchEvent(Component.java:4687) at
java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729) at
java.awt.EventQueue.access$200(EventQueue.java:103) at
java.awt.EventQueue$3.run(EventQueue.java:688) at
java.awt.EventQueue$3.run(EventQueue.java:686) at
java.security.AccessController.doPrivileged(Native Method) at
java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at
java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:702) at
java.awt.EventQueue$4.run(EventQueue.java:700) at
java.security.AccessController.doPrivileged(Native Method) at
java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699) at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
any one knows why this is happen?
Thanks in advice!

EDT and updating status textArea

I want to create new thread (for Server). I have textArea where I put my logs, I created a new class status which handles it. I run new thread for object "server", I try to "deliver" my status object to server and there do status.setStatus("blabal"); But there is a problem....
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Server.<init>(Server.java:16)
at Main.MainPanelButtonStartActionPerformed(Main.java:154)
at Main.access$000(Main.java:9)
at Main$1.actionPerformed(Main.java:63)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.JToggleButton$ToggleButtonModel.setPressed(JToggleButton.java:308)
at
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Exception in thread "Thread-2" java.lang.NullPointerException
at Server.run(Server.java:23)
at java.lang.Thread.run(Thread.java:722)
BUILD SUCCESSFUL (total time: 3 seconds)
My code:
private void MainPanelButtonStartActionPerformed(java.awt.event.ActionEvent evt) {
if(!serverCreated) {
server = new Server(Integer.parseInt(MainPanelTextPort.getText()));
server.setStatusObj(status);
serverThread = new Thread(server);
//SwingUtilities.invokeLater(serverThread); don't know how to use it correctly
serverThread.start();
MainPanelButtonStart.setText("Stop");
serverCreated = true;
} else {
if(!(serverThread.isInterrupted()))
{
try {
server.getServerSocket().close();
} catch(IOException e) {}
serverThread.interrupt();
MainPanelButtonStart.setText("Start");
serverCreated = false;
}
}
}
The variable status is not instantiated on this line in the Server constructor:
status.setStatus("Server Created.");
Assuming this represents RUNNING, STOPPED, etc. these can be set internally in the Server class as it will be able to determine the run state of the server itself.
Regular Threads do not encapsulate correct interaction with the Event Dispatch Thread. SwingWorker would be a better option here. This post has an example.
Aside: Given Java code conventions, MainPanelTextPort would be represented as mainPanelTextPort.

Categories