Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    Join Date
    Jan 2009
    Posts
    56
    Rep Power
    0

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

  2. #2
    Join Date
    Jan 2009
    Posts
    56
    Rep Power
    0

    Default

    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;  
    #     }

  3. #3
    Join Date
    Jan 2009
    Posts
    56
    Rep Power
    0

    Default

    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;  
    #     }

  4. #4
    Join Date
    Jan 2009
    Posts
    56
    Rep Power
    0

    Default

    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();  
    #      }  
    #     }

  5. #5
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    812
    Rep Power
    6

    Default

    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:

  6. #6
    Join Date
    Jan 2009
    Posts
    56
    Rep Power
    0

    Default

    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());  
    #         }  
    #     }

  7. #7
    Join Date
    Jan 2009
    Posts
    56
    Rep Power
    0

    Default

    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.

  8. #8
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    812
    Rep Power
    6

    Default

    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:

  9. #9
    Join Date
    Jan 2009
    Posts
    56
    Rep Power
    0

    Default

    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

  10. #10
    Join Date
    Jan 2009
    Posts
    56
    Rep Power
    0

    Default

    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();
    }
    }

  11. #11
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    812
    Rep Power
    6

    Default

    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:

  12. #12
    Join Date
    Jan 2009
    Posts
    56
    Rep Power
    0

    Default

    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)
    this is the output, the problem is that none of this variables stored in file.

  13. #13
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    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()

  14. #14
    Join Date
    Jan 2009
    Posts
    56
    Rep Power
    0

    Default

    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
            }
        }
    and i supposed you mean bufferedWriter!

  15. #15
    Join Date
    Jan 2009
    Posts
    56
    Rep Power
    0

    Default

    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.

  16. #16
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    812
    Rep Power
    6

    Default

    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
    Ramya
    Ramya:cool:

  17. #17
    Join Date
    Jan 2009
    Posts
    56
    Rep Power
    0

    Default

    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();
            
        }
      }

  18. #18
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    812
    Rep Power
    6

    Default

    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 ...nothing
    Ramya:cool:

  19. #19
    Join Date
    Jan 2009
    Posts
    56
    Rep Power
    0

    Default

    the problem is that txt is empty.

  20. #20
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    812
    Rep Power
    6

    Default

    first checjk the file is empty then do manipulations.
    Ramya:cool:

Page 1 of 2 12 LastLast

Similar Threads

  1. Exceptions
    By hedonist in forum New To Java
    Replies: 10
    Last Post: 09-08-2009, 08:38 AM
  2. Need Help With Exceptions
    By maggie_2 in forum New To Java
    Replies: 5
    Last Post: 12-15-2008, 07:12 PM
  3. Better way to handle exceptions
    By javaplus in forum Advanced Java
    Replies: 2
    Last Post: 01-16-2008, 06:47 PM
  4. Project of Exceptions
    By Albert in forum Advanced Java
    Replies: 1
    Last Post: 07-06-2007, 03:09 PM

Tags for this Thread

Posting Permissions

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