1 Attachment(s)
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;
}
}
Re: code isnt working, using random generator
Well...
Let's get directly into it:
within the
Code:
while (sd.getgh(greenStudent).getGame(game).equalsIgnoreCase(""))
you are calling
Code:
sd = new studentdetails();
which sets all elements in the gh array to nulls on the next iteration of the for loop (greenstudent loop) your sd is full of nulls, hence it's throwing an exception NullPointerException. So you'll have to either call the createstudentdetails() method before you get to the while loop or you can put a call to that method in the constructor of the studentdetails class
Re: code isnt working, using random generator
Quote:
Originally Posted by ComputerJy
Well...
Let's get directly into it:
within the
Code:
while (sd.getgh(greenStudent).getGame(game).equalsIgnoreCase(""))
you are calling
Code:
sd = new studentdetails();
which sets all elements in the gh array to nulls on the next iteration of the for loop (greenstudent loop) your sd is full of nulls, hence it's throwing an exception NullPointerException. So you'll have to either call the createstudentdetails() method before you get to the while loop or you can put a call to that method in the constructor of the studentdetails class
ignore this post, i reliase i made a mistake :p
Re: code isnt working, using random generator
is this the correct way if putting a call to the createstudentdetails() method from within the constructor of the studentdetails class?
Code:
public class studentdetails
{
public student[] gh = new student[18];
public student[] yh = new student[18];
public studentdetails()
{
createstudentdetails()
}
public void createstudentdetails()
{
gh[0] = new student("Arnold", 1);
gh[1] = new student("Bertha", 1);
gh[2] = new student("Bella", 1);
Re: code isnt working, using random generator
Yes. I'm not sure if the logic is correct here, I only found the reason the exception is being thrown.
Re: code isnt working, using random generator
Quote:
Originally Posted by ComputerJy
Yes. I'm not sure if the logic is correct here, I only found the reason the exception is being thrown.
this just causes the programme to crash, and when i try to implement the sd.createstudentdetails(); on the line before the while (sd.getgh(greenStudent).getGame(game).equalsIgnoreCase(""))
i get a total of 132 matches?! even though im sure all the conditions are there?
Re: code isnt working, using random generator
I told you, you have to check on the logic of the app. And BTW don't use the equalsIgnoreCase method, use the equals instead
Re: code isnt working, using random generator
Quote:
Originally Posted by ComputerJy
I told you, you have to check on the logic of the app. And BTW don't use the equalsIgnoreCase method, use the equals instead
im not sure of the difference? what will it do, and instead of using equalsIgnoreCase do i just replace those words with equals?
Re: code isnt working, using random generator
yes just replace it. It will save some execution time
Re: code isnt working, using random generator
thank you for that infromation, just outta curiosity, ive checked the logic, and there seems to be nothing out of place with the code, i mean ive moved things arond and still no avail, i cant see why it would print 132 matches and still say some people havent played even though the conditions clearly should only work up to 72 as i am using the following while loop
while (matches < 72)
thanks for your time (ps i added your msn to my list if thats ok? feel free to decline :p )