5 Replies - 769 Views - Last Post: 11 November 2011 - 05:21 PM Rate Topic: -----

#1 ryanjjjj  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 33
  • Joined: 10-November 11

Exception when opening URL in an applet.

Posted 11 November 2011 - 04:23 PM

My original thread displayed this same problem but ended up fixing everything else i had wrong with my code. (which was a lot)
so rather then continuing the same bloated thread, i started a new one.

I have (with a lot of help) created an j applet that will add a list of names to an arraylist and then randomly choose 1 and display it as a j label.
this all works but my issue is the next step.

i want to allow the user to then read about the samurai name that was just generated.

to do this i added a new jbutton then will give the user the option to go to the website where the samurai is listed or generate a new name.

the exact problem is this error that shows up in the console when i attempt to open the url.


Saigo TsugumichiException in thread "AWT-EventQueue-1" java.lang.IllegalArgumentException
	at java.net.URI.create(URI.java:842)
	at japanese_samurai.actionPerformed(japanese_samurai.java:83)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
	at java.awt.Component.processMouseEvent(Component.java:6038)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
	at java.awt.Component.processEvent(Component.java:5803)
	at java.awt.Container.processEvent(Container.java:2058)
	at java.awt.Component.dispatchEventImpl(Component.java:4410)
	at java.awt.Container.dispatchEventImpl(Container.java:2116)
	at java.awt.Component.dispatchEvent(Component.java:4240)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
	at java.awt.Container.dispatchEventImpl(Container.java:2102)
	at java.awt.Component.dispatchEvent(Component.java:4240)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
	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)
Caused by: java.net.URISyntaxException: Illegal character in query at index 54: http://wiki.samurai-archives.com/index.php?title=Saigo Tsugumichi
	at java.net.URI$Parser.fail(URI.java:2809)
	at java.net.URI$Parser.checkChars(URI.java:2982)
	at java.net.URI$Parser.parseHierarchical(URI.java:3072)
	at java.net.URI$Parser.parse(URI.java:3014)
	at java.net.URI.<init>(URI.java:578)
	at java.net.URI.create(URI.java:840)
	... 25 more




Saigo Tsugumichi is obviously the name that was selected at the time.
i don't know how exactly to fix this issue as most online forums iv looked at don't exactly fit this problem.

i'm wondering if i should make another class just for the url?

the entire source code is here:

main class:
//name: Ryan Jolin
//Purpose: to randomly generate the name of a famous samurai
//date created: Thur, Nov 10, 2011 -12:24am-
//date revised: Fri, Nov 11, 2011 -5:21pm-

   import javax.swing.*;
   import java.awt.*;
   import java.awt.event.*;
   import java.util.*;

   public class japanese_samurai extends JApplet implements ActionListener
   {
	
      Container pane = getContentPane();
      Container con = getContentPane();	  
    
      JLabel headline = new JLabel("Click the randomizer to recieve a famous japanese samurai name");
      JLabel response = new JLabel("");
      Font hf1 = new Font("Helvetica", Font.BOLD,35);
      Font hf2 = new Font("Times New Roman", Font.BOLD,22);
      Font hf3 = new Font("Arial", Font.BOLD,22);
      Font hf4 = new Font("Candara", Font.BOLD,22);
      Font hf5 = new Font("Gothic", Font.BOLD,22);
      JButton gen = new JButton("Generate");
		JButton info = new JButton("Click here to read about this Samurai");
		String name;
      
		public japanese_samurai()
		{
           japanese_samurai_scan samuraiScan = new japanese_samurai_scan();
           name = samuraiScan.getSamuraiName();
	   }
		    
      public void init()
      {
      
         pane.setLayout (new FlowLayout());
         con.setLayout (new FlowLayout());
      			
         pane.setBackground(Color.BLACK);
      			
         headline.setFont(hf1);
         headline.setForeground(Color.WHITE);
         pane.add(headline);
      
         //my generator button
         con.add(gen);
			gen.setActionCommand("gen");
         gen.addActionListener(this);
      
      
      }
   		    		 
      public void actionPerformed(ActionEvent e)
      {      
				
				//new generated name
				if ("gen".equals(e.getActionCommand ()))
	         {
                  japanese_samurai_scan samuraiScan = new japanese_samurai_scan();
						name = samuraiScan.getSamuraiName();
						
			         response.setText(name);
			         response.setFont(hf2);
			         response.setForeground(Color.YELLOW);
			         pane.add(response);
			         gen.setEnabled(true);
			        
						con.add(info);
						info.setActionCommand("info");
			         info.addActionListener(this);
						
						validate();
				}
				
				//my code for redirection.
				if ("info".equals(e.getActionCommand ()))
	         {
				System.out.print(name);
				     try 
			        {
                     String url = "http://wiki.samurai-archives.com/index.php?title=" + name;
                     java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
         
			        }
      
		           catch (java.io.IOException d) 
			        {
			  
                     System.out.println(d.getMessage());
					 
                 }
			   }
       }
   }



scanner class:
import java.util.*;
import java.io.File;

     public class japanese_samurai_scan
     {
	  ArrayList<String> names = new ArrayList<String>();
     Random random = new Random();
	  Scanner scanner;
	  
			  public japanese_samurai_scan()
		     {
		           try 
					  {
							  scanner = new Scanner(new File("japanese_samurai.txt"));          
					  }
										 
					  catch (java.io.FileNotFoundException e) 
					  {
							  System.out.println(e.getMessage());
					  }
					  
					  while (scanner.hasNext())
					  {
							  names.add(scanner.nextLine().trim());
					  }
		     }
		
		     public String getSamuraiName()
		     {
		            String name = names.get(random.nextInt(names.size()));
				      return name;
			  }
     }



any suggestions would be greatly appreciated. :helpsmilie:

Is This A Good Question/Topic? 0
  • +

Replies To: Exception when opening URL in an applet.

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1401
  • View blog
  • Posts: 3,109
  • Joined: 05-April 11

Re: Exception when opening URL in an applet.

Posted 11 November 2011 - 04:47 PM

Your URI doesn't follow URI syntax.
I dont believe you can have space in your URI, so writing the name in, Saigo Tsugumichi, will cause an error.
Was This Post Helpful? 1
  • +
  • -

#3 ryanjjjj  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 33
  • Joined: 10-November 11

Re: Exception when opening URL in an applet.

Posted 11 November 2011 - 05:09 PM

View PostCasiOo, on 11 November 2011 - 04:47 PM, said:

Your URI doesn't follow URI syntax.
I dont believe you can have space in your URI, so writing the name in, Saigo Tsugumichi, will cause an error.


what do you mean by space? the website in the browser bar would be exactly like this:
http://wiki.samurai-archives.com/index.php?title=Saigo Tsugumichi



do you mean the space in "Saigo Tsugumichi"?

ill try something quick and see what happens.

EDIT:

yup you were spot on, i changed the " " to an "_" and fixed my one error.
now im back to my banner warning.

Saigo_TsugumichiException in thread "AWT-EventQueue-1" java.security.AccessControlException: access denied (java.awt.AWTPermission showWindowWithoutWarningBanner)
	at java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)
	at java.security.AccessController.checkPermission(AccessController.java:546)
	at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
	at java.awt.Desktop.checkAWTPermission(Desktop.java:221)
	at java.awt.Desktop.browse(Desktop.java:358)
	at japanese_samurai.actionPerformed(japanese_samurai.java:83)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
	at java.awt.Component.processMouseEvent(Component.java:6038)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
	at java.awt.Component.processEvent(Component.java:5803)
	at java.awt.Container.processEvent(Container.java:2058)
	at java.awt.Component.dispatchEventImpl(Component.java:4410)
	at java.awt.Container.dispatchEventImpl(Container.java:2116)
	at java.awt.Component.dispatchEvent(Component.java:4240)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
	at java.awt.Container.dispatchEventImpl(Container.java:2102)
	at java.awt.Component.dispatchEvent(Component.java:4240)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
	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)



how do i display the window in a applet warning banner?

This post has been edited by ryanjjjj: 11 November 2011 - 05:10 PM

Was This Post Helpful? 0
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,534
  • Joined: 05-May 05

Re: Exception when opening URL in an applet.

Posted 11 November 2011 - 05:13 PM

The proper URL is: http://wiki.samurai-...aigo_Tsugumichi

So the first and last name in your file should be separated by an underscore.
Was This Post Helpful? 0
  • +
  • -

#5 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1401
  • View blog
  • Posts: 3,109
  • Joined: 05-April 11

Re: Exception when opening URL in an applet.

Posted 11 November 2011 - 05:14 PM

Try and click in your link field in your browser after it has loaded the page.

It replaces the spaces with underscore "_". At least my browser does.

This post has been edited by CasiOo: 11 November 2011 - 05:14 PM

Was This Post Helpful? 0
  • +
  • -

#6 ryanjjjj  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 33
  • Joined: 10-November 11

Re: Exception when opening URL in an applet.

Posted 11 November 2011 - 05:21 PM

ya i played with it a little and figured that much out.
that was just a small issue. i really need to figure out how i include the applet warning banner so the security dosn't freak out.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1