My strong advise to all users:Consider to
trim() filepath strings before feeding them to functions! Thereby you can avoid invisible whitespaces such as linebreak or tab at the string begining/end, which can cause invalid filepaths!
Suggestion to the processing development team:Please give
loadImage() a more meaningful debugging info such as: "File not found", "File permission error", etc, rather than only "NullPointerException". Thanks!
Quote:/// Global Variables
// In my project a filepath string came from an external application via TCP socket
// through the Net Library and contained a linebreak, which I didn't consider!!!
String imgPath = "test_card.jpg\n";
PImage img;
/// Setup Function
void setup() {
size(200, 200);
background(0);
noLoop();
frameRate(1);
}
/// Main Loop
void draw() {
// loadImage's debugging info in the console said "NullPointerException", but nothing further.
// Although I had my own debugging info printed to the console, like this:
println("Loading: " + imgPath);
// I never wondered, as the console said "Loading: 1.jpg" and the file "1.jpg"
// was at the source directory wihtout any file permission issues whatsoever!
// It MUST have worked. I was mad!!! I tried almost every possible correction. None worked!
// Only very finally I had the idea of invisible whitespaces in the String,
// for which I checked by surrounding the string with quotes like this:
println("Loading: \"" + imgPath + "\"");
// Now I realised that the second quote was after a linebreak, hence the string contained a linebreak!
// My strong advise to all users: trim() filepath-strings before feeding them to functions!
imgPath = trim(imgPath);
// Suggestion to the processing development team:
// Please give loadImage() a more meaningful debugging info such as:
// "File not found", "File permission error", etc, rather than only "NullPointerException". Thanks!
img = loadImage(imgPath);
image(img, 0, 0, width, height);
}