Passing PApplet Around
in
Integration and Hardware
•
2 years ago
Edit: I noticed the formatting of my code got a bit messed up, I think it still gets the point accross though.
Hey everyone, I'm knew to Processing and I keep getting a NullPointerException thrown when I try to load a PImage. Can someone help me out? The exception occurs on the first line of loadImage(String filename) inside SpriteManager.
Game.java
[source]
import
processing.core.*;public class Game extends PApplet {
private SpriteManager spriteManager;
public Game() {
spriteManager = new SpriteManager( this );
}
public void setup() {
size( 640 , 480 );
spriteManager.loadAll( "sprites" );
}
public void draw() {
background( 0 , 0 , 0 , 255 );
}
public static void main(String[] args) {
PApplet.main( new String[] { "--present" , "Game" });
}
}
[/source]
SpriteManager.java
[source]
import
java.util.HashMap;import java.io.File;
import processing.core.PImage;
import processing.core.PApplet;
public class SpriteManager {
private PApplet pApplet;
private HashMap hash;
public SpriteManager(PApplet pApplet) {
pApplet = pApplet;
hash = new HashMap( 128 );
}
public boolean load(String filename) {
PImage image = pApplet.loadImage(filename);
if (image == null )
return false ;
hash.put(filename, image);
return true ;
}
public boolean loadAll(String directory) {
File file = new File(directory);
if (!file.exists())
return false ;
String[] children = file.list();
for ( int i = 0 ; i < children.length; ++i)
load(children[i]);
return true ;
}
public Sprite get(String spriteName) {
return (Sprite)hash.get(spriteName);
}
public void unload(String spriteName) {
hash.remove(spriteName);
}
public void unloadAll() {
hash.clear();
}
}
[/source]
Thanks for reading.
1