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 & HelpSyntax Questions › loadImage silently fails if linebreak in filepath
Page Index Toggle Pages: 1
loadImage silently fails if linebreak in filepath (Read 363 times)
loadImage silently fails if linebreak in filepath
Jul 20th, 2009, 3:43pm
 
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);
}

Page Index Toggle Pages: 1