每次我运行我的程序时,我都会收到 NullPointerException,我不知道该怎么做才能让它工作。它应该解析输入文件数据,进行一些计算(所有尚未编码),然后打印到输出文件。
这是我的课程:
司机:
public class Driver
{
public static void main(String[] args) throws IOException
{
Analyzer start = new Analyzer();
start.start();
}
}
分析仪:
public class Analyzer
{
public void start() throws IOException
{
int total = 0;
int totalError = 0;
final int SIZE = 31;
Response data[] = new Response[SIZE];
PrintWriter out = new PrintWriter(new FileWriter("apacheOut.txt"));
Scanner scan = new Scanner (new File("access_log1.txt"));
while(scan.hasNext())
{
String address = scan.next();
scan.next(); //garbage line
scan.next(); //garbage line
String date = scan.next() + " " + scan.next();
String temp = scan.next();
String request = temp.substring(1);
scan.next(); //garbage line
int status = 0;
while(status == 0)
{
if(scan.hasNextInt())
status = scan.nextInt();
else
scan.next(); //garbage line
}
int bytes = 0;
if(status < 300)
{
if(scan.hasNextInt())
{
bytes = scan.nextInt();
}
else
{
scan.next(); //garbage line
}
}
String referAgent = scan.nextLine();
if(status < 400)
{
SuccessResponse srp = new SuccessResponse(address, date, request, status, bytes, referAgent);
}
else
{
ErrorResponse err = new ErrorResponse(address, date, request, status, bytes, referAgent);
totalError++;
}
total++;
}
int numGet = 0;
int numPost = 0;
double numBytes = 0;
for (int i = 0; i <= SIZE; i++)
{
String address = data[i].getAddress();
String date = data[i].getDate();
String request = data[i].getRequest();
int status = data[i].getStatus();
int bytes = data[i].getBytes();
String referAgent = data[i].getReferAgent();
/** GET/POST count */
if (request.substring(0,1) == "G")
{
numGet++;
}
else if (request.substring(0,1) == "P")
{
numPost++;
}
/** Number of total bytes */
numBytes = bytes++;
}
out.println ("Warren Smith's Results");
out.println ("======================");
out.println ("The total number of requests in the file: " + total);
out.println ("The total number of GET requests: " + numGet);
out.println ("The total number of POST requests: " + numPost);
out.println ("The total number of bytes served: " + numBytes);
out.println ("The number & percentage of pages producing various status categorizations:" );
out.println (" 1xx Informational: ");
out.println (" 2xx Status: ");
out.println (" 3xx Redirection: ");
out.println (" 4xx Client Error: ");
out.println (" 5xx Server Error: ");
out.println ("The percentage and number of Windows-based clients: ");
out.println ("The percentage and number of bad requests: " + totalError);
out.println ("The percentage and number of clients that are Mozilla-based: ");
out.println ("The percentage and number of requests from the Googlebot: ");
out.close();
}
}
回复:
abstract public class Response
{
protected String address;
protected String date;
protected String request;
protected int status;
protected int bytes;
protected String referAgent;
/** Response object constructor */
public Response (String eAddress, String eDate, String eRequest, int eStatus, int eBytes, String eReferAgent)
{
address = eAddress;
date = eDate;
request = eRequest;
status = eStatus;
bytes = eBytes;
referAgent = eReferAgent;
}
/** Returns the IP address */
public String getAddress()
{
return address;
}
/** Returns the date */
public String getDate()
{
return date;
}
/** Returns the request to server */
public String getRequest()
{
return request;
}
/** Returns the status code */
public int getStatus()
{
return status;
}
/** Returns the # of bytes */
public int getBytes()
{
return bytes;
}
/** Returns the referring page & agent */
public String getReferAgent()
{
return referAgent;
}
}
成功响应:
public class SuccessResponse extends Response
{
/** SuccessResponse object constructor */
public SuccessResponse (String eAddress, String eDate, String eRequest, int eStatus, int eBytes, String eReferAgent)
{
super (eAddress, eDate, eRequest, eStatus, eBytes, eReferAgent);
}
/** Returns the IP address */
public String getAddress()
{
return address;
}
/** Returns the date */
public String getDate()
{
return date;
}
/** Returns the request to server */
public String getRequest()
{
return request;
}
/** Returns the status code */
public int getStatus()
{
return status;
}
/** Returns the # of bytes */
public int getBytes()
{
return bytes;
}
/** Returns the referring page */
public String getReferAgent()
{
return referAgent;
}
}
错误响应:
public class ErrorResponse extends Response
{
/** ErrorResponse object constructor */
public ErrorResponse (String eAddress, String eDate, String eRequest, int eStatus, int eBytes, String eReferAgent)
{
super (eAddress, eDate, eRequest, eStatus, eBytes, eReferAgent);
}
/** Returns the request to server */
public String getRequest()
{
return request;
}
/** Returns the status code */
public int getStatus()
{
return status;
}
/** Returns the # of bytes */
public int getBytes()
{
return bytes;
}
/** Returns the referring page */
public String getReferAgent()
{
return referAgent;
}
}
输出是:
[java] Exception in thread "main" java.lang.NullPointerException
[java] at Analyzer.start(Unknown Source)
[java] at Driver.main(Unknown Source)
[java] Java Result: 1
为什么 NullPointerException 不断出现的任何想法?