在java中编程时,我收到此错误,是否有人能够告诉我到底出了什么问题?

时间:2013-04-15 05:02:32

标签: java arraylist nullpointerexception

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

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.

这是我的Histrogram文件

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

我目前正在尝试转换从文件中读取的字符串,并将其转换为char以与字母进行比较。我正在显示计数器,所以我可以看看它是否正常工作,但我得到了这个错误。这是我的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();
    }
}

如果有人能提供一些帮助,那将非常感激。

1 个答案:

答案 0 :(得分:5)

根据我的IDE,Line 62包含storageStr数据结构的操作。我注意到你永远不会初始化任何数据结构,因此意味着对它们的任何操作都会产生NullPointerException

例如,将构造函数更改为类似的内容:

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

应该有助于解决问题。