1

I'm using the following Code

public void getIPTableRules(){
    ProcessBuilder pb = new ProcessBuilder("/sbin/iptables", "-L");
    try {
        Process p = pb.start();
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        int lineCount = 0;
        String zeile;
        while ((zeile =  input.readLine()) != null) {
            System.out.println(zeile);
            System.out.println(lineCount);
            line[lineCount] = zeile;
            lineCount++;
        }

        input.close();
    } catch (IOException ex) {
        Logger.getLogger(CheckFirewall.class.getName()).log(Level.SEVERE, null, ex);
    }
}

The System.out's are printed correctly. The Variable line is set in the Class like:

public String line[];

The Exception occurs in the Line:

line[lineCount] = zeile;

So can someone please tell my what i'm doing wrong...

4

3 回答 3

3

很可能,您的String[] line数组没有像

String[] line = new String[100];

但是,由于您无法确定您的Process输出可能有多少行;我建议使用 aList<String>而不是你将初始化为

List<String> lines = new ArrayList<String>();

并将您的Process输出逐行添加为

while ((zeile =  input.readLine()) != null) {
    System.out.println(zeile);
    System.out.println(lineCount);
    lines.add(zeile); // using List#add()
    lineCount++;
}
于 2013-10-09T15:49:29.857 回答
3

你初始化你的线阵列了吗?看起来你只是声明了它。

你需要做这样的事情:

public void getIPTableRules(){
    ProcessBuilder pb = new ProcessBuilder("/sbin/iptables", "-L");
    line = new String[100]; // or whatever max size you want
于 2013-10-09T15:48:58.383 回答
1

你还没有初始化你的数组,只定义了它:

public String line[];

您需要使用适当的大小对其进行初始化:

public String line[] = new String[SIZE];

SIZE 是一个常数整数。

于 2013-10-09T15:49:22.637 回答