2013-06-28 6 views
1

Eclipse에서 Processing Applet을 Java로 작성하고 있습니다. 주 클래스에서 별도의 클래스로 PGraphics의 ArrayList를 만드는 메소드를 리팩터링 할 때 널 포인터 예외가 계속 발생합니다. main 클래스에서 모두 작성된 메소드 (아래의 첫 번째 코드 샘플)는 작동하지 않는 리팩터 (두 번째 코드 샘플 쌍)입니다. 나는 온갖 해답을 찾아 보았지만 (비슷한 것이지만 일치하지는 않음) 많은 재 작성을했지만 주사위는 없었다. 당신의 도움을 사전에보고하고, 종류 감사를 타고 :PGraphics의 ArrayList 생성을 메인 클래스에서 별도의 클래스로 리팩토링 할 때 널 포인터 예외가 발생했습니다.

이 코드 (하나 개의 클래스에있는 모든 것은) 작동 :

package tester; 

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

public class GraphicsMain extends PApplet{ 
int screenWidth = 800; 
int screenHeight = 500; 
String filepath = "https://stackoverflow.com/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); 
    } 
} 

} 

아래에이 코드 (두 개의 별도의 클래스로 리팩토링)하지 않습니다

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); 
    } 
} 

} 
:

** 나는 두 줄 널 포인터 예외

1 등급에서 확인 된 [각 클래스에서 하나]를 언급했습니다 617,451,515,

클래스 2 :

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 = "https://stackoverflow.com/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; 
} 
} 

어떤 도움을 멋진 것입니다. 감사!

답변

3

PGraphicsMaker에 대한 생성자에서 로컬 변수 parent을 선언하고 pa을 할당합니다.

PApplet parent = pa; 

... 난 당신이 변수 예를parent에 할당하도록 확신 할 때 : 당신의 코드에서

parent = pa; 

, 인스턴스 변수 parent 숙박이 그 때까지 null 액세스, 그리고 a NullPointerException 결과.

+0

고마워 --- 몇 시간 동안 나를 잊어 버렸던 간단한 실수! – gromiczek