首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将PGraphics的ArrayList从主类重构到单独的类中时,出现空指针异常

问将PGraphics的ArrayList从主类重构到单独的类中时,出现空指针异常
EN

Stack Overflow用户
提问于 2013-06-29 07:18:42
回答 1查看 521关注 0票数 1

我正在使用Eclipse用Java编写一个处理Applet。当我重构一个方法以从主类创建PGraphics的ArrayList到一个单独的类时,我总是得到一个空指针异常。所有在主类中编写的方法都可以工作(下面的第一个代码样本),不工作的是重构(第二对代码样本)。我到处寻找答案(一些相似但没有匹配),并进行了多次重写,但没有骰子。看一看,提前感谢你的帮助:

这段代码(所有代码都在一个类中)可以工作:

代码语言:javascript
复制
package tester;

import java.util.ArrayList;
import processing.core.*;

public class GraphicsMain extends PApplet{
int screenWidth = 800;
int screenHeight = 500;
String filepath = "/a/filepath/here/";
ArrayList<PGraphics> motifArray = new ArrayList<PGraphics>();
int stageWidth = 100; // eventually make this based on the motif's max width
int stageHeight = 400; // make this based on the motif's max height
PImage pic = loadImage(filepath + "small_jones6.png");
PImage pic2 = loadImage(filepath + "small_dresser10.png");


public void setup() {
    size(screenWidth,screenHeight,P3D);
    background(255);

    for (int i = 0; i < 2; i++) {
        motifArray.add(createGraphics(stageWidth,stageHeight,P3D));
        motifArray.get(i).beginDraw();
        motifArray.get(i).image(pic,10,10,100,90);
        motifArray.get(i).image(pic2,10,50,50,50);
        motifArray.get(i).endDraw();            
    }

    if (motifArray.get(0) == null) {
        System.out.println("The motif array is unfilled!");
    }
}

public void draw() {
    for (int i = 0; i < motifArray.size(); i ++) {
        image(motifArray.get(i),200+(400*i),100);

    }

}

static public void main(String[] passedArgs) {
    String[] appletArgs = new String[] { "GraphicsMain" };
    if (passedArgs != null) {
        PApplet.main(concat(appletArgs, passedArgs));
    } else {
        PApplet.main(appletArgs);
    }
}

}

下面的代码(重构为两个单独的类)不会:

**我注意到了在null指针异常中标识的每一类中的两行

第一类:

代码语言:javascript
复制
package tester;

import java.util.ArrayList;
import processing.core.*;

public class GraphicsMain extends PApplet{
int screenWidth = 800;
int screenHeight = 500;
PGraphicsMaker pgm = new PGraphicsMaker(this);
ArrayList<PGraphics> motifArray;

public void setup() {
    size(screenWidth,screenHeight,P3D);
    background(255);

    motifArray = pgm.makeMotifArray(); // ** NULL POINTER EXCEPTION HERE **

    if (motifArray.get(0) == null) {
        System.out.println("The motif array is unfilled!");
    }
}

public void draw() {
    for (int i = 0; i < motifArray.size(); i ++) {
        image(motifArray.get(i),200+(400*i),100);

    }


}

static public void main(String[] passedArgs) {
    String[] appletArgs = new String[] { "GraphicsMain" };
    if (passedArgs != null) {
        PApplet.main(concat(appletArgs, passedArgs));
    } else {
        PApplet.main(appletArgs);
    }
}

}

第二类:

代码语言:javascript
复制
package tester;

import java.util.ArrayList;
import processing.core.PApplet;
import processing.core.PGraphics;
import processing.core.PImage;
import processing.core.*;

public class PGraphicsMaker {
String filepath = "/a/filepath/here/";
PApplet parent;
int stageWidth = 100; // eventually make this based on the motif's max width
int stageHeight = 400; // make this based on the motif's max height
ArrayList<PGraphics> motifArray;
PImage pic;
PImage pic2;

public PGraphicsMaker(PApplet pa) {
    PApplet parent = pa;
    pic = parent.loadImage(filepath + "small_jones6.png");
    pic2 = parent.loadImage(filepath + "small_dresser10.png");
}

public ArrayList makeMotifArray() {
    ArrayList<PGraphics> motifArray = new ArrayList<PGraphics>();
    for (int i = 0; i < 2; i++) {
            // ** NULL POINTER EXCEPTION on the line below **
       motifArray.add(parent.createGraphics(stageWidth,stageHeight,parent.P3D));
       motifArray.get(i).beginDraw();
       motifArray.get(i).image(pic,10,10,100,90);
       motifArray.get(i).image(pic2,10,50,50,50);
       motifArray.get(i).endDraw();
    }
    return motifArray;
}
}

任何帮助都是极好的。谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-29 07:28:34

在PGraphicsMaker的构造函数中,声明一个局部变量parent并将pa赋值给它。

代码语言:javascript
复制
PApplet parent = pa;

..。当我确定您打算将其赋给实例变量parent时

代码语言:javascript
复制
parent = pa;

在您的代码中,实例变量parent保持为null,直到它被访问,并产生NullPointerException。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17375005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档