Java jFugue music play

234 Views Asked by At
import org.jfugue.player.Player;

public class MusicPlayer {
    public static void main(String[] args) {
        TwelveBarBlues twelveBarBlues = new TwelveBarBlues();

        System.out.println("Twelve Bar blues Playing: ");

        Player player = new Player();
        player.play(twelveBarBlues.getPattern());

        }

}

import java.io.IOException;

import org.jfugue.pattern.Pattern;
import org.jfugue.player.Player;
import org.jfugue.theory.ChordProgression;

public class TwelveBarBlues {

private String pattern;

public static void main(String[] args) throws IOException {
    Pattern pattern = new ChordProgression("I IV V")
            .distribute("7%6")
            .allChordsAs("$0 $0 $0 $0 $1 $1 $0 $0 $2 $1 $0 $0")
            .eachChordAs("$0ia100 $1ia80 $2ia80 $3ia80 $4ia100 $3ia80 $2ia80 $1ia80")
            .getPattern()
            .setInstrument("Acoustic_Bass")
            .setTempo(100);
    new Player().play(pattern);
}

public String getPattern() {
    this.pattern = pattern;
    return pattern;
    }
}

I want to play the twelveBarBlue music code at MusicPlayer. but I got error code Cannot invoke "java.lang.CharSequence.length()" because "this.text" is null when I run the MusicPlayer MusicPlayer Error Code

1

There are 1 best solutions below

0
David Koelle On

When you execute MusicPlayer, it calls main() in MusicPlayer but main() in TwelveBarBlues is never called, so in getPattern(), 'pattern' has never been set, and you're returning null.

Instead, change TwelveBarBlues so getPattern() returns the pattern that you currently have in main(). Just move the code from main() into getPattern(), then remove the main() method, since you're not running TwelveBarBlues as its own program.

Or, keep the main() method in case you want to use that class as its own executable, but have it call its own getPattern() method. You'll need to do something like TwelveBarBlues blues = new TwelveBarBlues(); and then new Player().play(blues.getPattern());.