We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi and thanks for reading this!
I'm trying to create a program to visualize and modify laser art. As an industry standard, laser art is saved as a binary file (an ILDA file for the ones interested). I tried loading in the data with loadBytes(String location) and this worked fine, provided the data was used in the same method in which I used loadBytes.
loadBytes() returns an Array with the bytes in it:
byte b[] = loadBytes("something.dat");
The problem is, I can only access the array b[] in the method in which I used loadBytes(). I'm probably missing something obvious, but I can't figure out how to get this array a class variable. This is what I have right now:
File theFile;
public void setup()
{
size(400, 400, P3D);
frame.setResizable(true);
//Load
theFile = new File("Medium Colours Test.ild");
theFile.printHeader();
}
class File
{
String location;
byte[] b;
File(String _location)
{
location = _location;
byte b[] = loadBytes(location);
println(b.length);
}
void printHeader()
{
//Print out the file header:
println("length of b: " + b.length); //This line throws a NullPointerException.
println("***HEADER***");
for (int i = 0; i < 4; i++) {
print(char(abs(b[i])));
}
/* And a whole lot more of these printlines... */
}
}
It looks like the Array b isn't classified as a global class variable, because of the NullPointerExceptions... even though I declared it in the beginning of the class code. If I put the contents of the printHeader() method inside the File constructor, it works just fine. What is the correct syntax?
(Edit) I have a feeling I should include a byte[] b = new byte[int length];
somewhere, but that should happen before the loadBytes() method, and the paradox is that an array requires you define its fixed length upon creating it, while you only know the length once you used loadBytes()! Because of this I assumed loadBytes() initiates the array somewhere internally (?) but not in a way so you can use the array as a global variable...
Thanks!
Answers
b is a File member var. Should be possible to access it by dot Notation or by a getter, something like: in setup or wherever (theFile is global)
theFile.b
ortheFile.b[0]
should work. Or define a getter method inside File class.byte[] getBytes(){ return b; }
then you do
theFile.getBytes()[0]
I believe the problem is the word "byte" at the beginning of line 21. You've already declared b[] but you're now creating a separate array, also called b[], that exists only inside the constructor. See if removing that word makes a difference.
Edit: maybe the b[] in line 21 should just be b.
Define your array up front, before you get to void setup(), outside of any function, and that will make it a global array with scope throughout your application.
For example:
Hope this helps. As mentioned bi bilmore, you also declare it twice, one declaration overwriting the other. A new declaration sets up a fresh empty array.
Aha! Thanks Bilmor, that was exactly the problem. I knew it was something simple like that :) And yes, it's exactly how you said it, line 21 now reads
b = loadBytes(location);
. @hondaman900: I really only want this array to exist inside the File class, since I'm going to convert the data from the file to my own data. @_vk: thanks for replying, but that didn't help.Still getting a weird OpenGL error but ignoring that for now :P OpenGL error 1282 at bot beginDraw(): invalid operation ???
I wonder creating a custom File
class
would conflict w/ Java's own File 1? :-SSI hope not, but I'll rename the class anyway :)