0

Hello I am trying to access an array from another class I have the code written but there seems to be a error and I can't figure out what is wrong with it (This is in Java)

Here is the first class

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

public class Main {
    private static List<String> Deck;

    public static void ArrayList() {// Method to create the deck
        Deck = new ArrayList<String>();
        int i = 1;// counter variable for loops

        ArrayList <String> Clubs = new ArrayList <String>();// ArrayList for all clubs cards
        while (i < 11) {// while loop to put each number in up to 10
            String c = String.valueOf(i);
            c = c + "C";
            Clubs.add(c);// adds the card
            i++;
            // each increases so each card is different so ace first time through then 2 then 3....
        }
        ArrayList <String> Spades = new ArrayList <String>();// same same for spades
        while (i > 1) {
            String s = String.valueOf(i);
            s = s + "S";
            Spades.add(s);
            i--;

        }
        ArrayList <String> Hearts = new ArrayList <String>();// same same for hearts
        while (i < 11) {
            String h = String.valueOf(i);
            h = h + "H";
            Hearts.add(h);
            i++;
        }
        ArrayList <String> Diamonds = new ArrayList <String>();// same same for diamonds
        while (i > 1) {
            String d = String.valueOf(i);
            d = d + "D";
            Diamonds.add(d);
            i--;
        }

        // Adds all Cards to 1 Deck Array
        for (int j = 0; j < 10; j++) {
            Deck.add(Clubs.get(j));
        }
        for (int j = 0; j < 10; j++) {
            Deck.add(Spades.get(j));
        }
        for (int j = 0; j < 10; j++) {
            Deck.add(Hearts.get(j));
        }
        for (int j = 0; j < 10; j++) {
            Deck.add(Diamonds.get(j));
        }
        Collections.shuffle(Deck);// shuffles deck
        while (i < 10) {// while loop to put nine 0 in array so the program can end
            Deck.add("0");// adds 0
            i++;// counter variable
    }


       public List<String> getDeck() {// Method used to Store the ArrayList to be accessed Through other classes
           return Deck;
       }
}

Here is the Next Class

import javax.swing.JButton;

import java.util.List;

import javax.swing.JFrame;

import java.util.ArrayList;

public class Window {

        Main m = new Main();

        public static void main(String[] args) {
            Main m = new Main();//allows us to access other class
            List <String> Deck1 = m.getDeck();// makes the new ArrayList equal the old Deck ArrayList

            System.out.println (Deck1.get(1));// Testing this is where java says the error is
}

Please let me know what is wrong and where I need to fix it

Brandon
  • 22,723
  • 11
  • 93
  • 186
  • What error do you see? Please [edit] your question and include it. – Pshemo Feb 06 '20 at 23:13
  • BTW `public static void ArrayList() {// Method to create the deck` so name that method `public static void createDeck(){...}` and NOT `ArrayList`. – Pshemo Feb 06 '20 at 23:16
  • Assuming your code compiles (because as it's presented, it won't), it will generate a `NullPointerException` – MadProgrammer Feb 06 '20 at 23:22
  • The (wonderfully) named method `ArrayList` is missing a closing `}`, which is causing a compiler error before `public List getDeck() {` – MadProgrammer Feb 06 '20 at 23:23

0 Answers0