Results 1 to 10 of 10

Thread: code isnt working, using random generator

Threaded View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2007
    Posts
    125

    code isnt working, using random generator

    my programme is attatched to this and was written in blue j


    Peeps, im really struggling with the random generator in my code, everytime i compile, then run my interface to generate a fixture list i get an error of

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Matchlist.CreateMatch(Matchlist.java:33)
    at MyPanel.actionPerformed(MyPanel.java:83)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6038)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
    at java.awt.Component.processEvent(Component.java:5803)
    at java.awt.Container.processEvent(Container.java:2058)
    at java.awt.Component.dispatchEventImpl(Component.java:4410)
    at java.awt.Container.dispatchEventImpl(Container.java:2116)
    at java.awt.Component.dispatchEvent(Component.java:4240)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
    at java.awt.Container.dispatchEventImpl(Container.java:2102)
    at java.awt.Window.dispatchEventImpl(Window.java:2429)
    at java.awt.Component.dispatchEvent(Component.java:4240)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

    which is pointing at this line of code;

    while (sd.getgh(greenStudent).getGame(game).equalsIgnoreCase(""))

    what the hell am i doing wrong? i just cant see the problem, It shouldn't really be possible for something there to be null, the only thing I can think off is that something is setting one of the arrays too high. if this were the case it should return array out of bounds? thanks for your time and if any one needs the other classes that i have created (there are 3 others) then just say and ill psot

    Code:
    import java.util.Random;
    
    public class Matchlist
    {
    
        private studentdetails sd = new studentdetails();
        /** matchList StringBuilder stores the match list in progress */
        private StringBuilder matchList = new StringBuilder();
        private Random studentPicker = new Random();
        private int loop = 0;
        private int matches = 0;
    
        public Matchlist()
        {
            sd.createstudentdetails();
        }
    
        /** Method to create the actual match list, returns the list as a string */
        public String CreateMatch()
        {
            int game;
            int yellowStudent = 0;
            int currentGame = 0;
            int matchAttempt = 0;
            
            while (matches < 70)
            {
                makeMatches:
                for (game = 0; game < 4; game++)//g = game
                {
                    for (int greenStudent = 0; greenStudent < 17; greenStudent++)
                    {
                        while (sd.getgh(greenStudent).getGame(game).equalsIgnoreCase(""))//equalsignroecase returns true if the value/string matches whats in the brackets
                        {
                            matchAttempt++;
                            if (matchAttempt > 800)
                            {
                                sd = new studentdetails();
                                game = 0;
                                matches = 0;
                                break makeMatches;
                            }
                            yellowStudent = studentPicker.nextInt(17);
                            if (sd.getyh(yellowStudent).getGame(game).equalsIgnoreCase(""))
                            {
                                if (sd.getgh(greenStudent).getC_lass() != sd.getyh(yellowStudent).getC_lass())
                                {
                                    /** Check to see if the person has played the other person */
                                    if (sd.getgh(greenStudent).checkPlayed(sd.getyh(yellowStudent).getName()) == false)
                                    {
                                        /** Set the game to the name of the opponent played */
                                        sd.getyh(yellowStudent).changeGame(game, sd.getgh(greenStudent).getName());
                                        sd.getgh(greenStudent).changeGame(game, sd.getyh(yellowStudent).getName());
                                        /** Build the match list step by step using append with \n at the end to create a new line */
                                        matchList.append(sd.getyh(yellowStudent).getName() + " vs " + sd.getgh(greenStudent).getName() + "\n");
                                        matches++;
                                        currentGame++;
                                        if (currentGame == 18)
                                        {
                                            currentGame = 0;
                                            break;
                                        }
                                    }
                                }
    
                            }
                        }
                    }
                }
            }
            /** Convert the stringbuilder into an actual string, then return it */
            String completeMatchList = matchList.toString();
            System.out.println(matches);
            for (int i = 0; i < 18; i++)
            {
                sd.getyh(i).getEmptyMatches();
                sd.getgh(i).getEmptyMatches();
            }
            return completeMatchList;
        }
        }
    Attached Files Attached Files

Posting Permissions

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



Click Here to Expand Forum to Full Width