0

I get nullpointerexception when I try to fill a 2D array with process IDs, it is 2D since there is a unlimited list of PIDs for each system and I need to eventually return that back to my main code (set to void now since just a prototype test function).

Any ideas would be great

private void testfunctionA (List<String> additionalPC) {

    // A 2d array that will contain a list of pids for each system - needs to be strings and not integers
    String[][] pidCollection = new String[additionalPC.size()][];

    // Go through one system at a time
    for (int i=0; i < additionalPC.size(); i++) {

            // Get pids for apple per system
            String listofpids = Driver.exec("ssh " +  additionalPayloads.get(i) + " ps -ef | grep -i apple | grep -v \"grep -i apple\" | awk \\' {print $2}\\'");

            // Works ok for printing for one system
            System.out.println(listofpids);
            // put the list of pids into a string array - they are separated by rows
            String[] tempPid = listofpids.split("\n");

            // Put the string array into the 2d array - put this fails with a NPE
            for (int j=0; j < tempPid.length; j++) {
                    pidCollection[i][j] = tempPid[j];
            }

            System.out.println(pidCollection);


    }
0

3 Answers 3

1

You've created your 2D array, but the array is full of null 1D arrays. Each element in the 2D array needs to have a 1D array created. You've already created it with tempPid; just use it. Instead of

for (int j=0; j < tempPid.length; j++) {
    pidCollection[i][j] = tempPid[j];
}

just use

pidCollection[i] = tempPid;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it works! also realized that i shouldn't be printing a multi-dimensional array like I did in my example.
1

You need to initialize each element of pidCollection:

String[] tempPid = listofpids.split("\n");

pidCollection[i] = new String[tempPid.length];

// Put the string array into the 2d array - put this fails with a NPE
for (int j=0; j < tempPid.length; j++) {
        pidCollection[i][j] = tempPid[j];
}

or, in this case, more simply:

pidCollection[i] = listofpids.split("\n");

Comments

0

Short answer, you need to define the second dimention of your array

String[][] pidCollection = new String[additionalPC.size()][?????]; //this initialises the` first axis

Long answer: You're not abliged to do it on that line, and you can do it on a case by case basis for each first dimention, eg

pidCollection[i]=new String[tempPid.length] //this initialised the second axis for a particular i

but you need to do it at some point, easiest solution is to define both dimentions at once unless you have a reason not to. Although I think that may not be applicable in this case so use the case by case method

1 Comment

There's no way for me to know the size of the PID list beforehand so I cannot give a fixed length beforehand. And the previous solution worked. Thanks for the input though

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.