Reputation: 1
I have written an Java Processing application. If I try to start it, I get the following error:
""processing.core.PGraphics.createShape(int, float[])" because "this.g" is null".
My Application has a View, Controller and Modell. They only interact through interfaces with each other. My Modell-Classes extend PApplet and create some shapes, but I also get errors in my view. Maybe something is not passed on correctly.
I know, that the error may be caused, because I try to call a method on an object that has not been initialized yet. In this case the processing.core.PGraphics.createShape(int, float[]) method on this.g.
My PApplet window opens, but nothing is drawn on it.
Could someone please look into it?
I tried to create instances of PApplet and/or Graphic in my View class and pass it in to my controller class; e.g.:
my View, where the objects are drawn.
public class xyView extends PApplet implements ILeiterspielView {
public static void main(String[] args) {
PApplet.main(LeiterspielView.class);
}
...
private static PApplet instance;
//other attempt: PApplet p = this;
//other attempt: PApplet p;
...
public xyView (other examples/*PApplet pa*//*Graphics gd*/) {
instance = this;
//this.gd = gd;
//this.p = pa;
setSize(1000, 1000);
}
public void setup (/*Dice[] dice*//*this*/) {
this.controller = new xyController(this, width, height, instance);
...
}
my controller, the interface between the view and model.
public LeiterspielController (IxyView view, ..., PApplet p) {
this.view = view;
this.game = new xyModel();
this.p = p;
...
}
My first solution doesn't change the outcome.
The PApplet p = this solution changes the outcome to:
java.lang.RuntimeException: java.lang.NoSuchMethodException: leiterspiel.view.LeiterspielView.()
I don't know what this means and the PApplet window doesn't open.
Upvotes: 0
Views: 431
Reputation: 6732
I have never worked with Processing, but a few things first:
Please stop using letters as variable names, it makes it a real pain to debug your code or to even look at it. Also, always include the stacktrace for exceptions and every method involved in causing it, if its needed.
Regarding your first problem: this.g is null
-> I can't see a g
variable anywhere?
The second problem comes from java not finding a no-args constructor for your LeiterspielController
- judging from your code, it takes several args which you haven't shared.
The constructor should by called by PApplet.main(LeiterspielView.class);
- this method (source) calls another main method (source) which then calls the runSketch(final String[] args,final PApplet constructedSketch)
method (source) in this method, you find the following snippet which tries to acquire a no-args constructor by reflection:
Class<?> c = Thread.currentThread().getContextClassLoader().loadClass(name);
sketch = (PApplet) c.getDeclaredConstructor().newInstance();
See the javadoc for getDeclaredConstructor() - as shown by the javadoc, this method throws an exception:
NoSuchMethodException - if a matching method is not found.
In Java, classes actually do have a default no-args constructor unless you have defined another one, which you actually have (see this answer).
So basically you need a non-args constructor here and probably this may not work with your architecture. I'd therefore recommend you to stick to examples with Processing and think about which architecture makes sense for you given the limits by Processing. However: There might be a way to do this I am not aware of, as I said, I never worked with that particular library.
Upvotes: 0