468,505 Members | 1,561 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,505 developers. It's quick & easy.

Constructor issue

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
Jun 22 '09 #1
9 1474
r035198x
13,262 8TB
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.
Jun 22 '09 #2
JosAH
11,448 Expert 8TB
Some Exceptions can be thrown from your constructor: either handle them or report that your constructor can (indirectly) throw those Exceptions.

kind regarsds,

Jos
Jun 22 '09 #3
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
Jun 22 '09 #4
JosAH
11,448 Expert 8TB
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
Jun 22 '09 #5
so using "throws BarException" will solve my problem ? Let me try

Thanks
Jun 22 '09 #6
Most important am I calling the method correctly ? in the constructor ?
Expand|Select|Wrap|Line Numbers
  1. ReadFile(filename, value11, value2);
Thanks
Jun 22 '09 #7
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
Jun 22 '09 #8
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
Jun 22 '09 #9
JosAH
11,448 Expert 8TB
@crochunter
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
Jun 23 '09 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by Michael Roper | last post: by
10 posts views Thread by linkspeed | last post: by
45 posts views Thread by Ben Blank | last post: by
5 posts views Thread by Pete Davis | last post: by
74 posts views Thread by Zytan | last post: by
9 posts views Thread by Morten Lemvigh | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.