Results 1 to 6 of 6
Thread: Reading text from .txt file
- 01-23-2012, 10:15 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 4
- Rep Power
- 0
Reading text from .txt file
Hi guys. I created this account because I use this forum a lot to get some answers when i'm stuck with Java. Right now, i'm stuck again, and i couldn't really find the answer. I saw some posts about this, but it didn't contain an answer I was satisfied with. So here's my first post, and i'm hoping you guys can help me out.
Were making this program that is required to read a .txt file, some kind of csv. It's seperated by tabs. The program is supposed to read a line untill it sees a TAB, and then add that to part to a list, continue on to the second part of the line, add that to the list, and continue to the third and last part of the line, and add that to the list. Then it has to read the next line. I used an example from this forum, and got up with this:
The problem is, that it crashes when I try to execute this statement: "stamverzameling.addStambestand(stambestand);"Java Code:public StamVerzameling importStambestand(){ StamVerzameling lijst = new StamVerzameling(); Stambestand stambestand; String filenaam = "C:\\Temp\\Stampcode.txt"; try { FileReader file = new FileReader(filenaam); BufferedReader buff = new BufferedReader(file); boolean eof = false; int i=0; while(!eof){ String line = buff.readLine(); if (line == null){ eof = true; } else{ String parts[] = line.split("\t"); String stamnr = parts[0]; String stamom = parts[1]; String stambed = parts[2]; System.out.println(parts[0]); System.out.println(parts[1]); System.out.println(parts[2]); BigDecimal stambeddb = new BigDecimal(stambed); int stamnrint = Integer.parseInt(stamnr); stambestand = new Stambestand(stamnrint, stamom, stambeddb); stamverzameling.addStambestand(stambestand); } } file.close(); } //catch (ClassNotFoundException e) { // System.out.println("Onbekende klasse bij inlezen bestand" + filenaam); //} catch (IOException e) { System.out.println("Fout bij het openen bestand" + filenaam); e.printStackTrace(); } return lijst; }
It gives back a nullpointer exception. It does however, read the first line, and the System.out also works.
The Stamverzameling class looks like this
And finally, the file I try to load looks like thisJava Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package Entity; import java.io.Serializable; import java.util.ArrayList; /** * * @author Wesley */ public class StamVerzameling implements Serializable { private ArrayList<Stambestand> stamverzameling; public StamVerzameling() { stamverzameling = new ArrayList(); } public void addStambestand(Stambestand stambestand) { stamverzameling.add(stambestand); } /** * * @param index */ public void removeStambestand(int index) { stamverzameling.remove(index); } /** * * @return */ public ArrayList<Stambestand> getStambestand() { return stamverzameling; } /** * * @param verzekeringen */ public void setStambestand(ArrayList<Stambestand> stamverzameling) { this.stamverzameling = stamverzameling; } /** * */ public void clearStambestand() { stamverzameling.clear(); } }
1000 Zitting fysiotherapie 27.00
1001 Zitting fysiotherapie inclusief toeslag uitbehandeling 39.70
1002 Zitting fysiotherapie inclusief inrichtingstoeslag 33.35
1100 Zitting kinderfysiotherapie 39.00
1101 Zitting kinderfysiotherapie inclusief toeslag uitbehandeling 51.70
1102 Zitting kinderfysiotherapie inclusief inrichtingstoeslag 45.35
1103 Instructie/overleg ouders van de patiënt 27.00
1105 Eenmalig kinderfysiotherapeutisch rapport 27.00
1200 Zitting manuele therapie 39.00
1201 Zitting manuele therapie inclusief toeslag uitbehandeling 51.70
1202 Zitting manuele inclusief inrichtingstoeslag 45.35
1301 Groepszitting specifieke behandeling fysiotherapie van 2 personen 7.00
1302 Groepszitting specifieke behandeling fysiotherapie van 3 personen 5.25
1303 Groepszitting specifieke behandeling fysiotherapie van 4 personen 4.25
1304 Groepszitting specifieke behandeling fysiotherapie van 5 tot en met 10 personen 3.75
1305 Groepszitting specifieke behandeling fysiotherapie van meer dan 10 personen 3.00
1400 Eenmalig fysiotherapeutisch onderzoek 54.00
1401 Eenmalig fysiotherapeutisch onderzoek inclusief toeslag uitbehandeling 66.70
1402 Eenmalig fysiotherapeutisch onderzoek inclusief inrichtingstoeslag 60.35
1500 Zitting oedeemtherapie 39.00
1501 Zitting oedeemtherapie inclusief toeslag uitbehandeling 51.70
1502 Zitting oedeemtherapie inclusief inrichtingstoeslag 45.35
1600 Zitting bekkentherapie 39.00
1601 Zitting bekkentherapie inclusief toeslag uitbehandeling 51.70
1602 Zitting bekkentherapie inclusief inrichtingstoeslag 45.35
1700 Lange zitting voor patiënt met complexe zorgvraag 39.00
1701 Lange zitting voor patiënt met complexe zorgvraag inclusief toeslag uitbehandeling 51.70
1702 Lange zitting voor patiënt met complexe zorgvraag inclusief inrichtingstoeslag 45.35
1850 Screening 9.00
1851 Screening inclusief toeslag uitbehandeling 21.70
1852 Screening inclusief inrichtingstoeslag 15.35
1860 Intake en onderzoek na screening 27.00
1861 Intake en onderzoek na screening inclusief toeslag uitbehandeling 39.70
1862 Intake en onderzoek na screening inclusief inrichtingstoeslag 33.35
1870 Intake en onderzoek na verwijzing 36.00
1871 Intake en onderzoek na verwijzing inclusief toeslag uitbehandeling 48.70
1872 Intake en onderzoek na verwijzing inclusief inrichtingstoeslag 42.35
1900 Rapportage 135.00
0404 Niet nagekomen afspraak 35.00
I'm out of idea's and hope you guys can help me out
Thanks!
- 01-23-2012, 11:01 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Reading text from .txt file
Where is "stamverzameling" declared? looks like it is null! You created a StamVerzameling object in line two with the name lijst, but thats another object or what? ....
- 01-23-2012, 11:14 PM #3
Member
- Join Date
- Jul 2010
- Posts
- 4
- Rep Power
- 0
Re: Reading text from .txt file
I'm sorry. I do have that declared. It's on the top of the page. I copied only the method. Here's the rest of the FileIO.java
Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package Control; import Entity.BehandelingVerzameling; import Entity.FactuurVerzameling; import Entity.KlantVerzameling; import Entity.MaatschappijVerzameling; import Entity.VerzekeringVerzameling; import Entity.StamVerzameling; import Entity.Stambestand; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.math.BigDecimal; import java.util.ArrayList; /** * * @author Timmeke */ public class FileIO { private StamVerzameling stamverzameling; /** * */ public FileIO(){ } /** * * @param klanten */ public ArrayList StamArray = new ArrayList(); //Test voor stambestand public void exportKlanten(KlantVerzameling klanten){ String filenaam = "C:\\Temp\\klanten.txt"; try { ObjectOutputStream uit = new ObjectOutputStream( new FileOutputStream(filenaam)); uit.writeObject(klanten); uit.close(); } catch (IOException e) { System.out.println("Fout bij het openen bestand " + filenaam); e.printStackTrace(); } } /** * * @return */ public KlantVerzameling importKlanten(){ KlantVerzameling lijst = new KlantVerzameling(); String filenaam = "C:\\Temp\\klanten.txt"; try { ObjectInputStream in = new ObjectInputStream(new FileInputStream(filenaam)); lijst = (KlantVerzameling) in.readObject(); in.close(); } catch (ClassNotFoundException e) { System.out.println("Onbekende klasse bij inlezen bestan" + filenaam); } catch (IOException e) { System.out.println("Fout bij het openen bestand" + filenaam); e.printStackTrace(); } return lijst; } /** * * @param maatschappijen */ public void exportMaatschappijen(MaatschappijVerzameling maatschappijen){ String filenaam = "C:\\Temp\\maatschappijen.txt"; try { ObjectOutputStream uit = new ObjectOutputStream( new FileOutputStream(filenaam)); uit.writeObject(maatschappijen); uit.close(); } catch (IOException e) { System.out.println("Fout bij het openen bestand " + filenaam); e.printStackTrace(); } } /** * * @return */ public MaatschappijVerzameling importMaatschappijen(){ MaatschappijVerzameling lijst = new MaatschappijVerzameling(); String filenaam = "C:\\Temp\\maatschappijen.txt"; try { ObjectInputStream in = new ObjectInputStream(new FileInputStream(filenaam)); lijst = (MaatschappijVerzameling) in.readObject(); in.close(); } catch (ClassNotFoundException e) { System.out.println("Onbekende klasse bij inlezen bestan" + filenaam); } catch (IOException e) { System.out.println("Fout bij het openen bestand" + filenaam); e.printStackTrace(); } return lijst; } /** * * @param verzekeringen */ public void exportVerzekeringen(VerzekeringVerzameling verzekeringen){ String filenaam = "C:\\Temp\\verzekeringen.txt"; try { ObjectOutputStream uit = new ObjectOutputStream( new FileOutputStream(filenaam)); uit.writeObject(verzekeringen); uit.close(); } catch (IOException e) { System.out.println("Fout bij het openen bestand " + filenaam); e.printStackTrace(); } } /** * * @return */ public VerzekeringVerzameling importVerzekeringen(){ VerzekeringVerzameling lijst = new VerzekeringVerzameling(); String filenaam = "C:\\Temp\\verzekeringen.txt"; try { ObjectInputStream in = new ObjectInputStream(new FileInputStream(filenaam)); lijst = (VerzekeringVerzameling) in.readObject(); in.close(); } catch (ClassNotFoundException e) { System.out.println("Onbekende klasse bij inlezen bestan" + filenaam); } catch (IOException e) { System.out.println("Fout bij het openen bestand" + filenaam); e.printStackTrace(); } return lijst; } /** * * @return */ public FactuurVerzameling importFacturen(){ FactuurVerzameling lijst = null; String filenaam = "C:\\Temp\\facturen.txt"; try { ObjectInputStream in = new ObjectInputStream(new FileInputStream(filenaam)); lijst = (FactuurVerzameling) in.readObject(); in.close(); } catch (ClassNotFoundException e) { System.out.println("Onbekende klasse bij inlezen bestand" + filenaam); } catch (IOException e) { System.out.println("Fout bij het openen bestand" + filenaam); e.printStackTrace(); } return lijst; } /** * * @param facturen */ public void exportFacturen(FactuurVerzameling facturen){ String filenaam = "C:\\Temp\\facturen.txt"; try { ObjectOutputStream uit = new ObjectOutputStream( new FileOutputStream(filenaam)); uit.writeObject(facturen); uit.close(); } catch (IOException e) { System.out.println("Fout bij het openen bestand " + filenaam); e.printStackTrace(); } } /** * * @param behandelingen */ public void exportBehandelingen(BehandelingVerzameling behandelingen){ String filenaam = "C:\\Temp\\behandelingen.txt"; try { ObjectOutputStream uit = new ObjectOutputStream( new FileOutputStream(filenaam)); uit.writeObject(behandelingen); uit.close(); } catch (IOException e) { System.out.println("Fout bij het openen bestand " + filenaam); e.printStackTrace(); } } /** * * @return */ public BehandelingVerzameling importBehandelingen(){ BehandelingVerzameling lijst = new BehandelingVerzameling(); String filenaam = "C:\\Temp\\behandelingen.txt"; try { ObjectInputStream in = new ObjectInputStream(new FileInputStream(filenaam)); lijst = (BehandelingVerzameling) in.readObject(); in.close(); } catch (ClassNotFoundException e) { System.out.println("Onbekende klasse bij inlezen bestan" + filenaam); } catch (IOException e) { System.out.println("Fout bij het openen bestand" + filenaam); e.printStackTrace(); } return lijst; } /** * * @param facturen */ public void ExportClieop(int sofinummer){ FacturatieController Fcontrol = new FacturatieController(); String filenaam = "C:\\Temp\\facturen.clieop"; try { ObjectOutputStream uit = new ObjectOutputStream(new FileOutputStream(filenaam)); uit.writeObject(Fcontrol.getFactuur(sofinummer)); uit.close(); } catch(IOException e){ System.out.println(""); } } public StamVerzameling importStambestand(){ StamVerzameling lijst = new StamVerzameling(); Stambestand stambestand; String filenaam = "C:\\Temp\\Stampcode.txt"; try { FileReader file = new FileReader(filenaam); BufferedReader buff = new BufferedReader(file); boolean eof = false; int i=0; while(!eof){ String line = buff.readLine(); if (line == null){ eof = true; } else{ String parts[] = line.split("\t"); String stamnr = parts[0]; String stamom = parts[1]; String stambed = parts[2]; System.out.println(parts[0]); System.out.println(parts[1]); System.out.println(parts[2]); BigDecimal stambeddb = new BigDecimal(stambed); int stamnrint = Integer.parseInt(stamnr); stambestand = new Stambestand(stamnrint, stamom, stambeddb); stamverzameling.addStambestand(stambestand); } } file.close(); } //catch (ClassNotFoundException e) { // System.out.println("Onbekende klasse bij inlezen bestand" + filenaam); //} catch (IOException e) { System.out.println("Fout bij het openen bestand" + filenaam); e.printStackTrace(); } return lijst; } }Last edited by Colossus; 01-23-2012 at 11:18 PM.
- 01-23-2012, 11:17 PM #4
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Reading text from .txt file
Yes its declared, but not initialized :D
line 30: private ArrayList<Stambestand> stamverzameling;
You never create an ArrayList ....
->
private List<Stambestand> stamverzameling = new ArrayList<Stambestand>();
- 01-23-2012, 11:24 PM #5
Member
- Join Date
- Jul 2010
- Posts
- 4
- Rep Power
- 0
Re: Reading text from .txt file
Hmm, thanks for your quick answer, but it's not working.
when i type this:it gives me the error: stamverzameling is already defined in Control.FileIOJava Code:private ArrayList<Stambestand> stamverzameling = new ArrayList<Stambestand>();
- 01-24-2012, 05:09 PM #6
Member
- Join Date
- Jul 2010
- Posts
- 4
- Rep Power
- 0
Re: Reading text from .txt file
He guys. We found out what the error was. We were creating 2 instances of 'StamVerzameling'. 1 as 'lijst' and 1 as 'stamverzameling'. when we then tried to
it didn't workJava Code:stamverzameling.addStambestand
So we made it:
and now its working fine! So thanks all :)Java Code:lijst.addStambestand
Similar Threads
-
Reading from a text file, then writing back to Text Area in Reverse
By medic642 in forum New To JavaReplies: 8Last Post: 07-17-2011, 02:38 PM -
Reading in a text file
By TheRealHoff in forum AWT / SwingReplies: 10Last Post: 02-07-2010, 11:47 PM -
reading text file
By trofyscarz in forum New To JavaReplies: 1Last Post: 02-05-2010, 02:24 AM -
Reading a text file
By diegosened in forum New To JavaReplies: 4Last Post: 01-15-2010, 11:32 PM -
Reading text file
By Lennon-Guru in forum New To JavaReplies: 1Last Post: 12-15-2007, 11:38 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks