Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

114
Vistas
Java URL method not filling properly with recursion
import java.util.Scanner;
import java.net.MalformedURLException;
import java.net.URL;
import java.io.IOException;

public class Main {

    // takes user input and validates
    public static URL URLPrompt() throws MalformedURLException {
        URL pageLocation = null;
        Scanner in = new Scanner(System.in);
        System.out.println("Please enter a webpage URL to find all it's links: ");
        try {
            pageLocation = new URL(in.nextLine());
        } catch (MalformedURLException exception) {
            System.out.println("Invalid URL");
            URLPrompt();
        }
        return pageLocation;
    }

    public static void main(String[] args) throws MalformedURLException, IOException {
        Scanner in = new Scanner(System.in);
        URL pageLocation = URLPrompt();
        Scanner scan = new Scanner(pageLocation.openStream());
        while (scan.hasNext()) {
            String webFile = scan.nextLine();
            // Searches for all hyperlinks in <a href=> </a> form
            if (webFile.contains("<a href=") && webFile.contains("</a>")) {
                int x = webFile.indexOf("<a href=");
                int y = webFile.indexOf("</a>");
                String link = webFile.substring(x, y + 4);
                System.out.printf("%s\n", link);

            }
        }
        scan.close();
        in.close();

    }
}

So this program is supposed to take a user inputted url link and print out all of the hyperlinks in html form, "a href" to the closing "a" tag. This code does just that if the url is valid initially. However, if an invalid url is input the system catches it and the Urlprompt method calls itself, when the method calls itself after an invalid input and then a valid URL is input during recursion I get a nullpointer exception on line 24... How can I get the URLPrompt method to accurately fill the variable pageLocation during recursion?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The recursive call should be return URLPrompt().

BTW: I wouldn't use recursion for this purpose (it's just a waste of stack frames), just a simple while loop.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda