我正在寫一個簡單的web服務器在java中作爲練習。當我運行程序時,它立即開始進入無限循環,輸出「Error:null」到控制檯中。我有幾個的try/catch語句做Java:無限錯誤「null」
System.out.println("Error: " + e.getMessage());
我使用Eclipse調試器來找出試了一下從哪裏開始,但是當我在調試器中運行它,它的正常動作。繼承人的代碼相關的兩個片段:
public void run() {
// Declare server socket
ServerSocket serversocket = null;
try {
// Initialize serversocket on the port passed to
// the constructor
serversocket = new ServerSocket(port);
System.out.println("Server started on port "
+ Integer.toString(port));
} catch (Exception e) {
System.out.println("Fatal Error: " + e.getMessage());
}
while (true) {
try {
// Wait for connections
Socket connectionsocket = serversocket.accept();
// Get client IP
InetAddress client = connectionsocket.getInetAddress();
System.out.println(client.getHostName() + " connected.");
// Read the request into a buffer
BufferedReader input = new BufferedReader(
new InputStreamReader(connectionsocket.getInputStream()));
// Prepare the output stream for sending headers
// and requested file to the client
DataOutputStream output =
new DataOutputStream(connectionsocket.getOutputStream());
http_handler(input, output);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
和
private void http_handler(BufferedReader input, DataOutputStream output) {
int method = 0; // 1 get, 2 head, 0 not supported
String http = new String();
String path = new String();
String file = new String();
String user_agent = new String();
try {
// Two types of requests we can handle:
// Get /index.html HTTP/1.0
// HEAD /index.html HTTP/1.0
String tmp = input.readLine(); // Read from the stream
String tmp2 = new String(tmp);
tmp.toUpperCase();
if (tmp.startsWith("GET")) {
method = 1;
}
if (tmp.startsWith("HEAD")) {
method = 2;
}
if (method == 0) {
try {
// If the request is unsupported, send the error
output.writeBytes(construct_http_header(501, 0));
output.close();
return;
} catch (Exception e2) {
System.out.println("Error: " + e2.getMessage());
}
}
// Get whats between the spaces in the request
// without the beginning slash
int start = 0;
int end = 0;
for (int a = 0; a < tmp2.length(); a++) {
if (tmp2.charAt(a) == ' ' && start != 0) {
end = a;
break;
}
if (tmp2.charAt(a) == ' ' && start == 0) {
start = a;
}
}
path = tmp2.substring(start + 2, end);
} catch(Exception e) {
System.out.println("Error: " + e.getMessage());
}
FileInputStream requestedfile = null;
try {
// Try to open the file
requestedfile = new FileInputStream(path);
} catch (Exception e) {
try {
output.writeBytes(construct_http_header(404, 0));
output.close();
} catch (Exception e2) {}
;
System.out.println("Error: " + e.getMessage());
}
try {
int type_is = 0;
// Find out what the filename ends with so we can
// put the right MIME type in the header
if (path.endsWith(".zip") || path.endsWith(".exe")
|| path.endsWith(".tar")) {
type_is = 3;
} else if (path.endsWith(".jpg") || path.endsWith(".jpeg")) {
type_is = 1;
} else if (path.endsWith(".gif")) {
type_is = 2;
} else {
type_is = 5;
}
// write out the header, 200 -> OK
output.writeBytes(construct_http_header(200, type_is));
// if the request was HEAD, we don't print the body
if (method == 1) {
while (true) {
// read the file from the filestream and ouput through
// the client outpustream
int b = requestedfile.read();
if (b == -1) {
break; // end of file
}
output.write(b);
}
}
// Clean up
output.close();
requestedfile.close();
} catch (Exception e) {}
}
**編輯** 我打印的堆棧跟蹤,它是給在
Socket connectionsocket = serversocket.accept();
我一個NullPointerException認爲這種方法應該等待連接。我如何阻止它做它正在做的事情?
而不是僅僅打印消息,使異常轉儲完整的堆棧跟蹤*和異常類型*到控制檯。這會給你更多的信息。 – 2010-04-14 15:29:40
更改您的「錯誤:」行,讓您知道錯誤發生的位置 - 並且也打印堆棧跟蹤(在每個例外情況下) – KevinDTimm 2010-04-14 15:31:29
此類錯誤消息通常表示空指針在某處取消引用。 – 2010-04-14 15:31:39