Results 1 to 2 of 2
  1. #1
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default Little GUI question.

    Ok, so I have a JButton, when you you open this button, you choose a file(JFileChooser). But if you choose the "x" button(close), I get exception.

    Java Code:
    JButton lfile = new JButton ("Load File");
    lfile.setPreferredSize (new Dimension (140, 40));
    
    lfile.addActionListener (new ActionListener ()
    {
    	@Override public void actionPerformed (ActionEvent e)
    	{
    		JFileChooser fchooser = new JFileChooser (new File ("."));
    		int option = fchooser.showOpenDialog(null);
    		
    		//If no file was choosen(user 	pressed cancel or exit button
    		if (option == JFileChooser.APPROVE_OPTION)
    			return;
    				
    		File inputFile = fchooser.getSelectedFile ();
    				
    		try 
    		{
    			MainStuff.cry.setFilePath (inputFile);
    		} 
    		catch (FileNotFoundException e1) 
    		{
    			e1.printStackTrace();
    		}
    				
    		MainStuff.inputPath = inputFile.getAbsolutePath ();
    	}
    });
    So, if user press exit or cancel while in the JFileChooser box, I get this error:


    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at pjjava.crypt.AbstractFileCrypting.setFilePath(AbstractFileCrypting.java:19)
    	at pjgui.app.BaseFrame$1.actionPerformed(BaseFrame.java:92)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$000(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.awt.EventQueue$3.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$4.run(Unknown Source)
    	at java.awt.EventQueue$4.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    So, how do I cancel the method in a better way?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Little GUI question.

    Why are you returning out of the method if the user chooses the approve option? Shouldn't that be != not == there? Also, you'll want to enclose all blocks, including single line if blocks, in curly braces.

Similar Threads

  1. GUI question.
    By ryainad in forum Advanced Java
    Replies: 3
    Last Post: 04-02-2011, 03:47 AM
  2. Question concerning question marks and colons
    By jim01 in forum New To Java
    Replies: 17
    Last Post: 01-14-2011, 01:05 AM
  3. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 09:49 AM
  4. Question!
    By anjogomez in forum Java Applets
    Replies: 2
    Last Post: 02-21-2009, 04:24 AM
  5. Hello everyone and my first question
    By htetnaing in forum New To Java
    Replies: 3
    Last Post: 01-26-2009, 04:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •