0

我正在处理从文件中读取的任务,并检测哪个字母使用最多,哪个字母使用最少。这是我得到的输出。

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";
    }
}

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

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();
    }
}

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

4

1 回答 1

5

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

例如,将您的构造函数更改为:

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

应该有助于解决问题。

于 2013-04-15T05:07:27.373 回答