Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我想从文本文件中添加值,并将其添加到二维java中。但给出空指针异常_Java_Arrays_Exception - Fatal编程技术网

我想从文本文件中添加值,并将其添加到二维java中。但给出空指针异常

我想从文本文件中添加值,并将其添加到二维java中。但给出空指针异常,java,arrays,exception,Java,Arrays,Exception,我想读取一个文本文件,并将内容添加到java中的二维数组中。但当我将这些值添加到数组中时,我得到了java中的数组越界异常 java.io.File test2 = new java.io.File("e:\\A.txt"); BufferedReader bw =new BufferedReader(new FileReader("e:\\A.txt")); String s=bw.readLine(); while(bw.readLine()!=null) {

我想读取一个文本文件,并将内容添加到java中的二维数组中。但当我将这些值添加到数组中时,我得到了java中的数组越界异常

java.io.File test2 = new java.io.File("e:\\A.txt");

   BufferedReader bw =new BufferedReader(new FileReader("e:\\A.txt"));

   String s=bw.readLine();
   while(bw.readLine()!=null)
   {

       counterRow++;
   }

   System.out.println(counterRow);
   String sw=bw.readLine();

   String[] words=s.split(",");

   counterCol=words.length;
   System.out.println(words.length);


   @SuppressWarnings("resource")
Scanner input = new Scanner(test2);
   String Data[][]=new String[counterRow][counterCol];
   int i1=0,j1=0;
   while(input.hasNext())
   {
       String val=input.nextLine();
       j1=0;
       if(val.contains(","))
       {
           String str[]=val.split(",");
           int cn=str.length;
           while(cn>0)
           {
               Data[i1][j1]=str[j1];
               cn--;
               j1++;
           }
     }
         else
           Data[i1][j1]=val;
       i1++;
   }
我的输入文件是

69,79,82

72,82,84

39,70,75

69,88,68

38,72,61

39,60,40

36,32,44

50,71,55

36,47,47

80,81,90

错误:线程“main”100sss中出现异常 位于的java.lang.NullPointerException com.associa.mining.associateruleminingago.main(associateruleminingago.java:200)


那是因为你在达到EOF后试图阅读

while(bw.readLine()!=null)  // read till EOF
{

   counterRow++;
}

System.out.println(counterRow);
String sw=bw.readLine();         // ERROR cant read, you have reached EOF
PS-您甚至似乎没有使用变量“sw”。

if(val.contains(“,”)
条件更改为
if(val!=null&&val.contains(“,”)

完全码

String val=input.nextLine();
j1=0;
if(val!=null){

   if(val.contains(","))
   {
       String str[]=val.split(",");
       int cn=str.length;
       while(cn>0)
       {
           Data[i1][j1]=str[j1];
           cn--;
           j1++;
       }
     }else
        Data[i1][j1]=val;
     i1++;
 }

第200行是哪一行?