开发者

className.main(new String[]{"filename.txt"})?

开发者 https://www.devze.com 2023-01-21 07:04 出处:网络
I created a class that does some file parsing for me. I made it possible to be started as a standalone application, taking file name from command line.

I created a class that does some file parsing for me. I made it possible to be started as a standalone application, taking file name from command line. Now I created another class, that needs to take advantage of what the first class is doing, and I tried to call its main method like this:

className.main(new String[]{"filename.txt"});

However, it seems that things aren't doing so fine, because I was getting some null pointer exceptions. When I inserted system.out.pr开发者_JAVA百科intln(args[0]) to see what was going on, I got the reference to the resource, and not the string I was expecting.

Here is more code:


// this is from the class that is reffered as 'new one'
// Cal the maze solver now for out.txt
        String[] outFile = new String[]{"out.txt"};
        MazeSolver.main(outFile);

// this is part of the MazeSolver main method
public static void main(String[] args) {
        if(args.length != 1) {
            System.out.println("Usage: java MazeSolver ");
            System.exit(0);
        }
// this is the part where i tried to debug
        System.out.println(args.toString()); 


// and this is the error message that i got in terminal
// [Ljava.lang.String;@63b9240e   <---------------------------------------
//ROWCOUNT: 199
//Exception in thread "main" java.lang.NullPointerException


Do I need to create one more method, doing the exact same thing, but with different name?

Thanks


I'm just answering the part about printing an array of Strings:

System.out.println(args.toString()); 

This won't work, Array.toString() just returns an internal representation. You will want to use the helper method Arrays.toString(arr) in the Arrays class:

System.out.println(Arrays.toString(args));

Or, if you are dealing with a multi-dimensional array, use Arrays.deepToString(arr):

final Object[][] arr = new Object[3][];
arr[0]=new String[]{"a","b","c"};
arr[1]=new Integer[]{1,2,3,4,5};
arr[2]=new Boolean[]{true,false};
System.out.println(Arrays.deepToString(arr));

Output:

[[a, b, c], [1, 2, 3, 4, 5], [true, false]]


You are not supposed to use main for that purpose. Refactor your old class and create a new method called parse(String path). This new method should do whatever main did to make everything work.

public static void parse(String path)

By making it public other classes can access it and static means you don't need to create an instance of the class to use it. Your other class that wants to use the first class would do

MyFirstClassName.parse("file.txt");


I don't get it. Since main is your entry point I don't understand how you suppose to call it from an outside method if it is the first thing that is called when program starts. This will work only if you have two main methods declared in two different classes and you call one from the another, taking care that this last one is the one invoked by JVM at application start.

In any case I suggest you to avoid using main as a name for a generic method, it is not a keyword but I suggest you to just rename it to something else.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号