简体   繁体   中英

I'm getting a NULL POinter Exception at the boldened lines of code! What am I doing wrong?

public class kmeansgeneral {
    public static void main(final String args[]) throws Exception {

        int words = 0;
        int chars = 0;
        int lines = 0;

        String s, sp1;
        StringTokenizer st;
        final ArrayList<Double> x = new ArrayList<Double>();

        final FileReader fr = new FileReader("G:/t1.txt");
        final BufferedReader buf = new BufferedReader(fr);

        // Counting number of words and lines
        while ((s = buf.readLine()) != null) {
            lines++;
            st = new StringTokenizer(s, " ,;:.");

            while (st.hasMoreTokens()) {

                words++;
                s = st.nextToken();
                chars += s.length();

                final Double y = new Double(s);
                x.add(y);

            }

        }

        System.out.println("Word Count : " + words / lines);
        System.out.println("Line Count : " + lines);
        // Counting and printing number of words and lines ENDS

        Double ct[] = new Double[0];
        ct = x.toArray(ct);

        // Input array, values to be read in successively, float
        // double[][] indat = new double[lines][lines*words];
        final double[][] indat = new double[10][10];
        int inval = 0;

        final BufferedReader buf1 = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter K-Value : ");
        sp1 = buf1.readLine();

        final Integer ky = new Integer(sp1);
        final int k = ky;

        System.out.println("K ==  " + k);

        // Now read in input array values, successively

        for (int i = 0; i < lines; i++) {
            for (int j = 0; j < words / lines; j++) {

                indat[i][j] = ct[inval];
                inval++;

                System.out.print(indat[i][j]);
                System.out.print("\t");
            }
            System.out.println();
        }


        // Initial Clusters
        System.out.println("   ");
        System.out.println(k + " seed  points ");

        final double[][] Clusters = new double[k][lines * words];
        // double[][] calcnt = new double[lines][words];
        final double[][] calcnt = new double[1000][1000];
        final double[][] array = new double[k][lines * words];

        System.out.println("Clusters==>");
        // int pos=0;
        for (int i = 0; i < k; i++) {
            for (int j = 0; j < words / lines; j++) {

                Clusters[i][j] = indat[i][j];
                // pos= j;

                System.out.print(Clusters[i][j]);
                System.out.print("\t");
            }
            System.out.println();

        }
        System.out.println("PRINTING VECTOR ELEMENTS:");
        final Vector FinalClusters[][] = new Vector[100][100];

        for (int i = 0; i < k; i++) {
            for (int j = 0; j < words / lines; j++) {
                final String tempString = String.valueOf(Clusters[i][j]);
                FinalClusters[i][j].add(tempString);
            }

        }

        // Inital Cluster Array
        System.out.println("Initial Cluster Array");
        int b = 0;
        final double[] arr = new double[2000];

        for (int i = 0; i < k; i++) {
            for (int j = 0; j < words / lines; j++) {

                arr[b] = Clusters[i][j]; // = indat[i][j];
                System.out.print(arr[b]);
                System.out.print("\t");
                b++;
            }

        }
        System.out.println();


        System.out.println("Centroids");

        for (int i = 0; i < k; i++) {
            for (int j = 0; j < words / lines; j++) {

                calcnt[i][j] = (Clusters[i][j] + indat[k][j]) / 2;

                System.out.print(calcnt[i][j]);
                System.out.print("\t");

            }

        }

        // Claculate Distances

        // System.out.println("MAGIC # 3");

        final double[] dist = new double[k];

        for (int i = 0; i < k; i++) {

            double dis = 0;
            for (int j = 0; j < words / lines; j++) {

                dis = dis + (Math.pow(Clusters[i][j] - indat[k][j], 2));

            }
            dist[i] = (Math.sqrt(dis));
            System.out.println("From Cluster K = " + i + "\t" + "Distance" + dist[i]);
        }

        System.out.println("To Find Minimum Distance ");

        double min = dist[0];

        int y = 0;

        for (int m = 0; m < k; m++) {

            if (dist[m] < min) {
                min = dist[m];
                y = m;
            }
        }
        System.out.print("Min Value =" + min + "\t" + "For Cluster :" + y);

        System.out.println();
        System.out.print("Added Cluster =");
        final double[] temp = new double[lines * (words / lines)];

        for (int j = 0; j < words / lines; j++) {

            temp[j] = indat[k][j];

            System.out.print(temp[j]);
            System.out.print("\t");

        }
        System.out.println();

        final Vector[] vector = new Vector[k];
        for (int i = 0; i < k; i++) {
            vector[i] = new Vector<Object>();
        }

        for (int i = 0; i < words / lines; i++) {
            vector[y].add(String.valueOf(temp[i]));
        }

        System.out.println(Arrays.toString(vector));


    }
}

First you should redit your post to make it more clear what the problem is.

Then remove all irrelevant code.

Then look at your stacktrace, it should say what line is wrong, that line you should post.

I guess its the one with the ** otherwise your code would not compile

so i assume its this line:

      FinalClusters[i][j].add(tempString);//is it this line ?
      // where did you initialise FinalClusters[i][j] ??
      // maybe you first need FinalClusters[i][j] = new Vector(); 

if it is then your error is that you did initiliase the array but not each individual element in the array.

Sidenote: Do you really need that array of Vectors?

I ran your code on the following input file:

t1.txt:

123
234
345
34456 

Output:

Word Count : 1
Line Count : 4
Enter K-Value : 3
K ==  3
123.0   
234.0   
345.0   
34456.0 

3 seed  points 
Clusters==>
123.0   
234.0   
345.0   
PRINTING VECTOR ELEMENTS:
Exception in thread "main" java.lang.NullPointerException
    at test.kmeansgeneral.main(kmeansgeneral.java:106)

That's this bit of code:

System.out.println("PRINTING VECTOR ELEMENTS:");
final Vector FinalClusters[][] = new Vector[100][100];

for (int i = 0; i < k; i++) {
    for (int j = 0; j < words / lines; j++) {
        final String tempString = String.valueOf(Clusters[i][j]);
        // NPE OCCURS IN THE LINE BELOW
        FinalClusters[i][j].add(tempString);
    }
}

The problem is the array 'FinalClusters' contains null values, not empty Vectors. So here's a fix, add the following lines above the line where the error occurs:

if (FinalClusters[i][j] == null) {
    FinalClusters[i][j] = new Vector();
}

Then the output is:

Word Count : 1
Line Count : 4
Enter K-Value : 3
K ==  3
123.0   
234.0   
345.0   
34456.0 

3 seed  points 
Clusters==>
123.0   
234.0   
345.0   
PRINTING VECTOR ELEMENTS:
Initial Cluster Array
123.0   234.0   345.0   
Centroids
17289.5 17345.0 17400.5 From Cluster K = 0  Distance34333.0
From Cluster K = 1  Distance34222.0
From Cluster K = 2  Distance34111.0
To Find Minimum Distance 
Min Value =34111.0  For Cluster :2
Added Cluster =34456.0  
[[], [], [34456.0]]

I don't have a clue what this is supposed to do, so can't tell if this is right. At least no exception is thrown anymore.

Let me guess: You're probably getting this error debugging this code in eclipse or some editor that doesn't make available a console to the jvm.

If this is the case then you'd probably hit a nullpointer at sp1 = buf1.readLine();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM