I have a SwingWorker that is running in the background. After it works on
some stuff. It will wait. When the user push a button, the MyListener will
be invoked and wake up the worker to continue do some stuff. It is not working
like I thought it would. It throws an exception at the bottom. Please tell
me what I did wrong.
MyWorker worker = new MyWorker();
class MyListener implements ActionListener {
#Override
public void actionPerformed (ActionEvent e) {
//DO some stuff and wake up worker
worker.notify ();
}
}
class MyWorker extends SwingWorker {
boolean _work = true;
#Override
protected Object doInBackground() throws Exception {
System.out.println ("in myWorker");
while (_work == true) {
//DO some stuff here
wait();
}
return null;
}
}
The exception:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalMonitorStateException
at java.lang.Object.notify(Native Method)
at worker.Worker$2.actionPerformed(Worker.java:54)
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:2713)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
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:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
You need to add a synchronized block around the notify call.
synchronized (worker) {
worker.notify();
}
Now, having said that, you are generally discouraged from using instance variables as monitor locks and instead use a static final lock object as (amongst other things) it decreases the potential of having the wrong instance
Related
I am trying to run the following code but it gives me an error
public void addToContainer(JInternalFrame internalframe){
JInternalFrame []allFrames = Jdpmain.getAllFrames();//getting an array of all the frames in my jdesktoppane(jdpmain)
boolean flag=false;
for(int i=0;i<allFrames.length;i++){
JInternalFrame currentFrame = allFrames[i];
if(internalframe.getClass().isInstance(allFrames[i])){
Jdpmain.setSelectedFrame(currentFrame);//if the internal frame already exist it might be under another jinternalframe so, I called this method to bring it to focus
flag=true;
}
if(flag==false){
Jdpmain.add(internalframe); //if an instance of the frame I am adding does not exist in jdesktoppane it should add an instance of it to jdesktoppane
}
}
}
Exception in thread "AWT-EventQueue-0"
java.lang.IllegalArgumentException: illegal component position at
java.awt.Container.addImpl(Container.java:1093) at
javax.swing.JLayeredPane.addImpl(JLayeredPane.java:230) at
javax.swing.JDesktopPane.addImpl(JDesktopPane.java:486) at
java.awt.Container.add(Container.java:410) at
MyLibrarypackage.MainFrame.addToContainer(MainFrame.java:169) at
MyLibrarypackage.MainFrame.jButton2ActionPerformed(MainFrame.java:148)
at MyLibrarypackage.MainFrame.access$100(MainFrame.java:13) at
MyLibrarypackage.MainFrame$2.actionPerformed(MainFrame.java:62) 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:2713) at
java.awt.Component.dispatchEvent(Component.java:4687) at
java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707) at
java.awt.EventQueue.access$000(EventQueue.java:101) at
java.awt.EventQueue$3.run(EventQueue.java:666) at
java.awt.EventQueue$3.run(EventQueue.java:664) 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:680) at
java.awt.EventQueue$4.run(EventQueue.java:678) at
java.security.AccessController.doPrivileged(Native Method) at
java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677) at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
I will be glad if anyone could help me here.
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
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!
Okay, so I have all of my classes inside of a few packages:
Game/src/package1/Class.class
and I want to get into a file in:
Game/res/Terrain.png
How would I do this?
public Screen(Game game){
this.game=game;
try {
URL url = new URL("/res/Terrain.png");
textures = ImageIO.read(url);
} catch (IOException e) {
e.printStackTrace();
}
}
then i get this error:
java.net.MalformedURLException: no protocol: /res/Terrain.png
at java.net.URL.<init>(URL.java:583)
at java.net.URL.<init>(URL.java:480)
at java.net.URL.<init>(URL.java:429)
at kore.survival.Screen.<init>(Screen.java:25)
at kore.survival.Game.<init>(Game.java:27)
at kore.survival.Game.main1(Game.java:99)
at kore.survival.LoginFrame.jButton1ActionPerformed(LoginFrame.java:201)
at kore.survival.LoginFrame.access$200(LoginFrame.java:16)
at kore.survival.LoginFrame$3.actionPerformed(LoginFrame.java:82)
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:703)
at java.awt.EventQueue.access$000(EventQueue.java:102)
at java.awt.EventQueue$3.run(EventQueue.java:662)
at java.awt.EventQueue$3.run(EventQueue.java:660)
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:676)
at java.awt.EventQueue$4.run(EventQueue.java:674)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:673)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
Assuming that the resource is embedded with the Jar...
textures = ImageIO.read(getClass().getResource("/res/Terrain.png"));
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.