Java I';我在加粗的代码行中得到一个空指针异常!我做错了什么? 公共类kmeansgeneral{ 公共静态void main(最终字符串args[])引发异常{ int字=0; int chars=0; int行=0; 字符串s,sp1; StringTokenizer st; 最终ArrayList x=新ArrayList(); 最终文件读取器fr=新文件读取器(“G:/t1.txt”); 最终BufferedReader buf=新的BufferedReader(fr); //计算字数和行数 而((s=buf.readLine())!=null){ 行++; st=新的StringTokenizer,“,;:.”; 而(st.hasMoreTokens()){ words++; s=圣奈克特肯(); 字符数+=s.长度(); 最终双y=新双y; x、 加(y); } } System.out.println(“字数:+字/行); System.out.println(“行计数:+行); //计算并打印字数和行尾数 双ct[]=新双精度[0]; ct=x.toArray(ct); //输入数组,依次读取的值,浮点 //double[]indat=新的双[行][行*字]; 最后一双[][]indat=新双[10][10]; int inval=0; final BufferedReader buf1=新的BufferedReader(新的InputStreamReader(System.in)); 系统输出打印(“输入K值:”); sp1=buf1.readLine(); 最终整数ky=新整数(sp1); 最终整数k=ky; System.out.println(“K==”+K); //现在依次读取输入数组值 对于(int i=0;i
首先,你应该重发你的帖子,以便更清楚问题出在哪里 然后删除所有不相关的代码 然后看看你的stacktrace,它应该说哪一行错了,那一行你应该发布 我猜是带**的那个,否则您的代码将无法编译 所以我假设这是一条线:Java I';我在加粗的代码行中得到一个空指针异常!我做错了什么? 公共类kmeansgeneral{ 公共静态void main(最终字符串args[])引发异常{ int字=0; int chars=0; int行=0; 字符串s,sp1; StringTokenizer st; 最终ArrayList x=新ArrayList(); 最终文件读取器fr=新文件读取器(“G:/t1.txt”); 最终BufferedReader buf=新的BufferedReader(fr); //计算字数和行数 而((s=buf.readLine())!=null){ 行++; st=新的StringTokenizer,“,;:.”; 而(st.hasMoreTokens()){ words++; s=圣奈克特肯(); 字符数+=s.长度(); 最终双y=新双y; x、 加(y); } } System.out.println(“字数:+字/行); System.out.println(“行计数:+行); //计算并打印字数和行尾数 双ct[]=新双精度[0]; ct=x.toArray(ct); //输入数组,依次读取的值,浮点 //double[]indat=新的双[行][行*字]; 最后一双[][]indat=新双[10][10]; int inval=0; final BufferedReader buf1=新的BufferedReader(新的InputStreamReader(System.in)); 系统输出打印(“输入K值:”); sp1=buf1.readLine(); 最终整数ky=新整数(sp1); 最终整数k=ky; System.out.println(“K==”+K); //现在依次读取输入数组值 对于(int i=0;i,java,Java,首先,你应该重发你的帖子,以便更清楚问题出在哪里 然后删除所有不相关的代码 然后看看你的stacktrace,它应该说哪一行错了,那一行你应该发布 我猜是带**的那个,否则您的代码将无法编译 所以我假设这是一条线: public class kmeansgeneral { public static void main(final String args[]) throws Exception { int words = 0; int chars = 0
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));
}
}
如果是,那么您的错误是您初始化了数组,但没有初始化数组中的每个元素
旁注:
您真的需要那个向量数组吗?让我猜猜:您可能在eclipse或某个编辑器中调试此代码时遇到了此错误,该编辑器无法为jvm提供控制台 如果是这样的话,你可能会
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();
123
234
345
34456
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)
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);
}
}
if (FinalClusters[i][j] == null) {
FinalClusters[i][j] = new Vector();
}
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]]