Constructor issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • crochunter
    New Member
    • May 2009
    • 21

    Constructor issue

    Hi,

    I have fixed some parameters to my constructor but unable to make them work.

    Here is my sample code:-

    Code:
      public MyConstructor(String filename, int value1, int value2){
    
               this.filename = filename;
               this.value1 = value1;
               this.value2 = value2;
    
               File reading code
               BufferedReader fh = new BufferedReader(new FileReader(filename)); 
                  ................
    
                     }
    
                  public static void main(String args[]) {
                           String s1 = args[1];
                           String s2 = args[2];
    
                           int value1 = Integer.parseInt(s1.trim());
                           int value2 = Integer.parseInt(s2.trim());
    
             MyConstructor myobject = new MyConstructor(args[0], value1, value2);
    
                                                         }
    Can anybody help me to fix this problem.

    Thanks
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    You haven't described what the exact problem is.
    Are you getting a compiler error? If so then post the error message you have?
    If it's an exception you are getting then post the exception message and line numbers reported.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Some Exceptions can be thrown from your constructor: either handle them or report that your constructor can (indirectly) throw those Exceptions.

      kind regarsds,

      Jos

      Comment

      • crochunter
        New Member
        • May 2009
        • 21

        #4
        Will this work ?

        If I create a method like
        Code:
         public void ReadFile(String filename, int Value1, int value2) {
            this.filename = filename;
            this.value1 = value1;
            this.value2 = value2;
        .........................
            BufferedReader fh = new BufferedReader(new FileReader(filename));
         ............................................  
         }
        Now if I want to call it in my previous example constructor like
        Code:
        public Constructor() {
        
        ...........................
        
          //How do I call my method ?
          //I am doing like this
        
           ReadFile(filename, range1, range2);
         }
        and then I will call this method in the main as:-
        Code:
        public static void main(String args[]) {
             
                 String s1 = args[1];
                 String s2 = args[2];
                 String filename = args[0];
        
                 int value1 = Integer.parseInt(s1.trim());
                 int value2 = Integer.parseInt(s2.trim());
          
               MyClass exmpl = new MyClass();
               exmpl.ReadFile(filename, value1, value2) 
              /// Will it work.. If not howto do then ?
        }
        Thanks

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          If a method foo() calls another method bar() and that other method can throw an Exception then either your method foo() has to handle that Exception or it must show that it lets it pass by putting it in its header like this:

          Code:
          void foo() throws BarException {
             ...
             bar();
             ...
          }
          The same goes for constructors that call other methods that can throw Exceptions; the compiler checks for that. Always read the API documentation for every method/constructor you want to use and see if it can throw an Exception.

          kind regards,

          Jos

          Comment

          • crochunter
            New Member
            • May 2009
            • 21

            #6
            Let me try

            so using "throws BarException" will solve my problem ? Let me try

            Thanks

            Comment

            • crochunter
              New Member
              • May 2009
              • 21

              #7
              Most important am I calling the method correctly ? in the constructor ?
              Code:
              ReadFile(filename, value11, value2);
              Thanks

              Comment

              • crochunter
                New Member
                • May 2009
                • 21

                #8
                I am a bit confused about howto call a method inside a constructor and how do I handle exception in this case using this example reading file. I am already using try catch inside my ReadFile method.

                Thanks

                Comment

                • crochunter
                  New Member
                  • May 2009
                  • 21

                  #9
                  I am actually using a class inside a class. Can anybody point out my mistake. My code is compiling OK but when I run it. It gives me NullPointExcept ion at java.io.FileInp utStream<init>. Like this one. Need help.

                  The basic structure of my code is like this:-

                  Code:
                  public class Class1 extends JFrame{
                  
                     public Class1(){this.somevalues
                    setDefaultCloseOperation(EXIT_ON_CLOSE);
                        setMinimumSize(new Dimension(1000, 200));
                        class2obj = new Class2();
                        getContentPane().add(class2obj, BorderLayout.CENTER);
                        pack();
                        }
                        }
                  
                     static class Class2  extends JPanel implements MouseMotionListener {
                     
                     public String filename;
                     public int value1;
                     public int value2;
                  
                    public Class2(){
                       super();
                       readFile(filename, value1, value2);  
                         }
                    public readFile(String filename, int value1, int value2){
                         this.filename = filename;
                         this.value1 = value1;
                         this.value2 = value2;
                         ............
                         try{
                              BufferedReader fh = new BufferedReader(new FileReader(filename)); 
                  .............................
                    fh.close();
                          } catch (FileNotFoundException e) {
                              e.printStackTrace();
                          } catch (IOException e2) {
                              e2.printStackTrace();
                          }
                  ...............
                  
                  public static void main(String args[]) {
                           String s1 = args[1];
                           String s2 = args[2];
                           String filename = args[0];
                           
                           int value1 = Integer.parseInt(s1.trim());
                           int value2 = Integer.parseInt(s2.trim()); 
                            
                           Class2 obj = new Class2();
                           obj.readFile(filename, value1, value2);
                           
                        java.awt.EventQueue.invokeLater(new Runnable() {
                        public void run() {
                        new Class1().setVisible(true);
                        }
                        });
                        }
                   }
                  Thanks

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by crochunter
                    I am actually using a class inside a class. Can anybody point out my mistake. My code is compiling OK but when I run it. It gives me NullPointExcept ion at java.io.FileInp utStream<init>. Like this one. Need help.
                    A stack trace printout mayhap? You want us to plow through all that (strangely structured) code and you don't give us any relevant information?

                    kind regards,

                    Jos

                    Comment

                    Working...