3

mainMethod.java

public class mainMethod{
    public animalsData[] animals;

    public mainMethod(){
        animals[this.animals.length + 1] = new animalsData("CAT", "4");

    }

    public static void main(String[] args) {
        mainMethod run = new mainMethod();
    }
}

animalsData.java

public class animalsData{
    String name, l;

    public animalsData(String name, String l) {
        super();
        this.name= name;
        this.l= l;
    }
}

I hava this problem: Exception in thread "main" java.lang.NullPointerException

4
  • 1
    animals has not been assigned a value. Now that you know what the problem is, please delete and/or help me close this question. (P.S. there are many such related questions. Try searching first for "ideas" about what could be wrong.) Commented Aug 31, 2012 at 17:42
  • Where do you initialize animals? Commented Aug 31, 2012 at 17:42
  • 1
    this.animals is null so it's not possible to acces length property Commented Aug 31, 2012 at 17:42
  • 1
    Also, in addition, arr[arr.length + 1] is never valid in Java because arrays have a fixed length. Commented Aug 31, 2012 at 17:44

3 Answers 3

6

You are never initializing your animals array in your mainMethod class.

In your public mainMethod() method, you need to do animals = new animalsData[INITIAL SIZE];

If you want it to grow automatically, you should use a List. Even then you would not use this.animals.length + 1 as its index. you would simply do List.add()

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

2 Comments

it's an array not an object, actually, so he's going about it totally wrong. :-/
@Gray - per the spec, yes. But the OP is obviously new to the language and since you instantiate arrays and objects in different fashions (new Object() versus new int[5]), it's best to stay simple.
4
public List<animalsData> animals = new ArrayList<animalsData>()

public mainMethod(){
    animals.add(new animalsData("CAT", "4"));
}

learn more about Lists in Java, arrays are not expandable by default. And can not expand beyond it's dimension if you add an item after last item of the array.

2 Comments

It is good that you provide an alternate solution, but you should mention what is actually causing the errors he is encountering.
I've seen another answers explaining this, why to reinvent the wheel? :)
1

1. Array must be initialized when its declared.

Eg:

public animalsData[] animals; 


    public static void main(String[] args) {
        mainMethod run = new mainMethod();
        System.out.println(run.animals.length);
    }

The above code gives a NullPointerException, cause animals which is an Object Reference Array Variable is Null and is not assigned the Array Object on the heap.

public animalsData[] animals = new animalsData[10]; 


        public static void main(String[] args) {
            mainMethod run = new mainMethod();
            System.out.println(run.animals.length);
        }

The above code works fine....as the Array is now initialized.

I would recommend you to use java.util.Collections instead of arrays, they are very flexible.

List<animalsData> animals = new ArrayList<animalsData>();

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.