0

Okay, so I have this code below and I keep getting run-time errors and I'm thinking its a flaw in the codes logic. I'm trying to use the setOneOtherPicture method to pick a picture and set it into an array to be later called on to be displayed in the showArtCollection method. I've been given two parameters, which and pRef. Can someone help me with this? Thanks.

  public class House
{
 String owner;
 Picture pRef;
 Picture favPic;
 Picture [] picArray = new Picture [3];

public void showArtCollection ()
  {

  ArtWall aWall = new ArtWall(600,600);
  aWall.copyPictureIntoWhere(favPic,250,100);
  aWall.copyPictureIntoWhere(pRef,51,330);
  aWall.copyPictureIntoWhere(pRef,151,330);
  aWall.copyPictureIntoWhere(pRef,351,280);

  aWall.show();

 }

public void setOneOtherPicture (int which, Picture pRef)
 {

 this.picArray [which] = new Picture (FileChooser.pickAFile ());
 }

  public static void main (String [] args)
   {
     House PhDsHouse = new House ("Mad PH.D.");
     Picture favPic = new Picture ();
     Picture pRef = new Picture ();
     PhDsHouse.setOneOtherPicture (0, pRef);
     PhDsHouse.setOneOtherPicture (1, pRef);
     PhDsHouse.setOneOtherPicture (2,pRef);
     PhDsHouse.showArtCollection ();
   }
7
  • What exceptions? Please post the entire Exception message. What lines of code are involved? The exception message should tell you this, and you will then have to indicate this to us via a comment in your code or something similar. Commented May 4, 2013 at 22:54
  • This question is very similar to stackoverflow.com/questions/16377626/… Commented May 4, 2013 at 22:57
  • Is this a homework assignment? Commented May 4, 2013 at 22:58
  • @Bill: it has to be homework. I wonder if we have one user with two identities? That's not a good thing and the mods if they find this out can ban both. Commented May 4, 2013 at 22:59
  • @HovercraftFullOfEels sorry, I forgot one bit of code, I just fixed it now. Its giving me a nullPointerException on 'PhDsHouse.setOneOtherPicture (0, pRef);' Commented May 4, 2013 at 22:59

2 Answers 2

1

Your House class has several fields, and your main method has local variables with the same names. Perhaps these should be sent into the constructor? Otherwise, these fields are null, which causes the crash in the showArtHouse method.

Sign up to request clarification or add additional context in comments.

Comments

0

this method:

public void setOneOtherPicture (int which, Picture pRef)
{
 this.picArray [which] = new Picture (FileChooser.pickAFile ());
}

shouldn't be calling FileChooser, and it shouldn't even be creating a new Picture object, but instead should just be putting pRef Picture object that you already have passed into the method into the array. Otherwise you're just throwing the pRef parameter away -- makes no sense.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.