Connecting Tech Pros Worldwide Help | Site Map

Constructor issue

Newbie
 
Join Date: May 2009
Posts: 21
#1: Jun 22 '09
Hi,

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

Here is my sample code:-

Expand|Select|Wrap|Line Numbers
  1.   public MyConstructor(String filename, int value1, int value2){
  2.  
  3.            this.filename = filename;
  4.            this.value1 = value1;
  5.            this.value2 = value2;
  6.  
  7.            File reading code
  8.            BufferedReader fh = new BufferedReader(new FileReader(filename)); 
  9.               ................
  10.  
  11.                  }
  12.  
  13.               public static void main(String args[]) {
  14.                        String s1 = args[1];
  15.                        String s2 = args[2];
  16.  
  17.                        int value1 = Integer.parseInt(s1.trim());
  18.                        int value2 = Integer.parseInt(s2.trim());
  19.  
  20.          MyConstructor myobject = new MyConstructor(args[0], value1, value2);
  21.  
  22.                                                      }
Can anybody help me to fix this problem.

Thanks
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Jun 22 '09

re: Constructor issue


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.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#3: Jun 22 '09

re: Constructor issue


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

kind regarsds,

Jos
Newbie
 
Join Date: May 2009
Posts: 21
#4: Jun 22 '09

re: Constructor issue


If I create a method like
Expand|Select|Wrap|Line Numbers
  1.  public void ReadFile(String filename, int Value1, int value2) {
  2.     this.filename = filename;
  3.     this.value1 = value1;
  4.     this.value2 = value2;
  5. .........................
  6.     BufferedReader fh = new BufferedReader(new FileReader(filename));
  7.  ............................................  
  8.  }
  9.  
  10.  
Now if I want to call it in my previous example constructor like
Expand|Select|Wrap|Line Numbers
  1. public Constructor() {
  2.  
  3. ...........................
  4.  
  5.   //How do I call my method ?
  6.   //I am doing like this
  7.  
  8.    ReadFile(filename, range1, range2);
  9.  }
  10.  
  11.  
and then I will call this method in the main as:-
Expand|Select|Wrap|Line Numbers
  1. public static void main(String args[]) {
  2.  
  3.          String s1 = args[1];
  4.          String s2 = args[2];
  5.          String filename = args[0];
  6.  
  7.          int value1 = Integer.parseInt(s1.trim());
  8.          int value2 = Integer.parseInt(s2.trim());
  9.  
  10.        MyClass exmpl = new MyClass();
  11.        exmpl.ReadFile(filename, value1, value2) 
  12.       /// Will it work.. If not howto do then ?
  13. }
  14.  
  15.  
Thanks
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#5: Jun 22 '09

re: Constructor issue


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:

Expand|Select|Wrap|Line Numbers
  1. void foo() throws BarException {
  2.    ...
  3.    bar();
  4.    ...
  5. }
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
Newbie
 
Join Date: May 2009
Posts: 21
#6: Jun 22 '09

re: Constructor issue


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

Thanks
Newbie
 
Join Date: May 2009
Posts: 21
#7: Jun 22 '09

re: Constructor issue


Most important am I calling the method correctly ? in the constructor ?
Expand|Select|Wrap|Line Numbers
  1. ReadFile(filename, value11, value2);
Thanks
Newbie
 
Join Date: May 2009
Posts: 21
#8: Jun 22 '09

re: Constructor issue


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
Newbie
 
Join Date: May 2009
Posts: 21
#9: Jun 22 '09

re: Constructor issue


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 NullPointException at java.io.FileInputStream<init>. Like this one. Need help.

The basic structure of my code is like this:-

Expand|Select|Wrap|Line Numbers
  1. public class Class1 extends JFrame{
  2.  
  3.    public Class1(){this.somevalues
  4.   setDefaultCloseOperation(EXIT_ON_CLOSE);
  5.       setMinimumSize(new Dimension(1000, 200));
  6.       class2obj = new Class2();
  7.       getContentPane().add(class2obj, BorderLayout.CENTER);
  8.       pack();
  9.       }
  10.       }
  11.  
  12.    static class Class2  extends JPanel implements MouseMotionListener {
  13.  
  14.    public String filename;
  15.    public int value1;
  16.    public int value2;
  17.  
  18.   public Class2(){
  19.      super();
  20.      readFile(filename, value1, value2);  
  21.        }
  22.   public readFile(String filename, int value1, int value2){
  23.        this.filename = filename;
  24.        this.value1 = value1;
  25.        this.value2 = value2;
  26.        ............
  27.        try{
  28.             BufferedReader fh = new BufferedReader(new FileReader(filename)); 
  29. .............................
  30.   fh.close();
  31.         } catch (FileNotFoundException e) {
  32.             e.printStackTrace();
  33.         } catch (IOException e2) {
  34.             e2.printStackTrace();
  35.         }
  36. ...............
  37.  
  38. public static void main(String args[]) {
  39.          String s1 = args[1];
  40.          String s2 = args[2];
  41.          String filename = args[0];
  42.  
  43.          int value1 = Integer.parseInt(s1.trim());
  44.          int value2 = Integer.parseInt(s2.trim()); 
  45.  
  46.          Class2 obj = new Class2();
  47.          obj.readFile(filename, value1, value2);
  48.  
  49.       java.awt.EventQueue.invokeLater(new Runnable() {
  50.       public void run() {
  51.       new Class1().setVisible(true);
  52.       }
  53.       });
  54.       }
  55.  } 
  56.  
  57.  
  58.  
Thanks
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#10: Jun 23 '09

re: Constructor issue


Quote:

Originally Posted by crochunter View Post

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 NullPointException at java.io.FileInputStream<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
Reply