Question

I have some processing code, which i know works. But when i begin rewriting the code into a setup and draw setup(i think its called active), it gives me a "NullPointerException", when running it. Can it be something with the arrays? hope you can help me :) (and yes i know it is alot of slave code writing, but it was the only way i could figure it out :3 im a newbie to processing and coding in general. also i ruled out some of the slave stuff, so it was only the code i know gave me problems):

    //stating the int's and strings
String[] lines = loadStrings("brown.txt");//the array with the words
String longString = join(lines, " "); //joining the array to a normal string of words
int characters = longString.length(); //used to find the amount of all the letters
String lowercase = longString.toLowerCase();//makes sure all the letters are lowercased

//setting up the letter freqiency finder
char[] c = lowercase.toCharArray(); //used to make a character array
int freq = 0, freq2 = 0, freq3 = 0, freq4 = 0,              //|
freq5 = 0, freq6 = 0, freq7 = 0, freq8 = 0, freq9 = 0,      //|
freq10 = 0, freq11 = 0, freq12 = 0, freq13 = 0,             //|naming and setting all the frequencies
freq14 = 0, freq15 = 0, freq16 = 0, freq17 = 0, freq18 = 0, //|
freq19 = 0, freq20 = 0, freq21 = 0, freq22 = 0, freq23 = 0, //|
freq24 = 0, freq25 = 0, freq26 = 0;                         //|

void setup() {
  size(600, 600);
  background(255);
}

void draw() {
//setting up the letter counters
  for (int i = 0; i< c.length; i++)
  {
    if (c[i]=='a')
      freq++; 
    if (c[i]=='b')
      freq2++; 
    ......... etc
Was it helpful?

Solution

You shouldn't do variable manipulation outside of a function in Processing. Before setup(), you should declare your variables, but you give them values in setup(). See here (under "Reading and Writing Text Files") for an example of this. Here is what you should do with your code:

String[] lines;
int[] freq;

void setup(){
    size(600,600);
    background(255);
    lines = loadStrings("brown.txt");//the array with the words
    if(lines == null){
      print("error loading strings!");
    }
    String longString = join(lines, " "); //joining the array to a normal string of words
    int characters = longString.length(); //used to find the amount of all the letters
    String lowercase = longString.toLowerCase();//makes sure all the letters are lowercased

    //setting up the letter frequency finder
    char[] c = lowercase.toCharArray(); //used to make a character array
    freq = new int[26];
    for(int i = 0; i < c.length; i++){
      //chars are really just numbers and a-z is consecutive, so 'a' - 'a' = 0, 'b' - 'a' = 1, 'c' - 'a' = 2...
      // note that you can also check to see if a character is between a-z by doing if(chr >= 'a' && chr <= 'z'){}
      freq[c[i] - 'a']++;
    }
}
void draw() {}

OTHER TIPS

try this String[] lines = new String[]{}; then lines = loadStrings("brown.txt"); if the problem persists the issue is certainly in your loadString Method

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top