gpt4 book ai didi

java - 运行时出现 NullPointerException 原因不明

转载 作者:行者123 更新时间:2023-12-02 07:28:42 24 4
gpt4 key购买 nike

每次运行程序时,我都会收到 NullPointerException,但我不知道该怎么做才能使其正常工作。它应该解析输入文件数据,进行一些计算(所有计算尚未编码),然后打印到输出文件。

这是我的类(class):

驱动程序:

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 不断出现吗?

最佳答案

似乎 data 数组中的元素未初始化,但您尝试使用它们调用方法。

关于java - 运行时出现 NullPointerException 原因不明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13205145/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com