Results 1 to 3 of 3
  1. #1
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    108
    Rep Power
    0

    Default Desktop API - Opening system browser

    What I am trying to accomplish is take a string given by a user in a JTextField, Pass that string to a JInternalFrame, and when I click on the JInternalFrame, have the systems default browser open using the supplied String passed into the JTextField. Code I have so far is below:
    Java Code:
    				
    				@Override
    				public void mouseClicked(MouseEvent arg0) {
    					try {
    						String x = billURL2.getText();
    						url = new URL("http://" + x);
    						System.out.println(url);
    					} catch (MalformedURLException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    					try {
    						uri = new URI(url.toString());
    					} catch (URISyntaxException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    					try {
    						desktop.browse(uri);
    					} catch (IOException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace();
    					}
    					System.out.println("MOUSE CLICKED");
    				}
    			});
    			iFrame.show();
    			
    			main.add(iFrame);
    		}
    	}
    }
    I am able to input the web address. I am able to get the JInternalFrame to popup. But as soon as I click on the JInternalFrame I get a NullPointerException error. I assume it is coming from the line (19): desktop.browse(uri); , but I have looked over the API for Desktop and that didnt help clear anything up. If someone could point me in a direction of what I am doing to trip the NPE I would greatly appreciate it.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,807
    Rep Power
    5

    Default Re: Desktop API - Opening system browser

    You need to show more of your code for me to help you. Did you assign an instance of Desktop to desktop? Are you checking that desktop is not null or that the URI is properly being obtained by the mouse? System.out.println() is your friend.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    108
    Rep Power
    0

    Default Re: Desktop API - Opening system browser

    I realized my problem thanks to you. I was missing Desktop.getDesktop(); when I first declared my Desktop
    Java Code:
    private Desktop desktop = Desktop.getDesktop();

    Thanks

Similar Threads

  1. opening an html file in default browser problems
    By dhazwa in forum New To Java
    Replies: 2
    Last Post: 08-22-2011, 06:42 AM
  2. opening default browser window in invisible mode
    By robby14 in forum Advanced Java
    Replies: 1
    Last Post: 02-20-2010, 02:50 AM
  3. Replies: 4
    Last Post: 10-07-2009, 10:14 AM
  4. Opening Text Files with Default System Editor
    By Pesch in forum Advanced Java
    Replies: 5
    Last Post: 10-08-2008, 07:17 PM
  5. Replies: 2
    Last Post: 09-12-2008, 07:10 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
  •