Results 1 to 10 of 10
Like Tree1Likes
  • 1 Post By Norm

Thread: Java Null error help

  1. #1
    OllyHal is offline Member
    Join Date
    Feb 2012
    Posts
    25
    Rep Power
    0

    Default 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 - :
    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]);
    		}
    
    	}
    
    }
    This is the error I get -:
    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,

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default 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?

    a null line in the middle of my text file
    How can a text file contain a null value?
    Last edited by Norm; 04-15-2012 at 05:47 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    OllyHal is offline Member
    Join Date
    Feb 2012
    Posts
    25
    Rep Power
    0

    Default Re: Java Null error help

    Quote Originally Posted by Norm View Post
    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?
    It is just a blank space but java recognises it as a null value.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default 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.
    Fubarable likes this.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    OllyHal is offline Member
    Join Date
    Feb 2012
    Posts
    25
    Rep Power
    0

    Default Re: Java Null error help

    Quote Originally Posted by Norm View Post
    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.
    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]);
            }
    Changing to array to go up to 5, displays this -:
    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
    ,,,,,

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default 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.

  7. #7
    OllyHal is offline Member
    Join Date
    Feb 2012
    Posts
    25
    Rep Power
    0

    Default 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 06:15 PM.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default 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.

  9. #9
    OllyHal is offline Member
    Join Date
    Feb 2012
    Posts
    25
    Rep Power
    0

    Default 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];
                    }
            }

  10. #10
    OllyHal is offline Member
    Join Date
    Feb 2012
    Posts
    25
    Rep Power
    0

    Default Re: Java Null error help

    Got it working!

    This is my check -:
    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;
                                      
                    }
                    }
    This is my output -:

    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
    Now all I need to do is loop until null and save it to a file!

    Thanks for all your help! Much appreciated!
    Last edited by OllyHal; 04-15-2012 at 07:28 PM. Reason: Thanks for all your help! Much appreciated!

Similar Threads

  1. HashMap Null error
    By anoorally in forum New To Java
    Replies: 4
    Last Post: 02-11-2012, 07:45 PM
  2. null pointer error help!!
    By fakepics500 in forum New To Java
    Replies: 1
    Last Post: 07-16-2011, 03:57 PM
  3. quick help, if(n == null) error?
    By JayP in forum New To Java
    Replies: 3
    Last Post: 06-10-2011, 10:14 PM
  4. help with with my null error
    By zhangster in forum New To Java
    Replies: 3
    Last Post: 03-20-2010, 01:32 AM
  5. Null Error
    By scoleman123 in forum New To Java
    Replies: 2
    Last Post: 09-19-2008, 05:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •