473,226 Members | 1,358 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 software developers and data experts.

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 1795
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

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

Similar topics

10
by: John Brock | last post by:
I have a base class with several derived classes (I'm writing in VB.NET). I want each derived class to have a unique class ID (a String), and I want the derived classes to inherit from the base...
9
by: Michael Roper | last post by:
If a class uses public properties to expose private fields, and has a constructor to initialize those fields, should the constructor set them directly or use the set accessors? Or does it matter?...
10
by: linkspeed | last post by:
Following texts are from C# spec. The optional constructor-initializer specifies another instance constructor to invoke before executing the statements given in the constructor-body of this...
26
by: Paul | last post by:
public class A { public A () { // here I would like to call the second version of _ctor, how to accomplish this ? } public A (int a, int b, int c) {
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
5
by: Pete Davis | last post by:
I know I can use reflection to call internal, protected, and private methods in a class, but I need to instantiate an object that is public but has an internal constructor (The CurrencyManager...
12
by: Andrew Schepler | last post by:
When compiled with Visual C++ .NET 2003 (only), the program below aborts as though no matching catch clause is present. If the copy constructor of A is made public, it successfully catches the...
5
by: | last post by:
Hi, How long do webservice objects live for? In particular, if i have static variables filled with data from a static constructor in a webservice, how long will that data persist? thxs
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
9
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.