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 › NullPointerExeption error
Page Index Toggle Pages: 1
NullPointerExeption error (Read 1273 times)
NullPointerExeption error
Apr 14th, 2010, 2:36pm
 
Hello,

I use the following code to send an image from my C328 camera to Processing.

I get the following error: nullpointerexeption

I use the following Processing code:

Code:

import processing.serial.*;

Serial myPort;
int i = 1;

String ImageFilename = "Photo_" + year() + "_" + day() + "_" + month() + "-" + hour() + "_" + minute() + "_" + second() + "_" + millis() + ".jpg";
int CharE = 69;
int CharO = 79;
int CharF = 70;
int TotalBytes = 0;

byte[] Photo = {
};

void setup()
{
 //Set and open serial port
 println( Serial.list() );
 myPort = new Serial( this, Serial.list()[1], 9600 );
 myPort.clear();
}

void draw()
{
 // Import Picture
 byte[] buffer = new byte[64];

 while( myPort.available() > 0 )
 {
   int readBytes = myPort.readBytes(buffer);

   //print( "Read " );
   //print( readBytes );
   //println( " bytes ..." );

   for( int i = 0; i < readBytes; i++ )
   {

TotalBytes = TotalBytes + 1;

//print( "Read " );
//print( TotalBytes );
//println( " bytes in total ..." );

Photo = append( Photo, buffer[i] );

print( "PhotoArray contains: " );
print(Photo.length);
println( " Elements..." );

if ( TotalBytes > 2 && Photo[TotalBytes-3] == CharE && Photo[TotalBytes-2] == CharO && Photo[TotalBytes-1] == CharF ){

 // Save picture to file
 if( Photo.length > 0 ) {
   print( "Writing to disk " );
   print( Photo.length );
   println( " bytes ..." );
   saveBytes( ImageFilename, Photo );
   println( "DONE!" );

   // Open Picture
   size(6400, 4800);
   PImage C328Image;
   C328Image = loadImage(ImageFilename);
   image(C328Image, 0, 0);

   //Clear the array
   Photo = null; // clear array

   //Clear totalbytes
   TotalBytes = 0;

   //Flush serial port
   myPort.clear();

   return;

 }
}
   }
 }
}


The error is executed on this line when a new picture is imported (2nd cycle):

Code:

Photo = append( Photo, buffer[i] );


Probably the problem is due to the fact that I try to clear the array using this line of code:

Code:

Photo = null; // clear array ????


Is this the problem? If yes, is there another way to clear/delete the array. Or is there something else wrong.....

Thanks in advance,

Cheers.
Re: NullPointerExeption error
Reply #1 - Apr 14th, 2010, 3:17pm
 
Try:

Photo = new byte[];

Rather than emptying the array I suspect that by assigning it to null you 'destroy' the array completely, meaning you're then unable to append anything to it.  By assigning it to a new empty array it should be ready to have new items appended...

Well - that's my hunch Wink
Re: NullPointerExeption error
Reply #2 - Apr 15th, 2010, 9:19am
 
Hi,

Thanks for you reply Smiley

I've tried your suggestion but get the following error:

Variable must provide either dimension dimensions or an array initializer.

This is the original declared byte:

Code:

byte[] Photo = {
};


Should I use an arraylist?

Whats wrong?
Re: NullPointerExeption error
Reply #3 - Apr 15th, 2010, 9:50am
 
Sorry - my bad...  That should have been:

Code:
Photo = new byte[0]; 

Re: NullPointerExeption error
Reply #4 - Apr 15th, 2010, 9:52am
 
byte[] Photo = { 0 };
or
byte[] Photo = new byte[0];
Re: NullPointerExeption error
Reply #5 - Apr 15th, 2010, 10:27am
 
mmmmm strange thing is when the second picture is imported and I print Photo.length(); it's not empty. But continues from where it stopped.

It looks like:

Code:

byte[] Photo = { 0 };
or
byte[] Photo = new byte[0];


does not empty the array?

EDIT:

Strange, I've used this one:

Code:

Photo = new byte[0];


And everything works.........
Re: NullPointerExeption error
Reply #6 - Apr 15th, 2010, 12:38pm
 
I think if you declare the type again before 'resetting' the array (i.e. 'byte[] Photo =...' rather than 'Photo =...') then you're in fact creating a new 'Photo' array with local scope...
Re: NullPointerExeption error
Reply #7 - Apr 15th, 2010, 12:48pm
 
I was referring to the initial declaration, of course. I shown { 0 } syntax to highlight that it needs at least an element inside to be legal, but indeed it adds an initial value you might not want.
The new byte[0] actually declares a new array... of size zero, so an empty one.
Page Index Toggle Pages: 1