---
title: "Add Object into an ArrayList error using java"  
description: "Add Object into an ArrayList error using java"  
author: "Anonymous User"  
published: 2013-10-22  
updated: 2013-10-22  
canonical: https://www.mindstick.com/forum/1700/add-object-into-an-arraylist-error-using-java  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# Add Object into an ArrayList error using java

I have a [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) using [add](https://www.mindstick.com/forum/12983/add-address-in-textbox-when-page-is-load-and-if-i-search-address-in-textbox-show-in-map-by-javascript) an object of the [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) Fish to an [arraylist](https://www.mindstick.com/articles/11973/arraylist-class-in-c-sharp) call itemList.

**My [Code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii): -**

\

```
public static void main(String[] args)
{
    ItemList i1 = new ItemList("item#1");
}
```

**Below my class and Constructor: -**

####

```
public class ItemList
{
    protected static String name;
    protected static int number;
    protected static List<ItemList> itemList = new ArrayList<ItemList>();
    public ItemList(String in)
    {
        name = in;
        number = 15;
        itemList.add(name, number);
    }
}
```

How do I add [objects](https://www.mindstick.com/forum/145447/what-are-objects) to an arraylist properly?

## Replies

### Reply by Samuel Fernandes

Firstly you shouldn’t be name and number is static (unless you want all the ItemList to have the same name/number but then creating more than 1 instance of that class would be a waste of resources)!

Secondly, change:

```
itemList.add(name, number);
```

To:

```
itemList.add(this);
```

itemList can hold references to objects of type ItemList. If you try to add "name, number" Java doesn't know you mean a ItemList:-)

this points to the objects that is currently being created in the constructor.


---

Original Source: https://www.mindstick.com/forum/1700/add-object-into-an-arraylist-error-using-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
