gpt4 book ai didi

java - 在用java编程时,我得到这个错误,有谁能告诉我到底出了什么问题吗?

转载 作者:行者123 更新时间:2023-12-01 17:24:37 25 4
gpt4 key购买 nike

我正在做一项作业,该作业从文件中读取,并检测哪个字母使用最多,哪个字母使用最少。这是我得到的输出。

Would you like to input a directory for a .dat file? Input Yes or No
Yes
Please input file location
G:\test.dat
Exception in thread "main" java.lang.NullPointerException
at Histogram.loadAndAnalyzeFile(Histogram.java:62)
at lab21c.main(newlab21c.java:15)

Process completed.

这是我的直方图文件

//© A+ Computer Science  -  www.apluscompsci.com
//Name -
//Date -
//Lab -

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import static java.lang.System.*;

public class Histogram
{
private ArrayList<Integer> count;
int counter = 0;
private String[] Letters = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
private String fileName;
ArrayList<String> storageStr;
String Yes = "Yes";
String Decision = "";
String Location = "";
String add = "";
Scanner Keyboard = new Scanner(System.in);

public Histogram()
{




}

public Histogram(char[] values, String fName)
{






}

public void loadAndAnalyzeFile() throws IOException
{
System.out.println("Would you like to input a directory for a .dat file? Input Yes or No");
Decision = Keyboard.next();




if (Decision.equals(Yes))
{
System.out.println("Please input file location");
String location = Keyboard.next();
Scanner file = new Scanner(new File(location));


while (file.hasNext())
{
add = file.next();
storageStr.add(add);
}

}
String[]Holder = new String[storageStr.size()-1];

for (int run = 0; run < storageStr.size(); run++)
{
Holder[run].equals(storageStr.get(run));

for (int run1 = 0; run1 < Letters.length; run1++)
{
char Charletters = Letters[run1].charAt(0);

for (int run2 = 0; run2 < Holder.length; run2++)
{
if (Holder[run].charAt(run2) == Charletters)
{
counter++;
}
}
System.out.println(counter);
}
}









}

public char mostFrequent()
{

return '#';
}

public char leastFrequent()
{


return '#';
}

public String toString()
{
return fileName
+ "\n" + count + "\n\n\n";
}
}

我目前正在做的是尝试转换从文件中读取的字符串,并将其转换为字符以与字母进行比较。我正在显示计数器,因此我可以查看它是否正常工作,但我收到了该错误。这是我的新lab12c 文件。

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import static java.lang.System.*;
import java.io.FileNotFoundException;

class lab21c
{
public static void main(String[] args)throws java.io.IOException
{
Histogram histogram = new Histogram();

histogram.loadAndAnalyzeFile();
}
}

如果有人可以提供一些帮助,我们将不胜感激。

最佳答案

根据我的 IDE,第 62 行 包含对 storageStr 数据结构的操作。我注意到,您从未初始化任何数据结构,这意味着对它们的任何操作都会产生 NullPointerException

例如,将构造函数更改为如下所示:

public Histogram()
{
storageStr = new ArrayList<String>();
count = new ArrayList<Integer>();
}

应该有助于解决问题。

关于java - 在用java编程时,我得到这个错误,有谁能告诉我到底出了什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16007974/

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