0

So i tried to research this. I tried using this magic 8 ball code i got from somewhere else but i want to use my own image when the J panel pops up to ask a question:

    import java.security.SecureRandom;
    import javax.swing.ImageIcon;
    import javax.swing.JOptionPane;

    public class Magic8Ball {
    private final static ImageIcon image = new 
    ImageIcon(this.getClass().getResource("BuckminsterFuller.jpg"));

    private final static SecureRandom randomNumber = new SecureRandom();
    private final static String answers[] = {
            "It is certain",
            "It is decidedly so",
            "Without a doubt",
            "Yes - definitely",
            "You may rely on it",
            "As I see it, yes",
            "Most likely",
            "Outlook good",
            "Signs point to yes",
            "Yes",
            "Reply hazy, try again",
            "Ask again later",
            "Better not tell you now",
            "Cannot predict now",
            "Concentrate and ask again",
            "Don't count on it",
            "My reply is no",
            "My sources say no",
            "Outlook not so good",
            "Very doubtful" };


    public static void main(String[] args) {

        boolean askQuestion = true;

        while (askQuestion) {
            String question = getUserQuestion();
            String randomAnswer = getRandomAnswer();

            displayAnswer(question, randomAnswer);

            askQuestion = userWantsToAskAnotherQuestion();
        }

        showExitMessage();
    }

    private static String getUserQuestion() {
        return JOptionPane.showInputDialog(null,
                "PLease enter a yes or no question:",
                "WELCOME: Have your questions answered!",
                JOptionPane.INFORMATION_MESSAGE);
    }

    private static String getRandomAnswer() {
        return answers[randomNumber.nextInt(answers.length)];
    }

    private static void displayAnswer(String question, String randomAnswer) {
        JOptionPane.showMessageDialog(null, question + "\n" + randomAnswer, "The Magic-8 Ball has responded.", JOptionPane.PLAIN_MESSAGE, image);
    }

    private static boolean userWantsToAskAnotherQuestion() {
        return 0 == JOptionPane.showConfirmDialog(null, "", "Would you like to ask again?", JOptionPane.YES_NO_OPTION, 0, image);
    }

    private static void showExitMessage() {
        JOptionPane.showMessageDialog(null, "Programmed by my name", "Goodbye! Your answers have been answerd.", JOptionPane.PLAIN_MESSAGE, image);
    }
}

I tried saving the image as BuckminsterFuller.jpg in the directory where the class is at, in a separate folder called "images" where the project, src, build, and class is at.

It gives me this error:

java.lang.ExceptionInInitializerError Caused by:
java.lang.RuntimeException: Uncompilable source code - non-static
variable this cannot be referenced from a static context    at
Assignment6.Magic8Ball.<clinit>(Magic8Ball.java:10)
Ansharja
  • 1,237
  • 1
  • 14
  • 37

1 Answers1

0

The error is in this line:

private final static ImageIcon image = new ImageIcon(this.getClass().getResource("BuckminsterFuller.jpg"));

You can't use this when you are declaring a static field.

You can use getSystemResource(String name) method from ClassLoader class.

Like this:

private final static ImageIcon image = new ImageIcon (ClassLoader.getSystemResource("BuckminsterFuller.jpg"));

EDIT

If you get a NullPointerException in ImageIcon constructor, it means that ClassLoader is not using the correct image path. ClassLoader.getSystemResource () uses the system classloader, the one which was used to start the program.

For example, if your directory tree is:

-> bin
  -> myapp
  -> resources
     -> ...
     -> BuckminsterFuller.jpg
     -> ...

And your Main class is in package myapp, you should use this path to load image:

private final static ImageIcon image = new ImageIcon (ClassLoader.getSystemResource("resources/BuckminsterFuller.jpg"));

But it all depends on how your application is structured.

Also, you are abusing of static modifier, which usually denotes a very poor design, take a look at this question: Why are static variables considered evil?

Ansharja
  • 1,237
  • 1
  • 14
  • 37
  • Hey @Ansharja now it gives me this: java.lang.ExceptionInInitializerError Caused by: java.lang.NullPointerException at javax.swing.ImageIcon.(ImageIcon.java:217) at Assignment6.Magic8Ball.(Magic8Ball.java:10) – Proximus Seraphim Dimitri Davi Oct 26 '17 at 19:08