Hi,
I am using bubble sort to sort a 2d array however the problem is that I am going to have a null line in the middle of my text file and was wondering if there is a null check I can put in my code.
Here is my code - :
This is the error I get -:Code:import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;
public class BubbleSort {
static Boolean swaps;
static int i;
static int count;
static String[][] data = new String[10][7];
static Date TheDate[] = new Date[10];
static String Payee[] = new String[10];;
static double Amount[] = new double[10];;
static String Type[] = new String[10];;
static String Category[] = new String[10];
static double Balance[]= new double[10];
static String Notes[]= new String[10];
static Date temp;
static String temp1,temp3,temp4,temp6,temp7;
static double temp2;
static double temp5;
static SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yyyy");
public static void main(String[] args) throws ParseException {
// TODO Auto-generated method stub
File TransactionFile = new File("Transactionollyhal.csv");
BufferedReader out = null;
try {
out = new BufferedReader(new FileReader(TransactionFile));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String strLine = null;
int row = 0;
int col = 0;
try {
while((strLine = out.readLine()) != null)
{
StringTokenizer st = new StringTokenizer(strLine,",");
while (st.hasMoreTokens())
{
//get next token and store it in the array
data[row][col] = st.nextToken();
col++;
}
count++;
col = 0;
row++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(int i = 0; i < 5; i++){
if(data[i][0] !=null){
TheDate[i] = (Date)formatter.parse(data[i][0]);
Payee[i] = data[i][1];
Amount[i] = Double.parseDouble(data[i][2]);
Type[i] = data[i][3];
Category[i] = data[i][4];
Balance[i] = Double.parseDouble(data[i][5]);
Notes[i] = data[i][6];
}
}
for(int i = 0; i < 3; i++){
System.out.println(TheDate[i] + ", " + Payee[i] + " " +Amount[i] + " " +Type[i] + " " +Category[i] + " " +Balance[i] + " " +Notes[i]);
}
System.out.println("Count = " + count);
do{
swaps = false;
i = 0;
do{
if(TheDate[i].after(TheDate[i+1])){
temp = TheDate[i];
temp1 = Payee[i];
temp2 = Amount[i];
temp3 = Type[i];
temp4 = Category[i];
temp5 = Balance[i];
temp6 = Notes[i];
TheDate[i] = TheDate[i+1];
Payee[i] = Payee[i+1];
Amount[i] = Amount[i+1];
Type[i] = Type[i+1];
Category[i] = Category[i+1];
Balance[i] = Balance[i+1];
Notes[i] = Notes[i+1];
TheDate[i+1] = temp;
Payee[i+1] = temp1;
Amount[i+1] = temp2;
Type[i+1] = temp3;
Category[i+1] = temp4;
Balance[i+1] = temp5;
Notes[i+1] = temp6;
swaps = true;
} else {
}
i++;
}while(i < 4);
}while(swaps == true);
for(int i = 0; i < 4; i++){
System.out.println(TheDate[i] + ", " + Payee[i] + " " +Amount[i] + " " +Type[i] + " " +Category[i] + " " +Balance[i] + " " +Notes[i]);
}
}
}
Exception in thread "main" java.lang.NullPointerException
at java.util.Date.getMillisOf(Unknown Source)
at java.util.Date.after(Unknown Source)
at BubbleSort.main(BubbleSort.java:95)
I believe this error is to do with the date being null therefore I need to find a way to get round it.
Here is a link to the text file -: Transactionollyhal.csv download - 2shared
Thanks,

