0

FavPic is an array with a picture element stored in it, im trying to set the variable, pRef, equal to that array element. Instead, I keep getting Null Pointer Exception crashes. What exactly is a Null Pointer exception, and how would these few lines of code cause such a thing? Thanks!

   public void setFavouritePicture (Picture pRef)
  {
    pRef = favPic[0];
   }
Alex
  • 21
  • 2
  • It means that you never initialized the array. – Justin Niessner May 03 '13 at 16:59
  • Initialize the array by using `favpic = new Picture[size];` – Rahul Bobhate May 03 '13 at 17:00
  • 1
    Well it sounds very much like `favPic` is actually a null reference, if that line is failing. Unfortunately you've given us so little context that there's no much more we can say. Note that changing the value of `pRef` wouldn't do anything anyway. Shouldn't you be setting `favPic[0]` to `pRef`, given that this is a *set* method? – Jon Skeet May 03 '13 at 17:00

4 Answers4

0

A NullPointerException means that you attempted to invoke a method on an object reference that is referring to null (more common), or like here, attempted to reference a specific element of an array that is referring to null.

It only takes 2 lines of code to cause a NullPointerException -- one to declare a reference variable so that it's null, and one to attempt to do something with it.

You must create your array to initialize it before using it.

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • I did initialise it which is the weird thing. http://pastebin.com/kcGWZWgt thats the code im working on – Alex May 03 '13 at 17:10
  • Along with the code, please add a stack trace for the NullPointerException. We don't have the code of other classes you use, which makes it difficult to tell what's the problem (for instance, where is the call to setFavoritePic()?). – Elist Jun 23 '13 at 08:36
0

A NullPointerException is called when something is null, or undefined. You are either trying to get a value from an undeclared array, or you are trying to get a value from an array that hasn't been defined.

For example, if I have an int[] that contains 1, 2, and 3, and I try to say System.out.println(int[3]), it won't work because there is no value.

Run the following checks and let me know what you get:

System.out.println(favPic == null);

System.out.println(favPic[0] == null);

nrubin29
  • 1,522
  • 5
  • 25
  • 53
  • in my code, i believe I initialised the array. Feel free to take a look if you'd like http://pastebin.com/kcGWZWgt – Alex May 03 '13 at 17:15
  • In your `setFavouritePic` method, add those two lines of code and tell me what you get. We are looking for two falses. – nrubin29 May 03 '13 at 17:25
0

When you try to access an object which value is null (it may not be property initialized or it may be manually assigned the value null).

In your case, the array favPic may be null so when you try to access the first value of favPic using favPic[0], it fails and throw NullPointerException.

zhuwenger
  • 111
  • 4
0

In your main, you should have something like this

Picture[] favPic = new Picture [1];// 1 is the number of pics
favPic = setFavouritePicture(favPic);

and your setFavouritePicture method should look something like this

public static Picture[] setFavouritePicture (Picture[] pRef){
    for (int i = 0; i < pRef.length; i++) {
        pRef[i] = new Picture();
    }
    pRef[0] = ... ; // "..." is your picture
    return pRef;
}
Arshi
  • 1
  • 1