0

I don't know why this is happening. Heres the code:

import com.lewislovesgames.chopthattree.minecraftservermanager.Main;

public class main {

 public static void main(String[] args) {
     if (args.length == 0) { 
          if  (args[0] == "--nogui") { // Debugger says this is the line it errors on
          System.err.println("NOGUI not made yet");
      } else {
       System.err.println("[Minecraft Server Manager]: Argument does not exist.");
      }
     }
     System.out.println("[Main]: Running Main Window");
     Main main = new Main();
     main.setVisible(true);

     }
 }

The code did work until I put in the args snippet. It looks like the array is never 0 so it passes on to the next line. I'm using Eclipse for the debugger and running of the program.

LewisTehMinerz
  • 656
  • 7
  • 15
  • `if (args.length == 0) if (args[0] ..` if the first if condition is true, then `args[0]` will throw the exception. It's like saying, if my array is empty, check the value of the first element. Does it sound logical for you? Also you need to use equals to compare Strings. – Alexis C. Dec 06 '14 at 19:57
  • You do if `if (args.length == 0)` and if that is true, you do `args[0]`. – mkobit Dec 06 '14 at 19:57
  • Did you want `args.length != 0` or `args.length > 0`? The line `args[0] == "--nogui"` is *guaranteed* to error if `args.length` is `0`. – dimo414 Dec 06 '14 at 19:58
  • Show us the actual messages!! – Hot Licks Dec 06 '14 at 20:40
  • @dimo414 I was looking for args.length != 0 and not == – LewisTehMinerz Dec 07 '14 at 08:41

2 Answers2

1

You are checking if the length of the array is 0, and you are trying to access the 0th element although you know there is no 0th element.

 if (args.length == 0) { 
      if  (args[0] == "--nogui") { // Debugger says this is the line it errors on
      System.err.println("NOGUI not made yet");
  }
Salih Erikci
  • 5,076
  • 12
  • 39
  • 69
0
  if (args.length == 0) {
      System.err.println("No argument supplied");
  }
  else {
      if  (args[0] == "--nogui") { // Debugger says this is the line it errors on
      System.err.println("NOGUI not made yet");
  //something like this.
  }
Noor Nawaz
  • 2,175
  • 27
  • 36