Results 1 to 7 of 7
  1. #1
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default How do I search an array from a different class?

    I have an array I want get grab information from. I've successfully got the array populated by reading in an Excel spreadsheet. The array is in its own class Players.java.

    My array has 6 columns (elements?) and contains Player ID (key), Name, Rating, Games, Wins, Losses. I want to change some labels in a GUI when someone selects the player name from a Listbox.

    For example,

    ID____Name________Rating____Games____Wins____Losses
    _1____Psyclone_____1631______125______71_______54
    _2____Arkoos_______1793______114______74_______40
    _3____Kellectra____1331_______97______38_______59
    _4____Razan________1468______101______48_______53


    When someone clicks on a name in the text box, their statistics (Rating, Games, Wins, Losses) get displayed in the UI. I already have some dummy text changing when someone clicks on a name from the list. I know how to get the value of the name selected in the list using getSelectedValue(). My problem is getting the values out of the array that is contained in a different class. I know how to search the array even... my problem is accessing it.

    QUESTION: How do I read from the array in a different class?

    I've been stuck on this problem for several days and have been searching for answers and just setting it aside while I worked on a different part of the program. But now I'm at a point where I'm pretty much stuck until I figure this out.

  2. #2
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Um do you mean get an array from a class? In the class, you can do either of the following:

    1. Make the array public (not recommended because of encapsulation):
    Java Code:
    public int[] arr;
    2. Make a public get method in the class.
    Java Code:
    public int[] getArray() {
         return arr;
    }
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  3. #3
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default

    It is an array of objects. I'm not sure if that changes things. What I'm trying to get is just 1 value from the array. For example...

    System.out.println(card[1].getCardName());

    When I run that line in one class is returns the name of the card. When I type that line in another class, it gives an error.

    How do I get that line of code to work in a different class.

  4. #4
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default

    I made the public get method in the class I'm trying to read from...

    Java Code:
        public CardProperties[] getArray()
            {
            return card;
            }
    }
    CardArrays.java is the class that the array I'm trying to read is in and is where I put the get method above. The array card is an array of objects of type CardProperties.

    I'm trying to read the array card in the class PlayerArrays.java. In the class PlayerArrays, I have tried...

    Java Code:
            CardArrays cardArrays = new CardArrays();
            CardProperties[] temp = cardArrays.getArray();
    
            System.out.println(temp[1]);
    which returns "null" instead of the card name. The array is 506 rows and 47 columns. I didn't get an error, but it returned null and I never told it which element to look for.

    I also tried...

    Java Code:
            CardArrays cardArrays = new CardArrays();
            CardProperties[] temp = cardArrays.getArray();
    
            System.out.println(temp[1].getCardName());
    which returns...

    Exception in thread "main" java.lang.NullPointerException
    at Cards.PlayerArrays.setPlayerArrays(PlayerArrays.ja va:74)
    at Cards.Main.main(Main.java:56)
    Java Result: 1

  5. #5
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default

    OK, well I kinda got it to work. I made my CardArrays class return the array, then input the array as a parameter into the next class.

    Is this the way it's supposed to be done?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Is the CardProperties[] array, cards, filled with CardProperties objects in the CardsArrays constructor?

  7. #7
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default

    Is the CardProperties[] array, cards, filled with CardProperties objects in the CardsArrays constructor?
    Yes, I think so. I just started using Java 2 weeks ago.

    I'll try to show you some of my code. Here is a portion of my CardArrays class. I deleted most of it because it's over 1000 lines long and had a lot of other methods.

    Java Code:
    public class CardArrays
    {
        //Here are the declarations for the arrays
        CardProperties[] card = new CardProperties[506];
    
        // Default Constructor builds the empty arrays
        public void ConstructCardArrays()
            {
            for(int i = 0; i < card.length; i++)
                card[i] = new CardProperties();
            }
    
        // Set values for Master Array
        public void setMasterArray() throws IOException, BiffException, WriteException
            {
            File inputWorkbook = new File("Master List.xls");
    	Workbook workbook;
    	try
                {
                workbook = Workbook.getWorkbook(inputWorkbook);
                Sheet sheet0 = workbook.getSheet(0);
    
                for(int i = 0; i < card.length; i++)
                    {
                    Cell cellValue0 = sheet0.getCell(0, i+1);
                    int k0 = Integer.valueOf(cellValue0.getContents()).intValue();
    
                    Cell cellValue1 = sheet0.getCell(1, i+1);
                    String k1 = String.valueOf(cellValue1.getContents());
    
                    ...
                    
                    Cell cellValue45 = sheet0.getCell(45, i+1);
                    String k45 = String.valueOf(cellValue45.getContents());
    
                    card[i].setCardID(k0);
                    card[i].setCardType(k1);
                    
                    ...
                            
                    card[i].setSpellText(k45);
     
                    }
                }
            catch (BiffException e)
                {
                e.printStackTrace();
                }
    	}//End setMasterArray()
    
         //Displays the current values in the Master Array
        public void displayMasterArray()
            {
            for(int i = 0; i < card.length; i++)
                {
                System.out.println(card[i].getCardProperties());
                }
            }//End displayMasterArray()
    
        public CardProperties[] getArray()
            {
            return card;
            }
    
    }
    This is my PlayerArrays class.

    Java Code:
    public class PlayerArrays
    {
        //Here are the declarations for the arrays
        PlayersOnline[] playersOnline = new PlayersOnline[8];
    
        // Default Constructor builds the empty array
        public void ConstructPlayerArrays()
            {
            for(int i = 0; i < playersOnline.length; i++)
                playersOnline[i] = new PlayersOnline();
            }
    
        public PlayersOnline[] setPlayerArrays(CardProperties[] temp) throws IOException, BiffException, WriteException
            {
    
    // *****************************************************************************
            System.out.println(temp[1].getCardName());
    // *****************************************************************************
    
    
            File inputWorkbook = new File("Player List.xls");
    	Workbook workbook;
    	try
                {
                workbook = Workbook.getWorkbook(inputWorkbook);
                Sheet sheet0 = workbook.getSheet(0);
    
                for(int i = 0; i < sheet0.getRows()-1; i++)
                    {
                    Cell cellValue0 = sheet0.getCell(0, i+1);
                    int k0 = Integer.valueOf(cellValue0.getContents()).intValue();
    
                    Cell cellValue1 = sheet0.getCell(1, i+1);
                    String k1 = String.valueOf(cellValue1.getContents());
    
                    Cell cellValue2 = sheet0.getCell(3, i+1);
                    int k2 = Integer.valueOf(cellValue2.getContents()).intValue();
    
                    Cell cellValue3 = sheet0.getCell(4, i+1);
                    int k3 = Integer.valueOf(cellValue3.getContents()).intValue();
    
                    Cell cellValue4 = sheet0.getCell(5, i+1);
                    int k4 = Integer.valueOf(cellValue4.getContents()).intValue();
    
                    Cell cellValue5 = sheet0.getCell(6, i+1);
                    int k5 = Integer.valueOf(cellValue5.getContents()).intValue();
    
                    playersOnline[i].setPlayerID(k0);
                    playersOnline[i].setPlayerName(k1);
                    playersOnline[i].setPlayerRating(k2);
                    playersOnline[i].setPlayerWins(k3);
                    playersOnline[i].setPlayerLosses(k4);
                    playersOnline[i].setPlayerCurrentDeck(k5);
                    }
                }
            catch (BiffException e)
                {
                e.printStackTrace();
                }
            return playersOnline;
    	}//End setMasterArray()
    
    
        public void displayPlayerArrays()
            {
            for(int i = 0; i < playersOnline.length; i++)
                {
                System.out.println(playersOnline[i].getPlayersOnline());
                }
            }//End displayDatabase()
    
    }
    I'm trying to access the array where all the *************** are in the code.

    I temporarily solved the problem by returning card back to main (where I called it) and then used it as a parameter. For example, right now the code is running as

    Java Code:
        public PlayersOnline[] setPlayerArrays(CardProperties[] temp) throws IOException, BiffException, WriteException
    instead of

    Java Code:
        public void setPlayerArrays() throws IOException, BiffException, WriteException
    This doesn't really solve my problem though. In the end, I want to be able to read this array into my GUI class, so I need to learn how to do this the right way.

Similar Threads

  1. Mostly completed array search program
    By Ryujin89 in forum New To Java
    Replies: 1
    Last Post: 11-04-2009, 03:25 AM
  2. Search a string in a byte array
    By 2BOrNot2B in forum New To Java
    Replies: 0
    Last Post: 03-12-2009, 05:52 PM
  3. [SOLVED] Search problem - array out of bounds
    By viper110110 in forum New To Java
    Replies: 5
    Last Post: 11-26-2008, 04:26 AM
  4. Newbie search array question
    By CirKuT in forum New To Java
    Replies: 19
    Last Post: 09-14-2008, 06:26 AM
  5. Array Search Test
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •