Results 1 to 4 of 4
- 12-23-2012, 01:46 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 1
- Rep Power
- 0
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:
Sorry for my crappy code, I'm still learning the basicsJava 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 }
- 12-23-2012, 03:23 PM #2
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
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!.gif)
- 12-27-2012, 05:49 AM #3
Member
- Join Date
- Dec 2012
- Posts
- 74
- Rep Power
- 0
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".
- 12-27-2012, 05:07 PM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Re: Nullpointerexception driving me mad :(
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
-
Methods driving me mad
By GrumpyBum in forum New To JavaReplies: 4Last Post: 04-22-2012, 05:09 AM -
Driving me crazy!!!! Please help!!
By Quizzle23 in forum New To JavaReplies: 33Last Post: 02-24-2011, 08:00 AM -
The null message is driving me crazy
By Yakg in forum New To JavaReplies: 5Last Post: 12-02-2010, 07:03 PM -
image does not refresh-driving me crazy
By jambon in forum AWT / SwingReplies: 1Last Post: 04-09-2010, 04:25 PM -
Loop driving me loopy!!!!!
By soc86 in forum New To JavaReplies: 8Last Post: 01-16-2009, 01:00 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks