Click to See Complete Forum and Search --> : Having problems getting arguments from main method


voidflux
10-10-2004, 03:24 PM
Hello everyone, I have 3 files,

Application1.java
Target.java
Frame1.java

In my target class I have code that will draw a target shape.
In this pattern:
********
*******
******
*****
****
***
**
*

My code works fine, but I want to be able to accept an argument from a command prompt, so I need to get the following:


int size = Integer.parseInt(argv[0])

then i can pass size to my draw function:

Target t = new Target();
t.drawPattern(size);


The probelm is, since this is a windows based program, the main function is located in Application1.java and looks like this:


//Main method
public static void main(String[] args) {


try {


UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new Application1();
}



Does anyone know how i'm suppose to access the main argument? If I try to put any code into the main fucntion, it will say,
java.lang.NullPointerException

at lab5.Application1.main(Application1.java:76)

Exception in thread "main"

So how am i suppose to get the main argument if i'm not allowed to put any code in there?


Thanks for listening :D

Kram
10-10-2004, 09:51 PM
you could try to pass the new Application1() command the argv[0] parameter, you would just need to make a very simple change to the constructor of the Application1.java class, then use the argument as you see fit

voidflux
10-11-2004, 12:46 PM
Thats a good idea but my professor doesn't want us to modify the constructor unfortunatley.

Kram
10-11-2004, 08:03 PM
im a little confused, is the main method inside the Application1 class?

If so then you should just be able to store it as a variable...

i noticed that you wrote this:

int size = Integer.parseInt(argv[0])


it should be:

int size = Integer.parseInt(args[0])


with the s not the v, maybe thats why you were getting the null pointer??