0

I have a method that returns 5 Strings of URLs. It then puts the URLs into string variables for the event handler buttons to open the URL.

I get the error:

Illegal character in query at index 67. 

If I put the actual string URL, the button opens it:

"https://www.youtube.com/watch?v=-BPMacaIwqg&ab_channel=DryBarComedy," 

From my understanding, if the string variable contains the same string, it should open the link but it doesn't. Why?

public void returnVideoUrls() throws IOException {
    UIController returnedFormats = new UIController();

    // returns list of urls
    // stores urls into an arraylist
    ArrayList<String> formattedCode = returnedFormats.combineVideosUrl(videoText.getText());

    //puts each url into a string
    String yes1 = formattedCode.get(0);
    String yes2 = formattedCode.get(1);
    String yes3 = formattedCode.get(2);
    String yes4 = formattedCode.get(3);
    String yes5 = formattedCode.get(4);

    //each event handler opens the link
    videoButton1.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent event){
            try {
                Desktop.getDesktop().browse(new URI(yes1));
            } catch (IOException | URISyntaxException e) {
                e.printStackTrace();
            }
        }
    });

    videoButton2.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent event){
            try {
                Desktop.getDesktop().browse(new URI(yes2));
            } catch (IOException | URISyntaxException e) {
                e.printStackTrace();
            }
        }
    });

    videoButton3.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent event){
            try {
                Desktop.getDesktop().browse(new URI(yes3));
            } catch (IOException | URISyntaxException e) {
                e.printStackTrace();
            }
        }
    });

    videoButton4.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent event){
            try {
                Desktop.getDesktop().browse(new URI(yes4));
            } catch (IOException | URISyntaxException e) {
                e.printStackTrace();
            }
        }
    });

    videoButton5.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent event){
            try {
                Desktop.getDesktop().browse(new URI(yes5));
            } catch (IOException | URISyntaxException e) {
                e.printStackTrace();
            }
        }
    });
}
jewelsea
  • 150,031
  • 14
  • 366
  • 406
Falrym
  • 1
  • 1
    Without a [mcve], this question is unanswerable. Note, the issues are only with URI encoding, so an mcve would only need logic for that as well as complete, valid test data, no JavaFX would be required at all. For general info on valid URL characters, see: [Which characters make a URL invalid?](https://stackoverflow.com/questions/1547899/which-characters-make-a-url-invalid) – jewelsea Dec 08 '21 at 18:14
  • 1
    You don't need awt functions for your logic, you can (and should) just use JavaFX functions: [HostServices.showDocument(String uri)](https://openjfx.io/javadoc/17/javafx.graphics/javafx/application/HostServices.html#showDocument(java.lang.String)). Even though the argument to the preferred function is a string, it should still only use a valid URI format and characters as specified in the link on the prior comment. – jewelsea Dec 08 '21 at 18:33
  • 1
    Yeah thanks host services works. I spent a lot of time on this and everything I found was someone having a space in a URL. – Falrym Dec 08 '21 at 19:36
  • You may be interested in this [url encoding tutorial](https://www.baeldung.com/java-url-encoding-decoding) and the java [UrlEncoder](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/net/URLEncoder.html), in case you need to deal with urls which contain characters which cannot be interpreted unless they are encoded (though you wouldn't want to pass a fully encoded url to host services, e.g. only encode stuff in query string portions, otherwise it wouldn't understand the protocol). – jewelsea Dec 08 '21 at 20:24

0 Answers0