简体   繁体   中英

What is this java.lang.NullPointerException?

I coded a short script to learn about Java array:

class Dog {
  String name;
  void bark() {
    System.out.println(name + " is barking");
  }

  void eat() {
    System.out.println(name + " is eating");
  }
}

class mypets {
    public static void main(String [] args) {

    Dog dogA = new Dog();

    dogA.name="Lassy";
    dogA.bark();
    dogA.eat();

    Dog[] myDogs = new Dog[3];
    myDogs[0].name = "Barney";
    myDogs[0].bark();
  }
}

I then compiled and run my program:

C:\Java>javac mypets.java

C:\Java>java mypets
Lassy is barking
Lassy is eating
Exception in thread "main" java.lang.NullPointerException
        at mypets.main(mypets.java:22)

What did I do wrong? I tried googling for that dreaded "java.lang.NullPointerException" but honestly I am confused right now.

An array is like a row of boxes that can hold objects; when you create an array, though, those boxes are empty. The NullPointerException is Java's way of telling you this. So, when you say

Dog[] myDogs = new Dog[3];
myDogs[0].name = "Barney";

You're trying to set the name of the Dog in the first box, but there is no such Dog -- the box is empty. You need to put a Dog in the box to make this work:

Dog[] myDogs = new Dog[3];
myDogs[0] = new Dog();
myDogs[0].name = "Barney";

Then it will work.

When you initialize an array, each element of the array is assigned a default value. In the case of an object, the default value is null. Invoking methods on a null reference results in a NullPointerException . You must instantiate each Dog using new

Dog[] myDogs = new Dog[3];

Each element of myDogs is assigned null.

for(int i = 0; i < myDogs.length; i ++) {
    myDogs[i] = new Dog();
}

NullPointException basically means you are attempting to reference an object or objects that haven't been initialized yet...

In your code, you are doing...

Dog[] myDogs = new Dog[3];

This will create an array that can contain 3 elements of Dog , it does not create any instances of Dog , so when you do this...

myDogs[0].name = "Barney";
myDogs[0].bark();

myDogs[0] is actually null .

Instead, you need to first allocate a new Dog to the element BEFORE you try to access it.

myDogs[0] = new Dog();
myDogs[0].name = "Barney";
myDogs[0].bark();

You might like to take a read through Arrays of more details

You need to initialise each reference in the Array. Following is the edited code.
See this and this for more info.

 class Dog {
      String name;
      void bark() {
        System.out.println(name + " is barking");
      }

      void eat() {
        System.out.println(name + " is eating");
      }
    }

    class mypets {
        public static void main(String [] args) {

        Dog dogA = new Dog();

        dogA.name="Lassy";
        dogA.bark();
        dogA.eat();

        Dog[] myDogs = new Dog[3];

        //Lines Added
        for(int i=0; i<3; i++)
            myDogs[i] = new Dog();
        //End   

        myDogs[0].name = "Barney";
        myDogs[0].bark();
      }
    }

You need to create objects and put in the array before you can use it.

Like

Dog[] myDogs = new Dog[3];
myDogs[0] = new Dog();
myDogs[0].name = "Barney";
myDogs[0].bark();

NullPointerException says that it is an exception that you (your reference variable) are pointing to null (object), and yet you intend to do some operation on that (which should be wrong).

In simplest terms, if you declare a variable and do not assign an object it points to null

MyClass myRef;//equivalent to MyClass myRef = null;

and then try to do some operation on it

myRef.doSomething(); //it's a null pointer exception

then you get NullPointerException

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM