0

I'm a newbie in java and while i'm doing coding for the sitting plan the counsole kept giving me errors Please help >.<

This is the class for SittingPlan

public SittingPlan()
{
    String[] name = {"charles" };
    String[] sex = {" Girl", "Boy"};
    Students[] names = new Students[NUMBER_OF_STUDENTS];
    currentStudents = 0;
    for ( int count = 0; count < names.length; count++ )
        names [ count ] = new Students ( name [count % 22 ], sex [ count / 22 ]);
}
public void shuffle()
{
    currentStudents = 0;
    SittingPlan.shuffle.setName();
    for ( int first = 0; first < names.length; first++ )
    { 
        int second = randomNumbers.nextInt ( NUMBER_OF_STUDENTS );
        Students temp = names[ first ];
        names[ first ] = names[ second ];
        names[ second ] = (Students) temp;
    }

}
public Students dealStudents()
{
    if ( currentStudents < names.length )
        return (Students) names[ currentStudents++ ];
    else
        return null;
}

and this is the stacktrace

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

at SittingPlan.shuffle(SittingPlan.java:22)
at mainlaunch.main(mainlaunch.java:6)
user15614540
  • 1
  • 1
  • 1
  • 2
    I see a method named shuffle() that returns void; there's no variable named shuffle. You can't call setName() on void. Bad code. Believe the compiler. – duffymo May 18 '16 at 15:53
  • Well, I doubt that its actually supposed to call shuffle() in the shuffle() method itself (infinite recursion). Guess we won't know what this line is supposed to do until charles tells us. – OH GOD SPIDERS May 18 '16 at 16:00
  • @911DidBush i called in shuffle() method to shuffle the variables mention on top such as the variable first and second tho. Any tips to let shuffle work? Sorry for late reply just got back from school – user15614540 May 19 '16 at 12:23
  • `for ( int first = 0; first < names.length; first++ )`. The compiler return NPE again, seems like 'first' variable isn't recognized like the 'second' variable? – user15614540 May 19 '16 at 12:40
  • sorry, i don't get what you are trying to say...But about the NPE, you should read this and if you understand it you will see that the NPE can only come from one place in that line of code: http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – OH GOD SPIDERS May 19 '16 at 13:55

1 Answers1

0

This line is wrong:

SittingPlan.shuffle.setName();

shuffle() is a method, but it's not static. It returns void, so you can't call setName() on the return value.

I can't guess what you want. This code is confusing.

I think a better idea would be using Java collections, like this:

List<Student> classList = new ArrayList<Student>();
// populate with Student instances
Random random = new Random(seed);
Collections.shuffle(classList, random);
duffymo
  • 305,152
  • 44
  • 369
  • 561