Results 1 to 20 of 21
Thread: problem with exceptions
- 09-10-2009, 05:42 PM #1
problem with exceptions
Hello to everybody. I have a project in which I want to put some variables to a txt file. In the code i have the class asthenhs where i have the variables i want to put into file also i have the write_new_record open_file read_file close_file and file_close. The problem is in write_new_record. I can normally enter in the output the variables but when i press enter finally i got this
Exception in thread "main" java.lang.NullPointerException
at ofthalmiatreio.Asthenhs.write_new_record(Asthenhs. java:223)
at ofthalmiatreio.Asthenhs.main(Asthenhs.java:335)
Java Result: 1
Have anyone idea whats this i tried days to find the problem, but nothing...
- 09-10-2009, 05:43 PM #2
ok the code is
Java Code:# import java.io.*; # import java.util.*; # import java.util.Formatter; # import java.io.FileWriter; # import java.io.File; # import java.io.FileNotFoundException; # import java.lang.IllegalStateException; # import java.io.IOException; # # /** # * # * @author krisimport java.util.Scanner; # */ # public class Asthenhs { //klasi asthenhs # # private String surname; # private String name; # private int phone; # private int ID; # private String insurance; # # FileWriter out; # Formatter out1; # BufferedWriter output; # Scanner input=new Scanner(System.in); # BufferedWriter outputt; # # public Asthenhs(Asthenhs ob){ //consrtructor me orisma object # # surname=ob.surname; # name=ob.name; # phone=ob.phone; # insurance=ob.insurance; # } # # public Asthenhs(){ # this.ID=0; # this.name="null"; # this.surname="null"; # this.phone=0; # this.insurance="null"; # } # public Asthenhs(String ep, String onom, int til, int id, String as){ # # this.surname=ep; # this.name=onom; # this.phone=til; # this.ID=id; # this.insurance=as; # } # public void setSurname(String ep){ //methodoi set kai gia tis private metavlites tis klasis # # this.surname=ep; # } # public String getSurname(){ # # return surname; # } # public void setName(String onom){ # # this.name=onom; # } # public String getName(){ # # return name; # } # public void setPhone(int til){ # # this.phone=til; # } # public int getPhone(){ # # return phone; # } # public void setId(int id){ # # this.ID=id; # } # public int getId(){ # # return ID; # } # public void setInsurance(String as){ # # this.insurance=as; # } # public String getInsurance (){ # # return insurance; # }
- 09-10-2009, 05:44 PM #3
the find method it use the integer key (for client's id) and return an object
Java Code:# public Object Find(int key) { # Asthenhs a = new Asthenhs(); # // Scanner input=new Scanner(System.in); # # try { # while (input.hasNext()) { //oso tha iparxei apothikeumeni eggrafi # String s = new Integer(input.nextInt()).toString(); # int log = Integer.parseInt(s.trim()); # a.setId(log); # a.setName(input.next()); # a.setSurname(input.next()); # String s2 = new Integer(input.nextInt()).toString(); # int til = Integer.parseInt(s2.trim()); # a.setPhone(til); # a.setInsurance(input.next()); # if (key == a.getId()) { //ean to kleidi isodinamei me ton trexonta logariasmo # return a; # } # } # } catch (NoSuchElementException e) { # System.err.println("Σφάλμα κατά την τροποποίηση του αρχείου"); # System.err.println(e.getMessage()); //emfanisi tou provlimatos # input.close(); # System.exit(0); # } catch (IllegalStateException e) { # System.err.println("Σφάλμα κατά την ανάγνωση από αρχείο"); # System.exit(0); # } # return a; # }
- 09-10-2009, 05:45 PM #4
method for opening file:
Java Code:# public void open_file(String filename) { //methodos open_file gia anoigma arxeiou txt me orisma to # //to onoma tou arxeiou # // FileWriter out; # try{ # File file = new File(filename); // Create file # out = new FileWriter(file.getPath(),true); # output = new BufferedWriter(out); # # if(file.createNewFile()){ # System.out.println("Ανοιγμα του αρχείου " + filename); //anoigma tou neou arxeiou me onoma filename pou dinetai san input stin methodo # } # else{ # System.out.println("Άνοιγμα του ήδη υπάρχοντος αρχείου " + file.getPath()); //partially qualified directory + name + extension # } # } # catch (FileNotFoundException e) { # System.err.println("Σφάλμα κατά τη δημιουργία του αρχείου"); //1o eidos sfalmatos kata tin dimiourgia arxeiou # System.exit(0); # } # catch (Exception e){//2o eidos sfalma kata to anoigma tou arxeiou # System.err.println("Error: " + e.getMessage()); # } # } # public void CloseFile(){ # # if(out1!=null){ # # out1.close(); # } # } # public void read_file(String filename){ # # try{ # // Scanner input=new Scanner(System.in); # input=new Scanner(new File(filename)); # } # catch (FileNotFoundException e){ # System.err.println("Error: " + e.getMessage()); # } # } # public void Close_Readfile(){ # if(input!=null){ # input.close(); # } # }
- 09-10-2009, 05:46 PM #5
You have to be pretty clear with your question.Please explain properly or share ur code.
As far as ur explanation is concerned the variable is not having anything other than null.Ramya:cool:
- 09-10-2009, 05:47 PM #6
the method write_new_record where is the problem. write_new_record i use it for writing objects into file firstly it try to find if in the file exist object with same account else write the object in file
Java Code:# public void write_new_record(String fileName){ # # Asthenhs a=new Asthenhs(); # Asthenhs b=new Asthenhs(); # # # # String namee,surnamee,insurancee; # int iidd,phonn; # # try{ # // while (input.hasNext()) { # a.read_file("Asthenhs.txt"); # //Prwta prepei na elexoume to account tou pelati gia na doume an idi uparxei # System.out.println("Give the client's ID"); # iidd=input.nextInt(); # b = (Asthenhs) a.Find(iidd); # if(b.getId()==iidd){ # System.out.println("This ID is for another patient.Try again."); # return; # } # System.out.println("Give the name of patient: "); # namee=input.next(); # System.out.println("Give the surname of the patient: "); # surnamee=input.next(); # System.out.println("Give the phone of the patient: "); # phonn=input.nextInt(); # System.out.println("Give the insurance of the patient: "); # insurancee=input.next(); # // System.out.println("Give the ID of the patient: "); # //1a.ID=input.nextInt(); # a.setName(namee); # a.setSurname(surnamee); # a.setPhone(phonn); # a.setInsurance(insurancee); # # int id = a.getId(); # int p = a.getPhone(); # String iID= String.valueOf(id); # String phonE= String.valueOf(p); # # outputt.write(iID); # outputt.write(" "); # outputt.write(phonE); # outputt.write(a.getName()); # outputt.write(" "); # outputt.write(a.getSurname()); # outputt.write(" "); # outputt.write(phonE); # outputt.write(" "); # outputt.write(a.getInsurance()); # outputt.write(" "); # # a.Close_Readfile(); # System.out.println("The information is stored."); # // } # //} # } # // catch(NoSuchElementException e){ # // System.err.println("..."); # // input.nextLine(); # // } # # catch (FormatterClosedException e) { # System.out.println("Error: " + e.getMessage()); # } # catch (IOException e) { //sfalma eisodou-eksodou se arxeio # System.err.println("Error: " + e.getMessage()); # } # }
- 09-10-2009, 05:52 PM #7
1. public static void main(String [] args){
2.
3. Asthenhs a= new Asthenhs(); //****methodos main gia dokimi tis klasis
4. a.open_file("Asthenhs.txt");
5. // a.read_file("Asthenhs2.txt");
6. a.write_new_record("Asthenhs.txt");
7. a.CloseFile();
8. }
of course the main method.
Ok my problem is that i want to put into txt file the variables name surname id ...Firstly i open txt with method open_file method. With write_new_record the user first looks if the account of patient is already exist in file. Else it put all the variables in the file. I think that the problem is some null variable or something with the exceptions.
- 09-10-2009, 05:59 PM #8
Your code is not properly framed.
U have to put comments properly.
One small suggestion.Try to print these values firrst for debugging.then u try to write
iidd=input.nextInt();
# b = (Asthenhs) a.Find(iidd);
# if(b.getId()==iidd){
# System.out.println("This ID is for another patient.Try again.");
# return;
# }
# System.out.println("Give the name of patient: ");
# namee=input.next();
# System.out.println("Give the surname of the patient: ");
# surnamee=input.next();
# System.out.println("Give the phone of the patient: ");
# phonn=input.nextInt();
# System.out.println("Give the insurance of the patient: ");
# insurancee=input.next();
# // System.out.println("Give the ID of the patient: ");
# //1a.ID=input.nextInt();Ramya:cool:
- 09-10-2009, 06:05 PM #9
Actually i have tried System.out.println(idd+namee+surnamee+phonn+insura ncee); in ouput it show me the result of the variables but after this
Exception in thread "main" java.lang.NullPointerException
at ofthalmiatreio.Asthenhs.write_new_record(Asthenhs. java:223)
at ofthalmiatreio.Asthenhs.main(Asthenhs.java:335)
Java Result: 1
- 09-10-2009, 06:25 PM #10
ok i find something. In file close method i used Filewriter out1; instead of Formatter out1 with this change
public void CloseFile(){
if(out1!=null){
it works but none variable stored in the txt
out1.close();
}
}
- 09-10-2009, 06:27 PM #11
My doubt is u are trying 2 print a different thing other than what u are writing.
Example
System.out.println(idd+namee+surnamee+phonn+insura ncee);
Just put the below s.o.p
outputt.write(iID);
# outputt.write(" ");
System.out.println(phoneE)# outputt.write(phonE); // u above phonn here phoneE
System.out.println(a.getName())
# outputt.write(a.getName());
# outputt.write(" ");
System.out.println(a.getSurName())
# outputt.write(a.getSurname());
# outputt.write(" ");
System.out.println(phonE)
# outputt.write(phonE);
# outputt.write(" ");
System.out.println(a.getInsurance())
# outputt.write(a.getInsurance());
# outputt.write(" ");Ramya:cool:
- 09-10-2009, 07:23 PM #12
ok have a look at this:
Java Code:init: deps-jar: compile-single: run-single: Άνοιγμα του ήδη υπάρχοντος αρχείου Asthenhs.txt Give the client's ID 1 Give the name of patient: kris Give the surname of the patient: athanasiadis Give the phone of the patient: 47508 Give the insurance of the patient: ika kris athanasiadis 47508 ika The information is stored. BUILD SUCCESSFUL (total time: 11 seconds)
- 09-10-2009, 07:32 PM #13
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 13
i may have missed something, but you seem to have two BufferedReader objects, output and outputt... you instantiate output but not outputt, and you call outputt.write() but not output.write()
- 09-10-2009, 08:06 PM #14
ok I see what you mean but already i have change it(if you mean in open_file method)
Java Code:public void OpenFile(String fileName) { //anoigma arxeiou gia eggrafi try { File name = new File(fileName); out = new FileWriter(name.getPath(), true); out1 = new BufferedWriter(out); if (name.createNewFile()) { //ean den iparxei idi to arxeia System.out.println("Ανοιγμα του αρχείου " + fileName); //ektipwnetai analogo minima } else { System.out.println("Άνοιγμα του ήδη υπάρχοντος αρχείου " + name.getPath()); } } catch (FileNotFoundException e) { System.err.println("Σφάλμα κατά τη δημιουργία του αρχείου"); //sfalma kata ti dimiourgia tou arxeiou System.exit(0); } catch (IOException e) { System.err.println("Σφάλμα κατά το άνοιγμα αρχείου"); //sfalma kata to anoigma tou arxeiou } }
- 09-11-2009, 01:08 AM #15
ok certainly my problem is in fileWrite or bufferWriter . Probably in the second but i thing i vee changed it and i instantiate outputt --> outputt = new BufferedWriter(out);. But again the same it run normally but none variable is been writing into the file.
- 09-11-2009, 10:57 AM #16
Hi,
Please attach the code .We need to see.You are declaring one and using one.lot of confusions.
"We can't give solutions with assumptions.Higly impossible"
-Regards
RamyaRamya:cool:
- 09-11-2009, 01:09 PM #17
ok here is the finally code
Java Code:package ofthalmiatreio; /* import java.util.*; import java.io.File; import java.io.BufferedWriter; import java.io.IOException; import java.io.FileWriter; import java.io.FileNotFoundException; import java.lang.IllegalStateException; */ import java.io.*; import java.util.*; import java.util.Formatter; import java.io.FileWriter; import java.io.File; import java.io.FileNotFoundException; import java.lang.IllegalStateException; import java.io.IOException; /** * * @author krisimport java.util.Scanner; */ public class Asthenhs { //klasi asthenhs private String surname; private String name; private int phone; private int ID; private String insurance; Formatter out2; FileWriter out; Scanner input=new Scanner(System.in); BufferedWriter outputt; public Asthenhs(Asthenhs ob){ //consrtructor me orisma object surname=ob.surname; name=ob.name; phone=ob.phone; insurance=ob.insurance; } public Asthenhs(){ this.ID=0; this.name="nik"; this.surname="kalathis"; this.phone=0; this.insurance="ika"; } public Asthenhs(String ep, String onom, int til, int id, String as){ this.surname=ep; this.name=onom; this.phone=til; this.ID=id; this.insurance=as; } public void setSurname(String ep){ //methods set for the private metavlites tis klasis this.surname=ep; } public String getSurname(){ return surname; } public void setName(String onom){ this.name=onom; } public String getName(){ return name; } public void setPhone(int til){ this.phone=til; } public int getPhone(){ return phone; } public void setId(int id){ this.ID=id; } public int getId(){ return ID; } public void setInsurance(String as){ this.insurance=as; } public String getInsurance (){ return insurance; } public Object Find(int key) { Asthenhs a = new Asthenhs(); // Scanner input=new Scanner(System.in); try { while (input.hasNext()) { //while there is exist stored variable String s = new Integer(input.nextInt()).toString(); int log = Integer.parseInt(s.trim()); a.setId(log); a.setName(input.next()); a.setSurname(input.next()); String s2 = new Integer(input.nextInt()).toString(); int til = Integer.parseInt(s2.trim()); a.setPhone(til); a.setInsurance(input.next()); if (key == a.getId()) { //ean to kleidi isodinamei me ton trexonta logariasmo return a; } } } catch (NoSuchElementException e) { System.err.println("Σφάλμα κατά την τροποποίηση του αρχείου"); System.err.println(e.getMessage()); input.close(); System.exit(0); } catch (IllegalStateException e) { System.err.println("Σφάλμα κατά την ανάγνωση από αρχείο"); System.exit(0); } return a; } public void open_file(String filename) { //method open_file for opening //txt file with the filename // FileWriter out; try{ File file = new File(filename); // Create file out = new FileWriter(file.getPath(),true); outputt = new BufferedWriter(out); if(file.createNewFile()){ System.out.println("Ανοιγμα του αρχείου " + filename); } else{ System.out.println("Άνοιγμα του ήδη υπάρχοντος αρχείου " + file.getPath()); //partially qualified directory + name + extension } } catch (FileNotFoundException e) { System.err.println("Σφάλμα κατά τη δημιουργία του αρχείου"); System.exit(0); } catch (Exception e){//2o eidos sfalma kata to anoigma tou arxeiou System.err.println("Error: " + e.getMessage()); } } public void CloseFile(){ if(out2!=null){ out2.close(); } } public void read_file(String filename){ try{ // Scanner input=new Scanner(System.in); input=new Scanner(new File(filename)); } catch (FileNotFoundException e){ System.err.println("Error: " + e.getMessage()); } } public void Close_Readfile(){ if(input!=null){ input.close(); } } public void write_new_record(String fileName){ Asthenhs a=new Asthenhs(); Asthenhs b=new Asthenhs(); String namee,surnamee,insurancee; int idd,phonn; try{ // while (input.hasNext()) { a.read_file("Asthenhs.txt"); //firsly we must check clients id to see if already exist System.out.println("Give the client's ID"); idd=input.nextInt(); b = (Asthenhs) a.Find(idd); if(b.getId()==idd){ System.out.println("This ID is for another patient.Try again."); return; } else{ System.out.println("Give the name of patient: "); namee=input.next(); System.out.println("Give the surname of the patient: "); surnamee=input.next(); System.out.println("Give the phone of the patient: "); phonn=input.nextInt(); System.out.println("Give the insurance of the patient: "); insurancee=input.next(); //System.out.println("Give the ID of the patient: "); //idd=input.nextInt(); a.setId(idd); a.setName(namee); a.setSurname(surnamee); a.setPhone(phonn); a.setInsurance(insurancee); // //System.out.println(idd+namee+surnamee+phonn+insurancee); int id = a.getId(); int p = a.getPhone(); String iID= String.valueOf(id); String phonE= String.valueOf(p); //iID != null, phonE != null,namee!=null,insurancee!=null); outputt.write(iID); outputt.write(" "); System.out.println(a.getName()); outputt.write(a.getName()); outputt.write(" "); System.out.println(a.getSurname()); outputt.write(a.getSurname()); outputt.write(" "); System.out.println(phonE); outputt.write(phonE); outputt.write(" "); System.out.println(a.getInsurance()); outputt.write(a.getInsurance()); outputt.write(" "); a.Close_Readfile(); System.out.println("The information is stored."); // } } } catch(NoSuchElementException e){ System.out.println("Dn vrethike"); // System.err.println("..."); input.nextLine(); } catch (FormatterClosedException e) { System.out.println("Dn vrethike"); // System.err.println("Error: " + e.getMessage()); } catch (IOException e) { System.out.println("Dn vrethike"); // System.err.println("Error: " + e.getMessage()); } public static void main(String [] args){ Asthenhs a= new Asthenhs(); a.open_file("Asthenhs.txt"); // a.read_file("Asthenhs2.txt"); a.write_new_record("Asthenhs.txt"); a.CloseFile(); } }
- 09-11-2009, 02:43 PM #18
How the data got structured in Asthenhs.txt.send the Asthenhs.txt also.
Lot of errors and code has to be streamlined properly.No comments ...nothingRamya:cool:
- 09-11-2009, 02:52 PM #19
the problem is that txt is empty.
- 09-11-2009, 03:20 PM #20
Similar Threads
-
Exceptions
By hedonist in forum New To JavaReplies: 10Last Post: 09-08-2009, 09:38 AM -
Need Help With Exceptions
By maggie_2 in forum New To JavaReplies: 5Last Post: 12-15-2008, 08:12 PM -
Better way to handle exceptions
By javaplus in forum Advanced JavaReplies: 2Last Post: 01-16-2008, 07:47 PM -
Project of Exceptions
By Albert in forum Advanced JavaReplies: 1Last Post: 07-06-2007, 04:09 PM
Bookmarks