Constructor issue | Newbie | | Join Date: May 2009
Posts: 21
| |
Hi,
I have fixed some parameters to my constructor but unable to make them work.
Here is my sample 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
| | Lives Here | | Join Date: Sep 2006
Posts: 12,070
| | | 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.
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | 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
| | | re: Constructor issue
If I create a method like -
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 -
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:- -
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
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | 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: -
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
| | Newbie | | Join Date: May 2009
Posts: 21
| | | re: Constructor issue
so using "throws BarException" will solve my problem ? Let me try
Thanks
| | Newbie | | Join Date: May 2009
Posts: 21
| | | re: Constructor issue
Most important am I calling the method correctly ? in the constructor ? - ReadFile(filename, value11, value2);
Thanks
| | Newbie | | Join Date: May 2009
Posts: 21
| | | 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
| | | 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:- -
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
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: Constructor issue Quote:
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 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
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,366 network members.
|