1

This is the class that creates the objects:

class cards_type{

String color;
String number;

public cards_type(final String input_color, final String input_number) {

    color = input_color;
    number = input_number;
}

}

And this are the arraylist:

ArrayList<ArrayList>players_cards = new ArrayList<ArrayList>();
players_cards.add(new ArrayList<cards_type>());

When I add the values and print it with this code, it returns the object:

System.out.println(players_cards.get(0).get(0));

OUT: cards_type@ea4a92b

But if I instance a property it returns a mistake like if it wasnt a object:

System.out.println(players_cards.get(0).get(0).color);

OUT: Exception in thread "main" java.lang.Error: Unresolved compilation problem: color cannot be resolved or is not a field

Thanks for help

3
  • 1
    Either make the color field public -not recommended- or add a getter and use the getter Commented Jul 31, 2020 at 8:02
  • There is error in the statement you have written, System.out.println(players_cards.get(0).get(0)); You just added the arrayList of cards_type ,but you did not initialize it. So second get(0) will give you null pointer exception Commented Jul 31, 2020 at 8:07
  • Also best practice in Java is write class name with big CamelCase so 'CardsType' would be better Commented Jul 31, 2020 at 8:10

1 Answer 1

1

This is how you should modify your code to make it work.

The way you initialized the ArrayList of ArrayList was wrong. You should always provide the type of the data you want to store in the arrayList.

ArrayList<ArrayList<cards_type>> players_cards = new ArrayList<ArrayList<cards_type>>();

CardTypes class:

class cards_type {

    String color;
    String number;

    public cards_type(String input_color, String input_number) {

        this.color = input_color;
        this.number = input_number;
    }
}

Test Class :

Class TestClass{
     public static void main(String[] args){
        ArrayList<ArrayList<cards_type>> players_cards = new 
  ArrayList<ArrayList<cards_type>>();
                cards_type pc = new cards_type("red","4");
                cards_type pc1 = new cards_type("black","2");
                cards_type pc2 = new cards_type("red","3");
                
                ArrayList<cards_type> al = new ArrayList<>();
                al.add(pc);
                al.add(pc1);
                al.add(pc2);
                
                players_cards.add(al);
                
                System.out.println(players_cards.get(0).get(0).color);
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

@scotby, great it helped you . I hope you wont mind accepting the answer and upvoting for better visibility
Cant upvote cause I need 2 more points of reputation, I just accepted the answer, thank you again for your help.

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.