Help Needed Solving NPE / String Array conversion/Globalization issue
in
Programming Questions
•
1 year ago
Hi... sorry for the verbose subject line... it's really hot here and I'm tired!
Which is probably why I'm not catching my mistake here, so I'd appreciate it if anyone has ideas about what I'm doing wrong (im sure its easy, just on of those days where its right under your nose...)
So, Im parsing some data from a webpage, and have all the info I need, but Im trying to assign a local String variable to an iterated index of a global String array, so that I can use the data in my draw method....
I have a global String Array called names[], and a local one called name.
In a for loop, name is set to join a bunch of characters.
Within that loop, I do
names[userCount] = name; to assign the relevant part of the array to the current name in that iteration of the loop (where userCount is initialized to 0 and increases after this statement every call.
But this is giving me an NPE... and I'm not sure why.
I wont know the length of the loop at this point in my code, so maybe I should use an ArrayList instead?
But there should be a way to do this with an array, no?
- String names[];
- int userCount = 0;
- //------------
- void setup() {
- // LOAD
- String lines[];
- lines = loadStrings("http://toolserver.org/~daniel/WikiSense/Contributors.php?wikifam=.wikipedia.org&wikilang=en&order=-edit_count&page=Great_Fire_of_Rome&grouped=on&ofs=0&max=1000");
- String name;
- String edit;
- int counter = 0;
- int curr = 0;
- for(int i = 0; i < lines.length; i++) {
- // Only Get Lines that Contain The Username
- if( lines[i].contains("title=User:") ) {
- int si = lines[i].indexOf("'>") +2; // Start Index of 1st Character In Name
- int fi = lines[i].indexOf("</") -1; // Final Index of last Character In Name
- int ei = lines[i -1].indexOf("("); // Edit Index of Number Of Edits In Previous Line
- String letters[] = new String[(fi - si) +1]; // Array Of Number Of Characters In Name
- // Add All Characters Into A String
- while(si + counter <= fi) {
- curr = si + counter; // Calculate Current Position
- letters[counter] = str(lines[i].charAt(curr)); // Add Current Character To Array
- counter++; // Iterate
- }
- counter = 0; // Reset For Next User
- edit = str(lines[i-1].charAt(ei -2)); // Find The Number Of Edits A User has Committed
- name = join(letters, ""); // Construct All Characters Into A String
- // Assign Current Name To Global Array
- names[userCount] = name; // This is the line thats giving me the NPE
- userCount++;
- println("NAME \t" + name + " \t \t EDITS \t" + edit);
- }
- }
- exit();
- }
- //------------
2