代码之家  ›  专栏  ›  技术社区  ›  hummingBird

classname.main(新字符串[]“filename.txt”)?

  •  1
  • hummingBird  · 技术社区  · 15 年前

    我创建了一个类,为我做一些文件解析。我让它可以作为一个独立的应用程序启动,从命令行获取文件名。 现在我创建了另一个类,它需要利用第一个类所做的工作,我尝试调用它的主要方法,如下所示:

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

    但是,事情似乎没有那么好,因为我得到了一些空指针异常。当我插入 system.out.println(args[0]) 为了了解发生了什么,我得到了对资源的引用,而不是我所期望的字符串。

    以下是更多代码:

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

    我是否需要再创建一个方法,执行完全相同的操作,但使用不同的名称?

    谢谢

    3 回复  |  直到 15 年前
        1
  •  0
  •   willcodejavaforfood    15 年前

    public static void parse(String path)
    

    MyFirstClassName.parse("file.txt");
    
        2
  •  3
  •   Sean Patrick Floyd    15 年前

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

    Arrays.toString(arr) Arrays

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

    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));
    

        3
  •  0
  •   Jack    15 年前

    main