试图制作一个漂亮的小程序,但我认为它试图通过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());
}
}
}
您的代码NullPointerException
在该行上给出了:
movie = console.readLine("Enter input:");
这意味着不会初始化控制台对象。
尝试像这样读取输入:
Scanner s = new Scanner(System.in);
System.out.println("Enter input:");
movie = s.nextLine();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句