Results 1 to 4 of 4
  1. #1
    Stevevo is offline Member
    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0

    Default Nullpointerexception driving me mad :(

    Hi guys and gals,
    I've just recently started learning java and I'm having a bit of difficulty with a program. Basically what I have to to is read in 10 names from a text file and place them in string[]. Then do 3 separate methods, one to print the array, one to reverse the elements in the array and the last one to write the reversed elements to a file. Funnily enough it worked perfectly yesterday, but today it not Getting a null pointer exception at line 50, and then line 30(which i guess are related). Its driving me mad Any help/hints appreciated. Here's the code:

    Java Code:
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    
    	public class ReverseArrays {
    		public static void main(String[] args) {
    			FileReader fr;
    			try {
    				fr = new FileReader("C:/java/Assignment2Q3.txt");
    				//read file
    				BufferedReader br = new BufferedReader(fr);
    				//read file line by line
    				String[] names;
    				//initialise string array
    				names = new String[10];
    				//size of array
    				System.out.println("Names from file: ");
    				for (int j = 0; j < names.length; j++){
    				//start at first array, stop at last array, loop through in increments of 1
    				names[j] = br.readLine();
    				//each new line placed in an array index
    				}
    				br.close();
    				//close buffered writer
    				printArray(names);
    				//print array using print method
    				String[] reversed = reverse(names);
    				//reverse string array using reverse method
    				writeToFile(reversed, "C:/java/Assignment2Q3.txt", true);
    				}catch (IOException e) {
    					e.printStackTrace();
    					System.out.println("File does not exist!!");
    				}
    		}
    			public static void printArray(String[] names){   
    				for(int j = 0; j < names.length; j++){
    					//start at first array index, stop at last array index, increments of 1
    				System.out.println(names[j]);
    					//print string array
    					//end of method
    				}
                }
    			public static String[] reverse(String[] name){
    					String [] reversed = new String[name.length];
    					for (int i=0;i<name.length;i++){
    					StringBuffer sb=new StringBuffer();
    					for (int j=0;j<name[i].length()-1;j++)
    					sb.append(name[i].charAt(name[i].length()-1-j));
    					reversed[i]=sb.toString();
    					}
    				return reversed;
    					//return reversed string array
    					//end of method
    			}
    			public static boolean writeToFile(String[] textLines, String filePath, boolean append){ 
    				//method for printing string array to file
    				boolean wroteOK = false;  
    			        BufferedWriter bw = null;  
    			        String lineSpace = System.getProperty("line.separator");
    			        	//add text to new line
    			        try {
    						bw = new BufferedWriter(new FileWriter(filePath, append)); 
    							//
    			            for (int i = 0; i < textLines.length; i++){  
    			            	//start at first array, stop at the end of the array, increments of 1.
    			            bw.write(textLines[i] + lineSpace);  
    			            	//write each element of array to file, new line after each element
    			            }
    			            wroteOK = true;  
    			            bw.close();   
    			            //close buffered write      
    			        } catch (IOException e) {
    						// TODO Auto-generated catch block
    						e.printStackTrace(); 
    			        }
    			        {
    					return wroteOK;
    					//return boolean
    					//end of method
    	
    			 }
    Sorry for my crappy code, I'm still learning the basics

  2. #2
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Nullpointerexception driving me mad :(

    I see that you are missing two brackets at the end and your code could use some reformatting... ;)
    If you give us your text file to reproduce the error you may get help. The program seems to work fine for me... maybe you have no "\n" at the last line in your file, so it does not get read properly by readLine? Check if that method returns a "null" at some point!
    This may happen if you do not have 10 lines in your text file...
    Last edited by Sierra; 12-23-2012 at 03:27 PM.
    I like likes!

  3. #3
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    107
    Rep Power
    0

    Default Re: Nullpointerexception driving me mad :(

    I tried out your program and I believe that you are getting NullPointerException errors because the data file has less than ten name in it.

    Edit: Previously, I posted fixed code, but somebody critisized me for "spoon-feeding" so I deleted the code.

    Just be aware that when you have an array of objects and the array isn't full, that the remaining elements are set to null and you should test that an object isn't null before you try to use it.

    Also, where you were reversing the names in the StringBuffer, there is an easy way to reverse a String buffer, you can call the reverse() method.
    Last edited by kaydell2; 12-27-2012 at 10:38 PM. Reason: So as not to "spoon-feed".

  4. #4
    doWhile is online now Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    6

    Default Re: Nullpointerexception driving me mad :(

    Quote Originally Posted by kaydell2 View Post
    I tried out your program and I believe that you are getting NullPointerException errors because the data file has less than ten name in it.

    See the comments in the code below, where I put in "arrows" <<<<< in the comments where I'm trying to call your attention to changes that I've made:
    Why not elaborate on the 'solution' - even perhaps describe suggested changes, why make them, the reasoning, and the process to get to that reasoning? Suggested reading: The Problem with Spoon-feeding

Similar Threads

  1. Methods driving me mad
    By GrumpyBum in forum New To Java
    Replies: 4
    Last Post: 04-22-2012, 05:09 AM
  2. Driving me crazy!!!! Please help!!
    By Quizzle23 in forum New To Java
    Replies: 33
    Last Post: 02-24-2011, 08:00 AM
  3. The null message is driving me crazy
    By Yakg in forum New To Java
    Replies: 5
    Last Post: 12-02-2010, 07:03 PM
  4. image does not refresh-driving me crazy
    By jambon in forum AWT / Swing
    Replies: 1
    Last Post: 04-09-2010, 04:25 PM
  5. Loop driving me loopy!!!!!
    By soc86 in forum New To Java
    Replies: 8
    Last Post: 01-16-2009, 01:00 AM

Posting Permissions

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