Results 1 to 10 of 10
Thread: Java Null error help
- 04-15-2012, 03:46 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 25
- Rep Power
- 0
Java Null error help
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 -:Java 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,
- 04-15-2012, 04:35 PM #2
Re: Java Null error help
What variable has the null value? Can you use an if statement to test if the variable is null and not use it?
How can a text file contain a null value?a null line in the middle of my text fileLast edited by Norm; 04-15-2012 at 04:47 PM.
If you don't understand my response, don't ignore it, ask a question.
- 04-15-2012, 04:53 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 25
- Rep Power
- 0
- 04-15-2012, 04:56 PM #4
Re: Java Null error help
Can you show the code where java treats a blank as a null?
By blank I assume you mean a String: " " containing one or more space characters.If you don't understand my response, don't ignore it, ask a question.
- 04-15-2012, 05:02 PM #5
Member
- Join Date
- Feb 2012
- Posts
- 25
- Rep Power
- 0
Re: Java Null error help
Changing to array to go up to 5, displays this -:Java Code:for(int i = 0; i < 6; i++){ System.out.println(TheDate[i] + ", " + Payee[i] + " " +Amount[i] + " " +Type[i] + " " +Category[i] + " " +Balance[i] + " " +Notes[i]); }
Thu Apr 12 00:00:00 BST 2012, Start 200.0 Bills Withdraw 200.0 Testing
Thu Apr 12 00:00:00 BST 2012, Start 200.0 Bills Deposit 1100.0 Testing
Fri Apr 13 00:00:00 BST 2012, Start 100.0 Bills Deposit 100.0 Testing
Sat Apr 14 00:00:00 BST 2012, Start 100.0 Bills Deposit 3100.0 Testing
Mon Apr 16 00:00:00 BST 2012, Start 300.0 Bills Deposit 200.0 Testing
null, null 0.0 null null 0.0 null
This is with this csv file -:
13 Apr 2012,Start,100,Bills,Deposit,100.0,Testing
14 Apr 2012,Start,100,Bills,Deposit,3100.0,Testing
12 Apr 2012,Start,200,Bills,Withdraw,200.0,Testing
16 Apr 2012,Start,300,Bills,Deposit,200.0,Testing
12 Apr 2012,Start,200,Bills,Deposit,1100.0,Testing
,,,,,
- 04-15-2012, 05:06 PM #6
Re: Java Null error help
What that shows is that elements in several arrays have null values. The arrays have 5 valid values and at least one null value. Why do you expect there to be 6 valid values in the array? Instead of hardcoding a 6 in the for loop, use a variable that has a count of the number of valid values in the arrays.
If you don't understand my response, don't ignore it, ask a question.
- 04-15-2012, 05:13 PM #7
Member
- Join Date
- Feb 2012
- Posts
- 25
- Rep Power
- 0
Re: Java Null error help
There is purposely another blank line as that line has been deleted but will be in the middle of the array, that was just a test to show the null.
Edit: I am just going to use a 2d Arraylist instead.Last edited by OllyHal; 04-15-2012 at 05:15 PM.
- 04-15-2012, 05:17 PM #8
Re: Java Null error help
Why put null values into the array or why skip over elements in an array leaving a null value?
Validate the data before putting it into the array.If you don't understand my response, don't ignore it, ask a question.
- 04-15-2012, 05:24 PM #9
Member
- Join Date
- Feb 2012
- Posts
- 25
- Rep Power
- 0
Re: Java Null error help
What I am doing is deleting a row from a jtable and then saving it back into the file. The problem I have is that it is already in an array when I make it null. I am trying to take it out of the array and then put it back in but not sure how to validate it in between. I have one idea in checking a null here -:
Java Code: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]; } }
- 04-15-2012, 06:27 PM #10
Member
- Join Date
- Feb 2012
- Posts
- 25
- Rep Power
- 0
Re: Java Null error help
Got it working!
This is my check -:
This is my output -:Java Code:for(int j = 0;j < 5;j++){ if(data[j][0] != null){ TheDate[j] = (Date)formatter.parse(data[j][0]); Payee[j] = data[j][1]; Amount[j] = Double.parseDouble(data[j][2]); Type[j] = data[j][3]; Category[j] = data[j][4]; Balance[j] = Double.parseDouble(data[j][5]); Notes[j] = data[j][6]; }else if(data[j][0] == null) { TheDate[j] = (Date)formatter.parse(data[j+1][0]); Payee[j] = data[j+1][1]; Amount[j] = Double.parseDouble(data[j+1][2]); Type[j] = data[j+1][3]; Category[j] = data[j+1][4]; Balance[j] = Double.parseDouble(data[j+1][5]); Notes[j] = data[j+1][6]; data[j+1][0] = null; data[j+1][1] = null; data[j+1][2] = null; data[j+1][3] = null; data[j+1][4] = null; data[j+1][5] = null; data[j+1][6] = null; } }
Now all I need to do is loop until null and save it to a file!Before null check:
13 Apr 2012, Start 100 Bills Deposit 100.0 Testing
14 Apr 2012, Start 100 Bills Deposit 3100.0 Testing
12 Apr 2012, Start 200 Bills Withdraw 200.0 Testing
null, null null null null null null
16 Apr 2012, Start 300 Bills Deposit 200.0 Testing
12 Apr 2012, Start 200 Bills Deposit 1100.0 Testing
After null check:
Fri Apr 13 00:00:00 BST 2012, Start 100.0 Bills Deposit 100.0 Testing
Sat Apr 14 00:00:00 BST 2012, Start 100.0 Bills Deposit 3100.0 Testing
Thu Apr 12 00:00:00 BST 2012, Start 200.0 Bills Withdraw 200.0 Testing
Mon Apr 16 00:00:00 BST 2012, Start 300.0 Bills Deposit 200.0 Testing
Thu Apr 12 00:00:00 BST 2012, Start 200.0 Bills Deposit 1100.0 Testing
null, null 0.0 null null 0.0 null
After bubble sort:
Thu Apr 12 00:00:00 BST 2012, Start 200.0 Bills Withdraw 200.0 Testing
Thu Apr 12 00:00:00 BST 2012, Start 200.0 Bills Deposit 1100.0 Testing
Fri Apr 13 00:00:00 BST 2012, Start 100.0 Bills Deposit 100.0 Testing
Sat Apr 14 00:00:00 BST 2012, Start 100.0 Bills Deposit 3100.0 Testing
Mon Apr 16 00:00:00 BST 2012, Start 300.0 Bills Deposit 200.0 Testing
null, null 0.0 null null 0.0 null
Thanks for all your help! Much appreciated!Last edited by OllyHal; 04-15-2012 at 06:28 PM. Reason: Thanks for all your help! Much appreciated!
Similar Threads
-
HashMap Null error
By anoorally in forum New To JavaReplies: 4Last Post: 02-11-2012, 06:45 PM -
null pointer error help!!
By fakepics500 in forum New To JavaReplies: 1Last Post: 07-16-2011, 02:57 PM -
quick help, if(n == null) error?
By JayP in forum New To JavaReplies: 3Last Post: 06-10-2011, 09:14 PM -
help with with my null error
By zhangster in forum New To JavaReplies: 3Last Post: 03-20-2010, 12:32 AM -
Null Error
By scoleman123 in forum New To JavaReplies: 2Last Post: 09-19-2008, 04:04 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks