尝试制作一个漂亮的小程序,但我认为它正在尝试使用GET请求运行代码,而不必等待用户输入。 这不是那么多代码,所以如果你可以帮我,我可以找到一个资源,我可以找到解决这个问题的信息,这很棒。
@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());
}
}
}
答案 0 :(得分:1)
您的代码在该行上显示NullPointerException
:
movie = console.readLine("Enter input:");
这意味着控制台对象未初始化。
尝试阅读这样的输入:
Scanner s = new Scanner(System.in);
System.out.println("Enter input:");
movie = s.nextLine();