0
import javax.swing.JOptionPane;

public class SpeedOfSound {
    public static void main(String[] args) {
        String medium;
        double time = 0;
        double distance = 0;

        medium = JOptionPane.showInputDialog("Please enter a medium\n" + "(air, water, steel)");
        distance = Double.parseDouble(JOptionPane.showInputDialog("Please enter the distance the soundwave has to travel"));

        if (medium.equalsIgnoreCase("air")) {
            time = distance / 1100;
        } else if (medium.equalsIgnoreCase("water")) {
            time = distance / 4900;
        } else if (medium.equalsIgnoreCase("steel")) {
            time = distance / 16400;
        }

        System.out.println(time);
        System.exit(0);
    }
}

I've never encountered this error and I can't point out what it even is given the code. Any help would be appreciated. Thanks.

Edit: someone mentioned that the question is a duplicate. I looked at that question before posting this. Again, I don't understand the context of my error. The previous question says the exception happens when the pointer points to "null" I can't seem to find an instance where I don't have anything pointing to the respective values.

Zabuzard
  • 20,717
  • 7
  • 45
  • 67
fusion_ow
  • 13
  • 3
  • 3
    The stack trace will include the line number where the exception occurred. – jspcal May 06 '18 at 22:41
  • 1
    When encountering an error, always include the full error message in your question. The stacktrace contains the exact line which triggered the exception. Take a look at that line and show it to us. A NPE means you have a situation like `null.someMethod()`. So you try to access an object that is currently `null` (i.e. it does not exist). If we know the line, we can directly tell you which variable is `null`. Also, you can easily debug stuff like that by just printing the lines and inspecting their current values. – Zabuzard May 06 '18 at 22:54
  • By the way, don't use `System.exit(...)`. It is a poor way to handle control flow. And definitely not needed in your case at all. The program automatically ends when reaching the end, no need to end it manually by calling that method. – Zabuzard May 06 '18 at 22:57
  • Add stack trace please – Vedant Kekan May 06 '18 at 22:57
  • The code itself looks fine at first glance. You probably misused the dialogs, maybe closed one or something like that. The dialogs return `null` if nothing was entered. See the official documentation of the dialog methods. – Zabuzard May 06 '18 at 22:59
  • https://imgur.com/bduopSF this is the only thing I get when I run the program. It doesn't show anything else. – fusion_ow May 06 '18 at 23:01
  • Can you start a Hello-World program? *Internal error* sounds more like your Eclipse is bugged than the program. – Zabuzard May 06 '18 at 23:17
  • 1
    @Zabuza was right. The program itself was bugged. I don't understand why though, but thanks for the help. – fusion_ow May 06 '18 at 23:33

0 Answers0