How to change icon of label with mouse entered event? - java

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

Related

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException in Resultset

I'm Trying to run this code to get the ResultSet row by row into an Array so i can put them in a jTable. I don't get why i get this error, i'm new in this.
I would be totally thankfull if you could help me.
try {
int pos = 1;
for (int r=0;r<rsCount;r++){
String[] na = null;
res.absolute(pos);
int a=1;
while(res.next()){
na[a]=res.getString(a+1);
a+=1;
}
pos+=1;
for(int c=0;c<8;c++){
tabla.setValueAt(na[c], r, c);
}
}
} catch (SQLException ex) {
Logger.getLogger(Reporte.class.getName()).log(Level.SEVERE, null, ex);
}
}
And here is the OUTPUT:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Main.Reporte.jButton1ActionPerformed(Reporte.java:254)
at Main.Reporte.access$000(Reporte.java:17)
at Main.Reporte$1.actionPerformed(Reporte.java:78)
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)
Your string array is not being properly instantiated. You need to instantiate it like:
String[] na = new String[100];
If you don't know how big the array is going to be you might want to consider using an ArrayList
ArrayList<String> na = new ArrayList<String>();
then each string can be added with:
na.add(res.getString(a+1));

AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException

i am a beginner in java, and i am trying to understand the problem in what my friend wrote, the problem from what i think is that the array size are different. if anyone can help it would be much appreciated.
the problem
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3
at fmenu$fillbuttonListener.actionPerformed(fmenu.java:115)
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:3320)
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)
and here is where i think the problem is
public void actionPerformed(ActionEvent e)
{
Random randomNumbers = new Random();
studentarray = new Object[numofstudent];
quizarray = new Object[numofstudent][numofquiz]; //quizarray = new int[numofstudent][numofquiz]; (original)
for(int i=0; i<numofstudent; i++)
{
quizarray[0][i] = baseid+i;
System.out.println(quizarray[0][i]);
//studentarray[i] = baseid+i;
for(int j=1; j<numofquiz+1; j++)
{
int number = randomNumbers.nextInt(100);
quizarray[j][i] = number; // row column
System.out.println(quizarray[j][i]);
}
}
You define:
quizarray = new Object[numofstudent][numofquiz]
But then access it using
quizarray[0][i]
Where i is < numofstudent. I think you just want:
quizarray[i][0]

NullPointerException and more exceptions

Good time, everyone. I have a bad issue in my code. When I compile it, it doesn't works like I expected and returns me too many exceptions.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at kazemirdo.KazemirdoMatcher.KazemirdoMatcherAct(KazemirdoMatcher.java:53)
at kazemirdo.KazemirdoGUI$1.actionPerformed(KazemirdoGUI.java:47)
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:3311)
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)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at kazemirdo.KazemirdoMatcher.KazemirdoMatcherAct(KazemirdoMatcher.java:53)
at kazemirdo.KazemirdoGUI$1.actionPerformed(KazemirdoGUI.java:47)
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:3311)
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)
First one at kazemirdo.KazemirdoMatcher.KazemirdoMatcherAct(KazemirdoMatcher.java:53) refers:
public void KazemirdoMatcherAct() {
requestWords = kC.getRequestBuffer();
int j = 0;
// Searchs the matches
for (String match : requestWords) { // Refers right here.
p = Pattern.compile(match + "\t.*");
for (int i = 0; i < morphBuffer.length; i++) {
if (morphBuffer[i] == null) {
buffer[j] = morphBuffer[i+1];
}
else {
m = p.matcher(morphBuffer[i]);
if(m.find()) j++;
}
}
}
}
Second exception refers to place, when I'm calling KazemirdoMatcherAct() method.
But others.. I can't understand, really.
What I do wrong?
Whole code, if needed: http://fpaste.org/62123/18201713/
Maybe you can System.out.print the requestWords in KazemirdoConverter,see what is the String request.

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