我有一个简单的套接字服务器示例,用户连接到它并且可能会或可能不会立即发送输入(在这种情况下它不会)。Java 抛出异常,我不明白为什么。
这是代码:
public static void main(String[] args) {
org.apache.log4j.PropertyConfigurator.configure("conf/log4j.properties");
log.debug("Application started");
try {
// Setup socket server
int port = 9999;
sv = new ServerSocket(port);
// Accept incoming sockets or block
Socket s = sv.accept();
log.warn("Accepted new client connection!");
InputStreamReader reader = new InputStreamReader(s.getInputStream(), "UTF-8");
while(!reader.ready()) {}
char[] b = null;
reader.read(b);
log.warn(new String(b));
} catch (Exception e) {
System.out.print(e.getMessage() + "\n");
e.printStackTrace();
System.exit(1);
}
}
和堆栈跟踪:
DEBUG [main] (Main.java:38) - Application started
WARN [main] (Main.java:47) - Accepted new client connection!
null
java.lang.NullPointerException
at java.io.Reader.read(Reader.java:140)
at com.cinefyapp.Main.main(Main.java:51)
感谢您的帮助。