0

Trying to make a nifty little program, but I think it's trying to run the code with the GET request, without waiting for the user input. It's not that much code, so if you could help me by directing me to a resource where I could find info to fix that issue it'd be awesome.

@SuppressWarnings("unused")
public class Test {

    public static void main(String args[]) throws IOException {
        BufferedReader rd;
        OutputStreamWriter wr;
        String movie = null;
        Console console = System.console();
        movie = console.readLine("Enter input:");
        try {
            URL url = new URL("http://www.imdbapi.com/?i=&t=" + movie);
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            wr = new OutputStreamWriter(conn.getOutputStream());
            wr.flush();

            // Get the response
            rd = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            String line;
            line = rd.readLine();
            if (line != null) {
                System.out.println(line);
            } else {
                System.out.println("Sorry! That's not a valid URL.");
            }
        } catch (UnknownHostException codeyellow) {
             System.err.println("Caught UnknownHostException: " + codeyellow.getMessage());
        }
    }

}
4

1 回答 1

1

您的代码NullPointerException在那一行给出了一个:

movie = console.readLine("Enter input:");

这意味着控制台对象没有被初始化。

尝试像这样读取输入:

Scanner s = new Scanner(System.in);
System.out.println("Enter input:");
movie = s.nextLine();
于 2013-09-16T12:51:45.103 回答