Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.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

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_Nullpointerexception - Fatal编程技术网

Java 空指针异常

Java 空指针异常,java,arrays,nullpointerexception,Java,Arrays,Nullpointerexception,我在下面的代码中有一个空指针异常错误 Exception in thread "main" java.lang.NullPointerException at BookTest.createInstances(BookTest.java:53) at BookTest.main(BookTest.java:20) 我真的需要一些帮助,我已经在这上面呆了很长时间了 import java.io.*; import java.io.FileNotFoundException; import jav

我在下面的代码中有一个空指针异常错误

Exception in thread "main" java.lang.NullPointerException
at BookTest.createInstances(BookTest.java:53)
at BookTest.main(BookTest.java:20)
我真的需要一些帮助,我已经在这上面呆了很长时间了

import java.io.*;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Formatter;
import java.util.Scanner;

import javax.swing.JOptionPane;

public class BookTest
{


public static void main (String[] args){

ArrayList list = createInstances();
writeFile(list);
 }


public  static ArrayList<Book> createInstances()
{
ArrayList<Book> bookArray = new ArrayList<Book>();
String inputArray[] = new String [10];
int i = 0;
Scanner input;

   // Read the text file and stores into inputArray where each line is stored as String.
try 
{
    input = new Scanner( new File("book.txt"));
    input.useDelimiter("\n");
    while (input.hasNext()){
        inputArray[i]=input.next();
        i++;
    }





    // dataArray defines the two dimensional array that store all the values in the line.
    String dataArray [] [] = new String [10] [11]; 

    System.out.println(inputArray.length);
    for (int k =0; k<inputArray.length; k++){

        String getLine = inputArray[k];
        String[] eachLine =getLine.split(" ");
        int length = eachLine.length;


          for ( int j=0; j<length;j++){
             dataArray [k][j]= eachLine[j];
         }
    }



    for (int l = 0; l < 10; l++)
        {
            if (dataArray[l][0].equals("Fiction"))
            {
                Publisher p = new Publisher(dataArray[l][3], dataArray[l][4]);
                bookArray.add(new Fiction(dataArray[l][1], dataArray[l][2], dataArray[l][5],
                p, Double.parseDouble(dataArray[l][6]), dataArray[l][7], l));
            }

            else 
            {
                Publisher p = new Publisher(dataArray[l][3], dataArray[l][4]);
                bookArray.add(new NonFiction(dataArray[l][1], dataArray[l][2],dataArray[l][5],
                p, Double.parseDouble(dataArray[l][6]), dataArray[l][7], l));
            }
        }

} 

catch (FileNotFoundException e) {

    e.printStackTrace();
}
return bookArray;
}


public static void writeFile(ArrayList arrayOfBook)

{
Formatter output ; 

try 

{
     output = new Formatter("book.txt");
            for(int i = 0; i<11;i++)
            {
                output.format( "%s %s %s %s %s %s %s %s %s %s %s \n", arrayOfBook.get(i));
            }
    }       
 catch (IOException e) 

 {
        // TODO Auto-generated catch block
        e.printStackTrace();
        } 

    }
  }
import java.io.*;
导入java.io.FileNotFoundException;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.Formatter;
导入java.util.Scanner;
导入javax.swing.JOptionPane;
公开课图书考试
{
公共静态void main(字符串[]args){
ArrayList=createInstances();
可写文件(列表);
}
公共静态ArrayList createInstances()
{
ArrayList bookArray=新的ArrayList();
字符串输入数组[]=新字符串[10];
int i=0;
扫描仪输入;
//读取文本文件并存储到inputArray中,其中每一行都存储为字符串。
尝试
{
输入=新扫描仪(新文件(“book.txt”);
input.useDelimiter(“\n”);
while(input.hasNext()){
inputArray[i]=input.next();
i++;
}
//dataArray定义存储行中所有值的二维数组。
字符串dataArray[][]=新字符串[10][11];
System.out.println(输入数组长度);

对于(int k=0;k您已经将您的inputArray声明为长度为10的数组(如果您不能保证文件正好包含10行,这可能不是最好的主意),这意味着剩余的元素(文件中没有行)保持为null,并且您使用null.split(“”),这将导致异常。请确保正好有10行,或者使用动态数据结构(例如
ArrayList
)相反。

您确定为
inputArray
的每个元素分配了一个值吗?我正在做一个详细的答案,只是注意到在我想发布答案之前,这个问题已经结束了。