Aşağıdaki kodda boş bir işaretçi istisna hatası yaşıyorum.Boş işaretçi istisnası
Exception in thread "main" java.lang.NullPointerException
at BookTest.createInstances(BookTest.java:53)
at BookTest.main(BookTest.java:20)
gerçekten yardıma ihtiyacı var, oldukça uzun bir süre bu sıkışmış ive.
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();
}
}
}
bu
dosya im 978-0446563079 13.99 222 gerçek 12 2015/01/01Kurgu Frankenstein Shelley Prescott GA 978-0486282114
Kurmaca AbrahamLincolnVampireHunter Grahame-Smith Wiley NY okumaya çalışıyor 7.99 321 doğru 8 5/12/2008
Roman Harici LifeofKennedy Jones Pearson MT 758-29.304.566 12.90 biyografi 3 11/11/2011
"inputArray" öğesinin her öğesine bir değer atadığınızdan emin misiniz? – fabian
Ve burada, bu cevabın cevabımı yayınlamak istemeden önce kapatıldığını görmek için ayrıntılı bir cevap üzerinde çalışıyorum. – mezzodrinker