"GUITAR.jpg"image is in data folder for this sketch. Can anyone explain NullPointerException error?

edited August 2014 in Questions about Code
int y=300;
int vertex = 800;
int j=600;
PImage GUITAR;


void setup() {
  size(2200, 1600);
  background(80, 185, 255, 3);
  drawNeighborhood();
  GUITAR = loadImage("data/GUITAR.jpg");
  imageMode(CENTER);
}

void draw() {
  /*drawHouse(400,600);
   drawBlock(600);*/
}

void drawHouse(float x, int y) {
  strokeWeight(1);
  smooth();

  beginShape();
  fill(random(150, 250));
  vertex (x, y-100);
  vertex (x, y+50);//400,450
  vertex (x-300, y+50);
  vertex (x-300, y-100);
  vertex (x-150, y-250);
  vertex (x-90, y-180);
  vertex (x-90, y-220);
  vertex(x-40, y-220);
  vertex(x-40, y-145);//360,355
  vertex(x, y-100);//400,300
  endShape();
  strokeWeight(4);
  line(x-88, y-200, x-42, y-200);
  image(GUITAR, x, y);
}

void drawBlock(int y) {
  int xSpace=250;
  fill(255);



  if (y==300) {
    xSpace=xSpace;
  } else if (y==500) {
    xSpace+=xSpace;
  } else if (y==900) {
    xSpace+=xSpace/2;
  } else {
    xSpace=xSpace-150;
  }
  for (int x=400; x<=2200; x+=xSpace) {
    drawHouse(random(300, x), y);
  }
  fill(random(50, 100));
  strokeWeight(0);
  rect(0, y+50, width, 30);
}

void drawNeighborhood() {

  for (int y=300; y<=1600; y+=200) {

    drawBlock(y);
  }
}

Comments

  • Sorry obviously I'm a newbie! Don't know how to apply markdown to format above code properly!

  • Highlight the code and press Ctrl +k

  • Use loadImage("GUITAR.jpg") you dont need to specify the data folder Processing does that for you

  • edited September 2014

    Thanks for your response! I eliminated the data folder and get the same error message: Any other ideas?

    int y=300;
    int vertex = 800;
    int j=600;
    PImage GUITAR;
    
    
    void setup() {
      size(2200, 1600);
      background(80, 185, 255, 3);
      drawNeighborhood();
      GUITAR = loadImage("GUITAR.jpg");
      imageMode(CENTER);
    }
    
    void draw() {
      /*drawHouse(400,600);
       drawBlock(600);*/
    }
    
    void drawHouse(float x, int y) {
      strokeWeight(1);
      smooth();
    
      beginShape();
      fill(random(150, 250));
      vertex (x, y-100);
      vertex (x, y+50);//400,450
      vertex (x-300, y+50);
      vertex (x-300, y-100);
      vertex (x-150, y-250);
      vertex (x-90, y-180);
      vertex (x-90, y-220);
      vertex(x-40, y-220);
      vertex(x-40, y-145);//360,355
      vertex(x, y-100);//400,300
      endShape();
      strokeWeight(4);
      line(x-88, y-200, x-42, y-200);
      image(GUITAR, x, y);
    }
    
    void drawBlock(int y) {
      int xSpace=250;
      fill(255);
    
    
    
      if (y==300) {
        xSpace=xSpace;
      } else if (y==500) {
        xSpace+=xSpace;
      } else if (y==900) {
        xSpace+=xSpace/2;
      } else {
        xSpace=xSpace-150;
      }
      for (int x=400; x<=2200; x+=xSpace) {
        drawHouse(random(300, x), y);
      }
      fill(random(50, 100));
      strokeWeight(0);
      rect(0, y+50, width, 30);
    }
    
    void drawNeighborhood() {
    
      for (int y=300; y<=1600; y+=200) {
    
        drawBlock(y);
      }
    }
    
  • edited August 2014

    Copied and pasted for Markdown:

    int y=300;
    int vertex = 800;
    int j=600;
    PImage GUITAR;
    
    
    void setup() {
      size(2200, 1600);
      background(80, 185, 255, 3);
      drawNeighborhood();
      GUITAR = loadImage("GUITAR.jpg");
      imageMode(CENTER);
    }
    
    void draw() {
      /*drawHouse(400,600);
       drawBlock(600);*/
    }
    
    void drawHouse(float x, int y) {
      strokeWeight(1);
      smooth();
    
      beginShape();
      fill(random(150, 250));
      vertex (x, y-100);
      vertex (x, y+50);//400,450
      vertex (x-300, y+50);
      vertex (x-300, y-100);
      vertex (x-150, y-250);
      vertex (x-90, y-180);
      vertex (x-90, y-220);
      vertex(x-40, y-220);
      vertex(x-40, y-145);//360,355
      vertex(x, y-100);//400,300
      endShape();
      strokeWeight(4);
      line(x-88, y-200, x-42, y-200);
      image(GUITAR, x, y);
    }
    
    void drawBlock(int y) {
      int xSpace=250;
      fill(255);
    
    
    
      if (y==300) {
        xSpace=xSpace;
      } else if (y==500) {
        xSpace+=xSpace;
      } else if (y==900) {
        xSpace+=xSpace/2;
      } else {
        xSpace=xSpace-150;
      }
      for (int x=400; x<=2200; x+=xSpace) {
        drawHouse(random(300, x), y);
      }
      fill(random(50, 100));
      strokeWeight(0);
      rect(0, y+50, width, 30);
    }
    
    void drawNeighborhood() {
    
      for (int y=300; y<=1600; y+=200) {
    
        drawBlock(y);
      }
    }
    
  • ^ mod edit.

  • ok, think about it.

    line 10 drawNeighborhood

    in drawHeighborhood() line 69 drawBlock

    in drawBlock() line 58 drawHouse

    in drawHouse() line 38 use the guitar image

    but you only load the image in line 11, which is after line 10.

  • edited September 2014

    Thanks for your response! Sorry for my delay...

    I moved the image to load in line... 9 (?) ...If you could also PLEASE explain to me how to prepare the code for Markdown, I'd be MUCH appreciative!

    Anyway when I load the image earlier, I still get a "NullPointerException" error.

    Thanks again for your help. I must be making silly mistakes. I'm a beginner!

    int y=300;
    int vertex = 800;
    int j=600;
    PImage GUITAR;
    
    
    void setup() {
      size(2200, 1600);
      background(80, 185, 255, 3);
      GUITAR = loadImage("GUITAR.jpg");
      drawNeighborhood();
    
      imageMode(CENTER);
    }
    
    void draw() {
      /*drawHouse(400,600);
       drawBlock(600);*/
    }
    
    void drawHouse(float x, int y) {
    
      strokeWeight(1);
      smooth();
    
      beginShape();
      fill(random(150, 250));
      vertex (x, y-100);
      vertex (x, y+50);//400,450
      vertex (x-300, y+50);
      vertex (x-300, y-100);
      vertex (x-150, y-250);
      vertex (x-90, y-180);
      vertex (x-90, y-220);
      vertex(x-40, y-220);
      vertex(x-40, y-145);//360,355
      vertex(x, y-100);//400,300
      endShape();
      strokeWeight(4);
      line(x-88, y-200, x-42, y-200);
      image(GUITAR, x, y);
    }
    
    void drawBlock(int y) {
      int xSpace=250;
      fill(255);
    
    
    
      if (y==300) {
        xSpace=xSpace;
      } else if (y==500) {
        xSpace+=xSpace;
      } else if (y==900) {
        xSpace+=xSpace/2;
      } else {
        xSpace=xSpace-150;
      }
      for (int x=400; x<=2200; x+=xSpace) {
        drawHouse(random(300, x), y);
      }
      fill(random(50, 100));
      strokeWeight(0);
      rect(0, y+50, width, 30);
    }
    
    void drawNeighborhood() {
    
      for (int y=300; y<=1600; y+=200) {
    
        drawBlock(y);
      }
    }
    
  • ".If you could also PLEASE explain to me how to prepare the code for Markdown, I'd be MUCH appreciative!"
    Well, quark just told you that some messages ago.

    Also see To newcomers in this forum: read attentively these instructions

    And you can edit your messages.

  • if you have an NPE there will be an error message in the bottom pane of the PDE please copy and paste it in this forum.

  • you can edit your last post....

    How to post code

    when you post your code:

    • in the editor hit ctrl-t to auto format

    • copy it

    • paste it in the browser

    • leave 2 empty lines before it, 1 line after it

    • mark the code (without the 3 empty lines)

    • press C in the small command bar.

  • edited September 2014

    GUITAR = loadImage("GUITAR.jpg");

    the file name on the hard drive must be exactly like here, even small / capital letters....

    • Guitar.jpg won't work

    • GUITAR.JPG won't work

  • edited September 2014

    attempting

    int y=300;
    int vertex = 800;
    int j=600;
    PImage GUITAR;
    
    void setup() {
        size(2200, 1600);
        background(80, 185, 255, 3);
        GUITAR = loadImage("GUITAR.jpg");
        drawNeighborhood();
        imageMode(CENTER);
    }
    
    void draw() {
        /*drawHouse(400,600);
        drawBlock(600);*/
    }
    
    void drawHouse(float x, int y) {
    
        strokeWeight(1);
        smooth();
    
        beginShape();
        fill(random(150, 250));
        vertex (x, y-100);
        vertex (x, y+50);//400,450
        vertex (x-300, y+50);
        vertex (x-300, y-100);
        vertex (x-150, y-250);
        vertex (x-90, y-180);
        vertex (x-90, y-220);
        vertex(x-40, y-220);
        vertex(x-40, y-145);//360,355
        vertex(x, y-100);//400,300
        endShape();
        strokeWeight(4);
        line(x-88, y-200, x-42, y-200);
        image(GUITAR, x, y);
    }
    
  • How to post code

    when you post your code:

    • in the editor hit ctrl-t to auto format

    • copy it

    • paste it in the browser

    • leave 2 empty lines before it, 1 line after it

    • mark the code (without the 3 empty lines)

    • press C in the small command bar.

  • edited September 2014

    just try

    PImage GUITAR;
    
    size(2200, 1600);
    
    background(80, 185, 255, 3);
    GUITAR = loadImage("GUITAR.jpg");
    image(GUITAR, 0, 0);
    

    Does it work??

    ;-)

  • Hello Quark: There is no text below the error NullPointerException.

  • can you post the entire console window?

  • When showing the error, the PDE should highlight a line. Tell us which one it is (I guess it is 39).

    Do you have a file named GUITAR.jpg (exact case) in the data folder of your sketch?

    As said, paste here the content of the console.

  • Is there a data folder in sketch folder?

    Hit ctrl-k in the ide

  • Here is a screen shot of the entire editor and console (only excluding last closing curly bracket): Hope this works for anyone trying to check it out. There is a data folder in the sketch folder and GUITAR.jpg is in the data folder.

    two things: •Thanks Chrisir for the simple image exercise which DOES work for me! •Also, as I mentioned way back, I'm trying to learn how to use functions and incorporate images, which is the point of this sketch to begin with. So the image isn't sized properly, etc. •Please pretend emoticon at bottom isn't there. Couldn't put it where I wanted it!

    %22GUITAR%22 console =D>

  • In setup load image before drawing neighbourhood

  • Also try x,y both float

  • Don't use vertex etc. as a name for a var

  • This is cool. Don't think I need to know why it works. I'll just remember to load image before function.

    BUT what I wanted was to put the image in each house...?

    ALSO thanks so much for your help with Markdown, but I don't know how to "mark the code" (your second to last line).

    Thanks Again.

  • You really need to know why this works.... That's learning

    You can't use the var GUITAR when there is no image in it - so you need to load first

    Koogs told you that btw - why didn't it work back then??

  • ok, getter closer... but imageMode is set at (CENTER). What is the image centered on? Or how could I, for example, have the image be entirely inside the house and line up with the right side of the house?

  • About Koogs, you're right, good question. I did try then to move the image up before drawNeighborhood. I didn't save it so I don't know what else was going on with it at the time. And thank you, I do actually know its important to know WHY anything works and in this case you've really helped me understand this in the context of a specific problem.

  • code is there on Aug. 30. Problem was I moved loadImage up without imageMode. It looks like I'm being sloppy but its more that I don't understand basic concepts yet. (Like the centerMode image within shape defined by vertices... probably a math problem but I'm not sure how processing is choosing to center it where its centered right now.)

  • In setup imageMode too must be before drawing the neighbourhood to make it work.

    Without centered when drawing an image x,y is the upper left corner

    With centered x.y is the center of the image (so that half of its width is left from the x-value, the other half right of it)

  • edited September 2014

    imageMode CENTER refers to the images, such as

    image(GUITAR, 0, 0);

    just test it a in a separate small sketch.

    imageMode CENTER does not refer to shapes though. That might be your current issue.

    So your house is not centered around x,y

    The way you draw it, it is mostly left from x,y since you say

    vertex (x-300, y+50);

    etc. which makes points far left from x.

    To have the guitar centered in the house you can say

    image(GUITAR, x-150, y-60);

    ;-)

  • edited September 2014

    this draws one house only

    please note the red dot, that's your x,y for that house - the house is all left from it.

    It's very good for testing to look at one house only.

    int y=300;
    int vertex = 800;
    int j=600;
    PImage GUITAR;
    
    void setup() {
      size(2200, 1600);
      background(80, 185, 255, 3);
      GUITAR = loadImage("GUITAR.jpg");
      imageMode(CENTER);
      // drawNeighborhood();
      drawHouse(500, 300);
    
      // test 
      fill(255, 2, 2);
      noStroke();
      ellipse(500, 300, 7, 7);
    }
    
    void draw() {
      /*drawHouse(400,600);
       drawBlock(600);*/
    }
    
    void drawHouse(float x, int y) {
    
      strokeWeight(1);
      smooth();
    
      beginShape();
      fill(random(150, 250));
      vertex (x, y-100);
      vertex (x, y+50);//400,450
      vertex (x-300, y+50);
      vertex (x-300, y-100);
      vertex (x-150, y-250);
      vertex (x-90, y-180);
      vertex (x-90, y-220);
      vertex(x-40, y-220);
      vertex(x-40, y-145);//360,355
      vertex(x, y-100);//400,300
      endShape();
      strokeWeight(4);
      line(x-88, y-200, x-42, y-200);
      image(GUITAR, x-150, y-60);
    }
    
    void drawBlock(int y) {
      int xSpace=250;
      fill(255);
    
    
    
      if (y==300) {
        xSpace=xSpace;
      } 
      else if (y==500) {
        xSpace+=xSpace;
      } 
      else if (y==900) {
        xSpace+=xSpace/2;
      } 
      else {
        xSpace=xSpace-150;
      }
      for (int x=400; x<=2200; x+=xSpace) {
        drawHouse(random(300, x), y);
      }
      fill(random(50, 100));
      strokeWeight(0);
      rect(0, y+50, width, 30);
    }
    
    void drawNeighborhood() {
    
      for (int y=300; y<=330; y+=200) {  //   for (int y=300; y<=1600; y+=200) {
    
    
        drawBlock(y);
      }
    }
    //
    
  • full neighboorhood

    int y=300;
    int vertex = 800;
    int j=600;
    PImage GUITAR;
    
    void setup() {
      size(2200, 1600);
      background(80, 185, 255, 3);
      GUITAR = loadImage("GUITAR.jpg");
      imageMode(CENTER);
      drawNeighborhood();
      // drawHouse(500, 300);
    
      // test 
      //  fill(255, 2, 2);
      //  noStroke();
      //  ellipse(500, 300, 7, 7);
    }
    
    void draw() {
      /*drawHouse(400,600);
       drawBlock(600);*/
    }
    
    void drawHouse(float x, int y) {
    
      strokeWeight(1);
      smooth();
    
      beginShape();
      fill(random(150, 250));
      vertex (x, y-100);
      vertex (x, y+50);//400,450
      vertex (x-300, y+50);
      vertex (x-300, y-100);
      vertex (x-150, y-250);
      vertex (x-90, y-180);
      vertex (x-90, y-220);
      vertex(x-40, y-220);
      vertex(x-40, y-145);//360,355
      vertex(x, y-100);//400,300
      endShape();
      strokeWeight(4);
      line(x-88, y-200, x-42, y-200);
      image(GUITAR, x-150, y-60);
    }
    
    void drawBlock(int y) {
      int xSpace=250;
      fill(255);
    
      if (y==300) {
        xSpace=xSpace;
      } 
      else if (y==500) {
        xSpace+=xSpace;
      } 
      else if (y==900) {
        xSpace+=xSpace/2;
      } 
      else {
        xSpace=xSpace-150;
      }
      for (int x=400; x<=2200; x+=xSpace) {
        drawHouse(random(300, x), y);
      }
      fill(random(50, 100));
      strokeWeight(0);
      rect(0, y+50, width, 30);
    }
    
    void drawNeighborhood() {
      for (int y=300; y<=1600; y+=200) {  //   for (int y=300; y<=1600; y+=200) {
        drawBlock(y);
      }
    }
    //
    
  • Thanks for this Chrisir! I'm excited to get this to work. But I'm not sure how you determined the x,y values for the single house tested. I guess any y value on that line would have worked for the purposes of the test?

        //test 
            fill(255, 2, 2);
            noStroke();
            ellipse(500, 300, 7, 7);
    

    There are 2 more things I'd like to try with this. One is to have a series of images appear i different houses randomly, which I think means I'll have to review arrays. I'd also like to now make a city of neighborhoods which will involve the challenge of manipulating scale within the functions. So you'll be hearing from me again soon, I'm sure.

    This post is also meant to test whether or not I finally have Markdown worked out...

  • edited September 2014

    everything you say is correct...

    the x,y value in single house is of course just a test without meaning

    for different images use array and random()

    also for scaling houses use scale()

    see reference for all

    ;-)

  • Thanks so much for being so helpful!

Sign In or Register to comment.