/**
*
*/
package be.pandapp.ehb;
这是第一堂课。
import processing.core.PApplet;
import processing.core.PShape;
/**
* @author Pindamannetje
*
*/
public class SlashTag extends DisplayObject {
PShape headAndFoot;
PShape middle;
PShape text;
float scale;
public SlashTag(float x, float y, float z, int fillColor, float scale, PShape headAndFoot, PShape middle, PShape text) {
super(x, y, z);
this.headAndFoot = headAndFoot;
this.middle = middle;
this.text = text;
this.scale = scale;
}
public float getScale() {
return scale;
}
public void setScale(float scale) {
this.scale = scale;
}
public PShape getMiddle() {
return middle;
}
public void setMiddle(PShape middle) {
this.middle = middle;
}
public PShape getHeadAndFoot() {
return headAndFoot;
}
public void setHeadAndFoot(PShape headAndFoot) {
this.headAndFoot = headAndFoot;
}
public PShape getText() {
return text;
}
public void setText(PShape text) {
this.text = text;
}
@Override
public void draw(PApplet app) {
/*app.fill(fillColor);
app.stroke(strokeColor);
app.strokeWeight(strokeWeight);*/
app.pushMatrix();
app.translate(x, y);
app.scale(scale);
app.shape(headAndFoot);
app.shape(middle);
app.shape(text);
app.popMatrix();
}
}
这是我尝试创建PApplet但却出错的类。
package be.pandapp.ehb;
import processing.core.*;
public class Slashing extends SlashTag {
boolean mouseAfterClick;
boolean outOfPlace;
int lastMouseX;
PApplet app;
public Slashing(float x, float y, float z, int fillColor, float scale,
PShape headAndFoot, PShape middle, PShape text, PApplet app) {
super(x, y, z, fillColor, scale, headAndFoot, middle, text);
this.app = app;
// TODO Auto-generated constructor stub
}
int mouseX2 = (int) (app.mouseX * 0.245);
int lastMouseX2 = (int) (lastMouseX * 0.245);
public void draw() {
if (app.mouseX >= 410 && app.mousePressed) {
outOfPlace = true;
app.shape(middle, app.mouseX, 492 - (mouseX2));
}
else if (lastMouseX < 420) {
outOfPlace = false;
mouseAfterClick = false;
}
if (outOfPlace && mouseAfterClick && app.mousePressed == false) {
app.shape(middle, lastMouseX, 492 - (lastMouseX2));
lastMouseX -= 15;
}
else if (outOfPlace == false) {
}
}
public void mouseReleased() {
mouseAfterClick = true;
lastMouseX = app.mouseX;
}
}
错误给出的是一个空指针。
抱歉忘了主要课程。
这里是:
import be.pandapp.ehb.Slashing;
import be.pandapp.ehb.Slashing;
import processing.core.PApplet;
import processing.core.PShape;
/**
*
*/
/**
* @author AdrienSchautteet
*
*/
public class MainSlashTag extends PApplet {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PApplet.main(new String[] { "PandApp" });
}
private int aantal = 15;
Slashing Slashing[] = new Slashing[aantal];
private PShape headAndFoot;
private PShape middle;
private PShape text;
// ---
boolean mouseAfterClick;
boolean outOfPlace;
int lastMouseX;
public void setup() {
size(1280, 800);
background(0);
smooth();
headAndFoot = loadShape("../images/SlashTagProcessingHeadFoot.svg");
middle = loadShape("../images/SlashTagProcessingMiddle.svg");
text = loadShape("../images/SlashTagProcessingText.svg");
text.disableStyle();
outOfPlace = false;
}
public void draw() {
for (int i = 0; i < this.aantal; i++) {
if (i == 0) {
Slashing[i] = new Slashing(random(0, 100),
random(0, 100), 0, 255, 1, headAndFoot, middle,
text,this);
Slashing[i].draw(this);
}
if (i < 5 && i > 0) {
Slashing[i] = new Slashing(random(Slashing[i - 1].getX() + 150,
Slashing[i - 1].getX() + 275), random(0, 100),
0, 255, 1, headAndFoot, middle, text,this);
Slashing[i].draw(this);
}
if (i == 5) {
Slashing[i] = new Slashing(random(0, 100),
random(250, 350), 0, 255, 1, headAndFoot, middle,
text,this);
Slashing[i].draw(this);
}
if (i < 10 && i > 5) {
Slashing[i] = new Slashing(random(Slashing[i - 1].getX() + 150,
Slashing[i - 1].getX() + 275), random(250, 350),
0, 255, 1, headAndFoot, middle, text,this);
Slashing[i].draw(this);
System.out.println(Slashing[i].getX());
}
if (i == 10) {
Slashing[i] = new Slashing(random(0, 100),
random(500, 600), 0, 255, 1, headAndFoot, middle,
text,this);
Slashing[i].draw(this);
}
if (i <= 15 && i > 10) {
Slashing[i] = new Slashing(random(Slashing[i - 1].getX() + 150,
Slashing[i - 1].getX() + 275), random(500, 600),
0, 255, 1, headAndFoot, middle, text,this);
Slashing[i].draw(this);
System.out.println(Slashing[i].getX());
}
stop();
}
}
}
这是错误:
线程“动画线程”中的异常java.lang.NullPointerException
at be.pandapp.ehb.Slashing.<init>(Slashing.java:20)
at MainSlashTag.draw(MainSlashTag.java:58)
at processing.core.PApplet.handleDraw(PApplet.java:2305)
at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:243)
at processing.core.PApplet.run(PApplet.java:2176)
at java.lang.Thread.run(Thread.java:722)
答案 0 :(得分:1)
问题是(在Slashing类中)你试图在初始化类时给字段mouseX2赋值,无论你是否把它放在构造函数之后。在构造函数之前创建此类的引用时,将执行不在构造函数或方法(或内部类)中的类中的任何值赋值。由于您在构造函数内调用填充变量“this.app”并带有传入的“app”值,因此在“app”尚未定义之前,因此为null。然后你尝试访问它以获取mouseX并且jvm会抛出一个nullpointer ...
因此,行:
public Slashing(float x, float y, float z, int fillColor, float scale,
PShape headAndFoot, PShape middle, PShape text, PApplet app) {
super(x, y, z, fillColor, scale, headAndFoot, middle, text);
this.app = app;
// TODO Auto-generated constructor stub
}
int mouseX2 = (int) (app.mouseX * 0.245);
int lastMouseX2 = (int) (lastMouseX * 0.245);
应该成为:
int mouseX2,lastMouseX2;
public Slashing(float x, float y, float z, int fillColor, float scale,
PShape headAndFoot, PShape middle, PShape text, PApplet app) {
super(x, y, z, fillColor, scale, headAndFoot, middle, text);
this.app = app;
// TODO Auto-generated constructor stub
mouseX2 = (int) (app.mouseX * 0.245);
lastMouseX2 = (int) (lastMouseX * 0.245);
}