2014-01-08 2 views
0
/** 
* 
*/ 
package be.pandapp.ehb; 

이것은 첫 번째 클래스입니다.비 메인 클래스에있는 PApplet 만들기

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

} 

오류는 nullpointer입니다.

죄송합니다. 여기

그것이 :

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

해당합니다 (의 클래스를 초기화 할 때 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); 

이 될해야 : DisplayObject입니다 무엇

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); 
} 
관련 문제