NullPointerException (createGraphics)

I'm getting a NPE in my code, but i fail to see what i'm doing wrong.

NPE@line 7

class Card {
  String id;
  int amount;
  int colour;
  int shape;
  int fill;
  PGraphics face = createGraphics(100,100);
  int xPos;
  int yPos;
  Boolean selected = false;
  Card (int preamount, int precolour, int preshape, int prefill){
    amount = preamount;
    colour = precolour;
    shape = preshape;
    fill = prefill;
    id = str(preamount)+str(precolour)+str(preshape)+str(prefill);
//    face = createGraphics(100,100);
    face.beginDraw();
    face.background(0,0);
    face.noStroke();
    face.fill(255);
    face.rect(2,2,96,96,10);
    face.stroke(colours[colour]);
    face.fill(colours[colour],pow(15,fill));
    int drawing = (amount << 2)|shape;
    switch(drawing){
      case 0:
        face.triangle(50,20,80,80,20,80);
        break;
      case 1:
        face.ellipse(50,50,80,60);
        break;
      case 2:
        face.rect(20,40,60,20);
        break;
      case 4:
        face.triangle(15,15,55,15,35,55);
        face.triangle(85,85,45,85,65,45);
        break;
      case 5:
        face.ellipse(50,30,60,30);
        face.ellipse(50,70,60,30);
        break;
      case 6:
        face.rect(20,25,60,20);
        face.rect(20,55,60,20);
        break;
      case 8:
        face.triangle(15,15,45,15,30,45);
        face.triangle(55,15,85,15,70,45);
        face.triangle(35,55,65,55,50,85);
        break;
      case 9:
        face.ellipse(35,20,60,30);
        face.ellipse(50,50,60,30);
        face.ellipse(65,80,60,30);
        break;
      case 10:
        face.rect(20,10,60,20);
        face.rect(20,40,60,20);
        face.rect(20,70,60,20);
        break;   
    }
    face.endDraw();   
  }
}

As you can see, I've tried moving the createGraphics() to the same line that I define it at, but that throws me the same error.

Answers

  • I don' see any obvious problems within your class. Where is the rest of the code? I suppose the problem starts there.

  • edited October 2015

    Here's the rest:

    ArrayList<Card> deck;//cards in the deck
    ArrayList<Card> table;//cards on the table
    ArrayList<Card> stack;//selected cards
    color[] colours = new color[3];//the three colours
    int score = 0;//sets found
    Boolean gameEnded = false;//self-explanatory
    
    void setup(){
      size(400,500);
      init();
    }
    
    void draw(){}
    
    void init(){
      generateColours();
      generateCards();
      drawCards(12);
    }
    
    void generateCards(){
      //makes every possible card and adds it to the deck
      deck = new ArrayList<Card>();
      table = new ArrayList<Card>();
      for(int w = 0; w<3; w++){
        for(int x = 0; x<3; x++){
          for(int y = 0; y<3; y++){
            for(int z = 0; z<3; z++){  
              deck.add(new Card(w,x,y,z));
            }
          }
        }
      }
    }
    
    void generateColours(){
      colours[0] = #FF0000;
      colours[1] = #00FF00;
      colours[2] = #0000FF;
    }
    
    void drawCards(int i){
    //removes a card from a random position in the deck and adds it to the table, repeats i times, then redraws the table, feeding 0 just redraws
      for(int x = 0; x<i; x++){
        table.add(deck.remove(floor(random(deck.size()))));
      }
      background(0);
      int x = 0;
      int y = 0;
      for(Card card: table){
        image(card.face.get(),50+100*x,50+100*y);
        card.xPos = 50+100*x;
        card.yPos = 50+100*y;
        x++;
        if(x==3){
          x = 0;
          y++;
        }
      }
      fill(255);
      if(gameValid()){
    //makes sure that there's a valid set on the table
        textAlign(LEFT);
        text("Your score is: "+score,0,height-textDescent());
        textAlign(RIGHT);
        text("Cards in deck: "+deck.size(),width,height-textDescent());
      }else{
        gameEnd();
      }
    }
    
    void keyPressed(){
      if(gameEnded){exit();}
    }
    
    void mousePressed(){
      if(gameEnded){exit();}
      if(mouseButton==LEFT){
        int x = mouseX;
        int y = mouseY;
        for(Card card: table){
          Boolean xHit = card.xPos+2<x&&x<card.xPos+96;
          Boolean yHit = card.yPos+2<y&&y<card.yPos+96;
          if(xHit&&yHit){
            card.selected =! card.selected;
            break;
            }
        }
        select();
        highlight();
      }
    }
    
    void select(){
      stack = new ArrayList<Card>();
      for(Card card: table){
        if(card.selected){
          stack.add(card);
        }
      }
      if(stack.size()==3){
        for(Card card: table){
          card.selected = false;
        }
        if(sets(stack)){
          table.removeAll(stack);
          score++;
          drawCards(min(3,deck.size()));
        }
      }
    }
    
    Boolean gameValid(){
      return sets(table);
    
    }
    
    Boolean sets(ArrayList<Card> group){
      Boolean hasSet = false;
      for(Card card1: group){
        for(Card card2: group){
          for(Card card3: group){
            if(card1!=card2&&card2!=card3&&card1!=card3){
              Boolean amountSet = (card1.amount==card2.amount&&card3.amount==card2.amount)||(card1.amount!=card2.amount&&card2.amount!=card3.amount&&card1.amount!=card3.amount);
              Boolean colourSet = (card1.colour==card2.colour&&card3.colour==card2.colour)||(card1.colour!=card2.colour&&card2.colour!=card3.colour&&card1.colour!=card3.colour);
              Boolean shapeSet = (card1.shape==card2.shape&&card3.shape==card2.shape)||(card1.shape!=card2.shape&&card2.shape!=card3.shape&&card1.shape!=card3.shape);
              Boolean fillSet = (card1.fill==card2.fill&&card3.fill==card2.fill)||(card1.fill!=card2.fill&&card2.fill!=card3.fill&&card1.fill!=card3.fill);
              Boolean set = amountSet&&colourSet&&shapeSet&&fillSet;
              if(set){
                hasSet = true;
              }
            }
          }
        }
      }
      return hasSet;
    }
    
    void highlight(){
      for(Card card: table){
        fill(0);
        noStroke();
        rect(card.xPos,card.yPos,100,100,10);
        image(card.face.get(),card.xPos,card.yPos);
        if(card.selected){
          fill(0xAA4444FF);
          rect(card.xPos,card.yPos,100,100,10);
        }
      }
    }
    
    void gameEnd(){
      background(0);
      textSize(20);
      line(0,height/2,width,height/2);
      textAlign(CENTER,CENTER);
      if(score>0){
        text("Wow, you only managed to get "+score+" sets? I'm honestly considering deleting myself right now.\n\n\nPress any key to admit defeat.",5,5,width-10,height-10);
      }else{
    //    init();
    //    return;
      }
      gameEnded = true;
    }
    
  • By the way, I'm using processing 2.2.1, if that matters

  • Answer ✓

    You should not name your function "init", this seems to be used by processing 2.2 internally. Renaming it should fix your problem. (In 3x it actually works..)

  • Huh. Usually they mark internal functions as blue, but I'll try that.

  • That seems to have fixed it, thanks!

Sign In or Register to comment.