We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Simple Question I Think (NullPointerException)
Page Index Toggle Pages: 1
Simple Question I Think (NullPointerException) (Read 3604 times)
Simple Question I Think (NullPointerException)
Mar 30th, 2006, 5:41am
 
Hey guys, I'm brand new to processing and was hoping I could get some insight into this error that has been bugging me. Other than a single semester of C++ I don't have much knowledge of programming (apart from HTML/CSS). I'm writing what I think is a relatively simple program: basically I'm importing a text file, counting the number of characters in each line and then spitting out a random circle proportionally sized for each value.

Everything works great when I run it without any functions (no setup(), draw(), etc) but as soon as I add those in, I get a NullPointerException. Not sure what I'm doing wrong, here's the code:

Quote:
// Import the source file: list.txt
String lines[] = loadStrings("list.txt");

// Create array that will contain the length, in number characters, for each line
int[] longs = new int[lines.length];

void setup() {
size(1000,500);
smooth();
background(50);
}

// Fill length array, "longs," with values from the "lines" array
for(int i = 0; i < lines.length; i++) {    
 String a = lines[ i];
 int x = a.length();
 longs[ i] = x;
}

void draw() {
// Print the number of lines
println("There are " + lines.length + " lines!");

// Create a randomly placed and colored circle for each line, size of circle proportional to line length

noStroke();

for (int i = 0; i < lines.length; i++) {
 int b = longs[ i];
 fill(random(0,255), random(0,255), random(0,255), random(0,255));
 ellipse(random(100,width-100), random(100,height-100), b/4, b/4);
 }
}
Re: Simple Question I Think (NullPointerException)
Reply #1 - Mar 30th, 2006, 11:22am
 
You've got code outside of functions...

Code:
 // Fill length array, "longs," with values from the "lines" array
for(int i = 0; i < lines.length; i++) {
String a = lines[ i];
int x = a.length();
longs[ i] = x;
}


This chunk needs to go inside the setup() function.

When you're using the setup/draw system all code has to be inside a function, you can't have any bare code just sitting outside like that.
Re: Simple Question I Think (NullPointerException)
Reply #2 - Mar 31st, 2006, 7:26am
 
Thanks for the reply John!

I tried putting that chunk of code into the setup() function but I'm still getting a NullPointerException. When the error occurs it highlights the line: "int[] longs = new int[lines.length];"

I made sure the file that it's being imported is in the correct spot and oddly enough when I comment out the voids everything works again.

Here's the code after moving that section into setup():

Quote:
// Import the source file: list.txt
String lines[] = loadStrings("list.txt");

// Create array that will contain the length, in number characters, for each line
int[] longs = new int[lines.length];

void setup() {
size(1000,500);  
smooth();  
background(50);

// Fill length array, "longs," with values from the "lines" array
for(int i = 0; i < lines.length; i++) {      
 String a = lines[ i];
 int x = a.length();
 longs[ i] = x;  
}  

}


void draw() {
// Print the number of lines
println("There are " + lines.length + " lines!");

// Create a randomly placed and colored circle for each line, size of circle proportional to line length

noStroke();

for (int i = 0; i < lines.length; i++) {
 int b = longs[ i];
 fill(random(0,255), random(0,255), random(0,255), random(0,255));
 ellipse(random(100,width-100), random(100,height-100), b/4, b/4);
 }
}
Re: Simple Question I Think (NullPointerException)
Reply #3 - Mar 31st, 2006, 11:16am
 
Ah yes, I think I know why.
Try changing it to:
Code:
 // Import the source file: list.txt  
String[] lines;

// Create array that will contain the length, in number characters, for each line
int[] longs;

void setup() {
size(1000,500);
lines = loadStrings("list.txt");
longs = new int[lines.length];
smooth();
background(50);

// Fill length array, "longs," with values from the "lines" array
for(int i = 0; i < lines.length; i++) {
String a = lines[ i];
int x = a.length();
longs[ i] = x;
}
}


void draw() {
// Print the number of lines
println("There are " + lines.length + " lines!");

// Create a randomly placed and colored circle for each line, size of circle proportional to line length

noStroke();

for (int i = 0; i < lines.length; i++) {
int b = longs[ i];
fill(random(0,255), random(0,255), random(0,255), random(0,255));
ellipse(random(100,width-100), random(100,height-100), b/4, b/4);
}
}


The problem is that even though the line to load the strings is before the longs definition, the loadStrings can't happen until the program actually runs, and the compiler has to create memory for the longs and lines arrays at compile-time, which it can't do, as it doesn't know how long list.txt is.

Basicly, if you'r using the setup/draw style of sketch, it's better if you don't give values to variables outside of a function, as you can't guarantee that it'll work as expected. You can define them outside, and need to in many cases, you just need to wait until inside setup() to actually give them a value/values.
Re: Simple Question I Think (NullPointerException)
Reply #4 - Mar 31st, 2006, 5:17pm
 
Perfect, thanks a bunch!
Page Index Toggle Pages: 1