Exception while trying to restore a JFrame?

in the following code a simple system tray

there is no errors but i,ve a nullpointerexception

when trying to use the methodframe.setVisible(true);

to restore a JFrame

this is the code:

if (SystemTray.isSupported()){

final SystemTray tray = SystemTray.getSystemTray();

Image image = Toolkit.getDefaultToolkit().getImage("D:\\Other\\JAVA\\Icons\\shutdown.png");

// trayIcon = new TrayIcon(image, "JShutdown Timer",popup);

//action listener for the exit menu option

//that is part of the tray icon's pop-up menu

//it ends the application

ActionListener exitListener =new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

System.exit(0);

}

};

//action listener for the restore menu option

//that is part of the tray icon's pop-up menu

//it unhides the window and removes the tray icon

ActionListener unHideListener =new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

frame.setVisible(true);

tray.remove(trayIcon);

}

};

//Action Listener that recieves events from tray icon

//basically if someone double clicks the tray icon

//then the window is restored

ActionListener actionListener =new ActionListener(){

publicvoid actionPerformed(ActionEvent e){

frame.setVisible(true);

tray.remove(trayIcon);

}

};

//create the pop-up menu

PopupMenu popup =new PopupMenu();

//create the exit option

MenuItem defaultItem =new MenuItem("Exit");

defaultItem.addActionListener(exitListener);

//create the restore option

MenuItem unHideItem =new MenuItem("Restore");

unHideItem.addActionListener(unHideListener);

//add the options to the menu

popup.add(unHideItem);

popup.add(defaultItem);

trayIcon =new TrayIcon(image,"JShutdown Timer",popup);

class TrayActionListenerimplements ActionListener{

publicvoid actionPerformed(ActionEvent ee){

trayIcon.getPopupMenu();

}

};

trayIcon.setImageAutoSize(true);

trayIcon.addActionListener(actionListener);

trayIcon.addActionListener(new TrayActionListener());

try{

tray.add(trayIcon);

}catch (AWTException e){

}

}

else{

}

}

and this is the exception:

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

at Countdown2$3.actionPerformed(CountDown2.java:185)

at java.awt.MenuItem.processActionEvent(MenuItem.java:627)

at java.awt.MenuItem.processEvent(MenuItem.java:586)

at java.awt.MenuComponent.dispatchEventImpl(MenuComponent.java:300)

at java.awt.MenuComponent.dispatchEvent(MenuComponent.java:288)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:602)

at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

[5456 byte] By [First_knighta] at [2007-11-27 8:13:18]
# 1
are you exiting the jframe or setting its state to iconified? If the latter, then I think all you have to do is set state to normal to revisualize the jframe. If the former, then your frame is gone and you'll have to reconstruct it (AFAIK).
petes1234a at 2007-7-12 19:57:52 > top of Java-index,Java Essentials,New To Java...
# 2
even when using any other show messageframe.toFront();frame.Show();frame.setState(JFrame.NORMAL);still the same exception
First_knighta at 2007-7-12 19:57:52 > top of Java-index,Java Essentials,New To Java...
# 3
I think that the method that you want to look at is the Frame method, setExtendedState(int) which you can find here. http://java.sun.com/javase/6/docs/api/java/awt/Frame.html#setExtendedState(int)Good luck and let me know if this helps.
petes1234a at 2007-7-12 19:57:52 > top of Java-index,Java Essentials,New To Java...
# 4
hello petes1234the jframe is minimized to the system tray and i want to restore it again
First_knighta at 2007-7-12 19:57:52 > top of Java-index,Java Essentials,New To Java...
# 5

> even when using any other show message

>

> frame.toFront();

> frame.Show();

> frame.setState(JFrame.NORMAL);

>

> still the same exception

My question remains: how is the jframe iconified in the first place? By clicking on the "-" button at the upper right menu?

Also, I believe that you shouldn't use frame.setState() as this is an obsolete function. Instead use the frame.setExtendedState() fuction.

petes1234a at 2007-7-12 19:57:52 > top of Java-index,Java Essentials,New To Java...
# 6
even this methodframe.setExtendedState(int);it gives the same exception
First_knighta at 2007-7-12 19:57:52 > top of Java-index,Java Essentials,New To Java...
# 7
My question remains: how is the jframe iconified in the first place? By clicking on the "-" button at the upper right menu?(gads, I'm starting to sound like a broken record)Message was edited by: petes1234
petes1234a at 2007-7-12 19:57:52 > top of Java-index,Java Essentials,New To Java...
# 8
the Jframe is iconified to the tray when pressing the close(X) standard button on the JFrame
First_knighta at 2007-7-12 19:57:52 > top of Java-index,Java Essentials,New To Java...
# 9

> the Jframe is iconified to the tray when pressing the

> close(X) standard button on the JFrame

EXACTLY. That's your problem! You are not minimizing the frame, you're exiting it. It doesn't exist. It's null. It's gone to the great beyond. It ceases to be. It has shifted off its mortal coil (what else do dead parrots do?). You cannot resurrect this dead bird.

petes1234a at 2007-7-12 19:57:52 > top of Java-index,Java Essentials,New To Java...
# 10

More: It's not pinin', it's passed on! This parrot is no more! It has ceased to be! It's expired and gone to meet its maker! This is a late parrot. It's a stiff! Bereft of life, it rests in peace! If you hadn't nailed it to the perch it would be pushing up the daisies! It's rung down the curtain and joined the choir invisible! This is an ex-parrot!

petes1234a at 2007-7-12 19:57:52 > top of Java-index,Java Essentials,New To Java...
# 11
i didn't understandwould you please explain ?
First_knighta at 2007-7-12 19:57:52 > top of Java-index,Java Essentials,New To Java...
# 12

You have closed the frame not minimized it. The frame is now gone. Set up for garbage collecting. Dead, etc... (look at the dead parrot sketch above). (anybody, please correct me if i'm wrong)

I really don't know how to explain this any more clearly. Exactly what about a closed dead form do you not understand? Don't be a silly ole bear of very little brain. Surely you've closed apps such as Word, and understand that if you want to resurrect them, you have to restart the entire program from scratch. If you understand that, surely you understand this.

Message was edited by:

petes1234

petes1234a at 2007-7-12 19:57:52 > top of Java-index,Java Essentials,New To Java...
# 13

dear petes1234

thanks for your words!

but i want to make my application like MSN Messenger this is not a strange behave as you said!

when you hit the standard close button(X) the program is minimized to tray and when you double click on it in the system tray the program is restored or maximized? i made the minimizing step but i've an exception with the restoring step which i'm trying to fix

thank you at all

any one could help me?

First_knighta at 2007-7-12 19:57:52 > top of Java-index,Java Essentials,New To Java...
# 14
So what exactly does your program do when it encounters a windows exit command? How do you go around it?Oh, BTW, cool article on windows menus w/ minimize and tray icons can be found here: http://www.devx.com/Java/Article/21604/1954?pf=true
petes1234a at 2007-7-12 19:57:52 > top of Java-index,Java Essentials,New To Java...
# 15
thanks petes1234 i visited the link but didn't find what i wase searching for...but iam sure there is a valid way to restore the program from the system traywith double click by mouseany one have experience with this situation?
First_knighta at 2007-7-21 22:33:13 > top of Java-index,Java Essentials,New To Java...
# 16
override the close button or disable it.
deAppela at 2007-7-21 22:33:13 > top of Java-index,Java Essentials,New To Java...
# 17
to re-ask my question from previous:So what exactly does your program do when it encounters a windows exit command? How do you go around it?
petes1234a at 2007-7-21 22:33:13 > top of Java-index,Java Essentials,New To Java...
# 18

so sorry for late,network problem

hello petes here is a sample of my code run it and you will understand what i,m trying to do

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.text.NumberFormatter;

import javax.swing.JFrame;

import javax.swing.JButton;

import java.awt.Image;

import javax.swing.ImageIcon;

import java.awt.Toolkit;

import java.awt.Container;

import java.awt.event.ActionListener.*;

import java.awt.event.ActionEvent.*;

import java.lang.Runtime;

import java.io.*;

import java.io.IOException;

import java.rmi.*;

import java.rmi.server.*;

import java.math.*;

import java.util.*;

import java.text.*;

import javax.swing.text.*;

import javax.swing.JMenuItem;

import javax.swing.JPopupMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenu;

import javax.swing.AbstractButton;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.awt.event.KeyEvent;

import java.awt.AWTException;

import java.awt.Desktop;

import java.awt.Image;

import java.awt.SystemTray;

import java.awt.Toolkit;

import java.awt.TrayIcon;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JPanel;

import javax.swing.JTextField;

import java.util.EventObject;

class systemtray extends JFrame

{

private Desktop desktop;

private TrayIcon trayIcon=null;

private SystemTray tray;

private JFrame frame;

private PopupMenu popup;

private MenuItem unHideItem;

private MenuItem defaultItem;

public systemtray()

{

if (Desktop.isDesktopSupported()) {

desktop = Desktop.getDesktop();

}

initComponents();

final JFrame frame=new JFrame("System Tray");

frame.setSize(350,200);

frame.setResizable(false);

frame.setVisible(true);

}

private void initComponents(){

setupSystemTray();

}

private void setupSystemTray() {

if (SystemTray.isSupported()) {

final SystemTray tray = SystemTray.getSystemTray();

Image image = Toolkit.getDefaultToolkit().getImage("D:\\Other\\JAVA\\Icons\\shutdown.png");

ActionListener exitListener = new ActionListener() {

public void actionPerformed(ActionEvent e) {

System.exit(0);

}

};

ActionListener unHideListener = new ActionListener() {

public void actionPerformed(ActionEvent e) {

//which method should i add here to restore the application from the system tray

tray.remove(trayIcon);

}

};

ActionListener actionListener = new ActionListener() {

public void actionPerformed(ActionEvent e) {

//which method should i add here to restore the application from the system tray

tray.remove(trayIcon);

}

};

PopupMenu popup = new PopupMenu();

MenuItem defaultItem = new MenuItem("Exit");

defaultItem.addActionListener(exitListener);

MenuItem unHideItem = new MenuItem("Restore");

unHideItem.addActionListener(unHideListener);

popup.add(unHideItem);

popup.add(defaultItem);

trayIcon = new TrayIcon(image, "JShutdown Timer",popup);

class TrayActionListener implements ActionListener {

public void actionPerformed(ActionEvent ee) {

trayIcon.getPopupMenu();

}

};

trayIcon.setImageAutoSize(true);

trayIcon.addActionListener(actionListener);

trayIcon.addActionListener(new TrayActionListener());

try {

tray.add(trayIcon);

} catch (AWTException e) {

}

}

else{

}

}

public static void main(String args[]){new systemtray();}

}

First_knighta at 2007-7-21 22:33:13 > top of Java-index,Java Essentials,New To Java...
# 19

I see kind of what you're doing. I have no idea of what I'm doing, but what if you try this?

constructor

public systemtray() {

super("My System Tray");

if (Desktop.isDesktopSupported())

{

desktop = Desktop.getDesktop();

}

initComponents();

setSize(350, 200);

setResizable(false);

setVisible(true);

//final JFrame frame = new JFrame("System Tray");

//frame.setSize(350, 200);

//frame.setResizable(false);

//frame.setVisible(true);

}

unHideListener

ActionListener unHideListener = new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

whatTheF();

tray.remove(trayIcon);

}

};

whatTheF()

protected void whatTheF()

{

setVisible(true);

}

Again, I have no f****** idea of what I'm doing here; I'm just throwing code into Eclipse. But for what it's worth, it does restore a JFrame. Kinda.

petes1234a at 2007-7-21 22:33:13 > top of Java-index,Java Essentials,New To Java...
# 20

I suspect that you may have had trouble working with class fields while in the nested actionlistener class. That's why I created an extremely simple method "whatTheF" that's located outside the actionlistener but called by it. It has full access to all of the systemtray class's fields and methods.

petes1234a at 2007-7-21 22:33:14 > top of Java-index,Java Essentials,New To Java...
# 21
so nice trick petes1234it worked with me proberlybut it forced me to replace my code instead of using JFrame i'm using Container ,got it?but it worksthank you so muchMessage was edited by: First_knight
First_knighta at 2007-7-21 22:33:14 > top of Java-index,Java Essentials,New To Java...
# 22

but i have a little bug here

when i minimized the program to tray(by Pressing the Standard close button) and restored it for the first time,it was restored proberly .and when i tried to minimize it again(by Pressing the Standard close button) it disappeares? but still working in the back ground?

First_knighta at 2007-7-21 22:33:14 > top of Java-index,Java Essentials,New To Java...
# 23

what if you change whatTheF() to:

protected void whatTheF()

{

setVisible(true);

setupSystemTray();

}

or something similar?

(please change the name of this method).

petes1234a at 2007-7-21 22:33:14 > top of Java-index,Java Essentials,New To Java...
# 24
thanks again it workedmy last question is how to display a trayicon message when just the program is minimized to tray?
First_knighta at 2007-7-21 22:33:14 > top of Java-index,Java Essentials,New To Java...
# 25
i tried the window events is that right?but nothing happenshow to do that?
First_knighta at 2007-7-21 22:33:14 > top of Java-index,Java Essentials,New To Java...
# 26

WindowAdapter actionListener2 = new WindowAdapter()

{

public void windowClosing(WindowEvent e2) {

try{

Thread.sleep(1);

trayIcon.displayMessage("Take Care !",

"JShutdown Timer Is In Progress Here.",

TrayIcon.MessageType.WARNING);

}

catch(InterruptedException un){}

}

};

First_knighta at 2007-7-21 22:33:14 > top of Java-index,Java Essentials,New To Java...
# 27
any one could help?
First_knighta at 2007-7-21 22:33:14 > top of Java-index,Java Essentials,New To Java...