以下のコードでヌルポインタ例外エラーが発生しました。ヌルポインタ例外
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();
}
}
}
これは978から0446563079 13.99 222真12 2015年1月1日
フィクションフランケンシュタインシェリープレスコットGA 978から0486282114
フィクションAbrahamLincolnVampireHunterグレアム・スミスワイリーNYを読み取ろうとするファイルのイムです7.99 321真8 2008年5月12日
ノンフィクションLifeofKennedyジョーンズピアソンMT 758から29304566 12.90伝記3 2011年11月11日
あなたは 'inputArray'のすべての要素に値を割り当ててもよろしいですか? – fabian
ここで私は詳細な答えを出していましたが、私は自分の答えを投稿する前にこの質問が閉じられていることに気付くだけでした。 – mezzodrinker