0

This might be an extremely stupid question, but I keep on getting a NullPointer.

In my Android app I've got a class called MyMetadata, and I want to fill a List using objects of this class. So I try to do so as follows:

List<MyMetadata> metadataList;
MyMetadata myMetadata = new MyMetadata("first string", "second string");
Log.i(this, myMetadata.toString());
metadataList.add(myMetadata);

The Log.i() displays a line saying com.mycompany.model.MyMetadata@41af34301, so it does exist. But when I try to add it to the list I get a NullPointerException.

Does anybody know what I'm doing wrong here?

7 Answers 7

7

You have not initialized your list. So, without initializing if you are trying to work around with an object it will end you up in Null pointer exception.

Try to initialize your list..

metadataList = new ArrayList<MyMetadata>();
Sign up to request clarification or add additional context in comments.

Comments

3

You are getting a NullPointerException because no space is allocated to the list and the add function is not able to find any pointer to the same.

Initialise the list before you use it and it will solve the issue:

metadatalist = new ArrayList '<'MyMetadata'>'();

Please remove the single quotes in the above code around '><'. Somehow it wasn't showing up in the post without them.

Comments

1

Your metadataList list is still uninitialized and thus is null by default. You need to initialize it before adding any elements to it. That's the reason you get the NPE.

List<MyMetadata> metadataList; // uninitialized
...
metadataList.add(myMetadata); // adding an element to an uninitialized will throw a NPE

Initialize it like this

List<MyMetadata> metadataList = new ArrayList<>(); // initialized now, example
...
metadataList.add(myMetadata);

Comments

1

Your list is not initialized...

List<MyMetadata> metadataList = new ArrayList<Metadata>();

ArrayList is one of the implementation of List interface, you can also initialize it as LinkedList, Vectors etc..

For complete list read http://docs.oracle.com/javase/7/docs/api/java/util/List.html

Comments

1

This happened because you forgot to initialize List metadataList. Initialize it with any List implemented class Ex: List metadataList=new ArrayList();

Happy coding :)

Comments

1

firstly you needed to create a collection by initializing your variable ( metadataList )with ArrayList or other as your requirement then you can use it

   List<MyMetadata> metadataList=ArrayList<MyMetadata>();
   MyMetadata myMetadata = new MyMetadata("first string", "second string");
   Log.i(this, myMetadata.toString());
   metadataList.add(myMetadata);

Comments

1
metadataList.add(myMetadata);

The above line is throwing you a NPE, your above line looks like null.add(myMetadata) before adding objects into a list you have to initialize your list something like this metadataList = new ArrayList<MyMetadata>(); then you can do this metadataList.add(myMetadata);

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.