开发者

NullPointerException in Processing, Java

开发者 https://www.devze.com 2023-02-20 05:00 出处:网络
I\'m hoping some of you are familiar with the Processing development environment for Java. I\'ve searched the site, but wasn\'t able to find too much. So I\'m supposed to be passing command-line argum

I'm hoping some of you are familiar with the Processing development environment for Java. I've searched the site, but wasn't able to find too much. So I'm supposed to be passing command-line arguments from the 'args' string array from

public static void main(String[] args )

...to arrays in a setup() function so that I can parse them and use them as values for the heights of bar graphs, which are to be drawn on a graphical window. I don't understand why I keep getting a NullPointerException error at line 16...

for(int i = 0; i < tempArgs.length; i++) {
    dataArray[i] = Integer.parseInt(tempArgs[i]);
}

.....as I thought the tempArray array should have values in it, which seems to be bourne out by the System.out.println statement I used to test this at the end of the main method. Here's the whole code, it's quite short. Any help would be much appreciated.

public class Main extends PApplet {
    int[] dataArray ;
    int[] normalizedData;
    String[] tempArgs;

    @Override
    public void setup() {
        /*size(dataArray[2], dataArray[3]);
        smooth();
        background(255);*/
        for(int i = 0; i < tempArgs.length; i++) {
            dataArray[i] = Integer.parseInt(tempArgs[i]);
        }

        int max = dataArray[0];
        for(int i = 0; i < dataArray.length; i++) {
            if(dataArray[i] > max) {
                max = dataArray[i];
            }
        }

        for(int i = 0; i < dataArray.length; i++) {
            normalizedData[i] = (dataArray[i] / max) * (height - 20);
        }

        for(int i = 0; i < normalizedData.length; i++) {
            fill(255,34,65);
            rect(3, 5, 10, normalizedData[i]);
        }

        size(dataArray[2], dataArray[3]);
        smooth();
        background(255);

     }

    public static void main(String[] args) {
        String[] tempArgs = new String[args.length + 2];
        tempArgs[0] = "--bgcolor=#FFFFFF";
        tempArgs[1] = "project1.Main";
        for(int i = 2; i < tempArgs.length; i++) {
            tempArgs[i] = args[i-2];
        }
        System.out.println(tempArgs.length);
          PApple开发者_StackOverflowt.main(tempArgs);          
    }
}


You are missing the creation of dataArray.

@Override
public void setup() {
    // add this line
    dataArray = new int[tempArgs.length];

    for(int i = 0; i < tempArgs.length; i++) {
        dataArray[i] = Integer.parseInt(tempArgs[i]);
    }
    ...
}

Also, the tempArgs should be static, because in main() there is no instance of Main class yet, so only saving tempArgs will allow to use it later in setup().

public class Main extends PApplet {
    int[] dataArray ;
    int[] normalizedData;
    static String[] tempArgs;

    ...

    public static void main(String[] args) {
        tempArgs = new String[args.length + 2]; // note change in this line!
        ...
    }
}

Possibly it could be done simplier, because I see you are passing tempArgs to PApplet.main(), however I do not know how this applet framework works.


you have 2 separate tempArgs arrays.

  • One is created locally in main(), and that one prints out correctly
  • the other one belongs to the class and is never instantiated.
0

精彩评论

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