1 2 Previous Next 18 Replies Latest reply: Nov 4, 2009 4:29 PM by 796447 RSS

    Exception in thread "main" java.lang.NullPointerException

    843789
      Hello,

      I'm getting the "Exception in thread "main" java.lang.NullPointerException" error message, here is the full message:
      Exception in thread "main" java.lang.NullPointerException
              at Main.checkDatabase(Main.java:75)
              at Main.prompt(Main.java:41)
              at Main.main(Main.java:28)
      Java Result: 1
      The program worked perfectly well before calling the "checkDatabase" method, so something must be missing and I know it has to do with "null" input parameters but I just can't figure it out. Here is the full code:

      P.S: The statements between 2 stars * are the lines mentioned in the error message.
      import java.util.Scanner;
      
      public class Main {
      
          private static String username;
          private static String password;
          private static String user[],passwd[], authentication[];
      
          public static void main(String[] args) {
      
               String passwd[] = {"abc123","cda$54","animal64","580Obama","password666","lal1lul3l0"
                      ,"MeTaL$gEaR","John654","Micheal65$1","Bu$h007",
              "$123FAISSaL","CALLofDUTY4","K0j1mA000","LiNuX#$!@123","Z3rO567","Z0nLyStr0g3nA"
                       ,"black%HOUSE","The*Patr10t$","S3^PLAN","GWAI110011",};
      
               String user[] = {"Rami","Micheal","John","Jinx","Mohamad","Mounir","Jad","Alaa",
              "Jigsaw","Faissal","Kamal","Jean","Rayane","Tristan","Marc","Sebastian","Elie","Azam"
                       ,"Jason","Snake"};
      
              authentication = new String [20];
      
             for(int i=0; i<passwd.length; i++)
              authentication=(""+user[i]+passwd[i]);

      /*for(int i=0; i<authentication.length; i++)
      System.out.println(authentication[i]);*/

      prompt();

      }

      public static int prompt()
      {
      Scanner input = new Scanner(System.in);

      System.out.println("Please input your username and password to login: ");
      System.out.print("Username: ");
      username = input.nextLine();


      boolean check = checkDatabase(username);
      if(check==true)
      System.out.println("Username found!");
      else if(check==false)
      System.out.println("NOT FOUND!");


      System.out.println();
      System.out.print("Password: ");
      password = input.nextLine();

      if(password.length()<6)
      {
      System.out.println("\n\nThe password length must be at least 6 characters.");

      while(password.length()<=5)
      {
      System.out.print("Password: ");
      password = input.nextLine();

      }
      }
      else if(password.length()>=5)
      return 1;

      return -1;
      }

      public static boolean checkDatabase(String name)
      {
      int i=0;

      while(i<20)
      {
      *if(user[i].equals(name))*
      return true;
      else i++;
      }

      return false;

      }
      }
      Help is appreciated.
      
      Edited by: Batzi on Nov 3, 2009 1:04 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
        • 1. Re: Exception in thread "main" java.lang.NullPointerException
          843789
          Batzi wrote:
                       if(user.equals(name)) user[i] is null. Did you give it a value? 
          • 2. Re: Exception in thread "main" java.lang.NullPointerException
            3004
            If you mean this line
                        *if(user.equals(name))*


            Then either user is null or user[ i ] is null.
            • 3. Re: Exception in thread "main" java.lang.NullPointerException
              843789
              jverd wrote:
              If you mean this line
              *if(user.equals(name))*


              Then either user is null ...
              Whoops, that's the other option.  And the correct one.  You aren't assigning that array in main to your static class member, you're assigning it to a local variable.  Get rid of the String[] in String[] user = ...                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
              • 4. Re: Exception in thread "main" java.lang.NullPointerException
                843789
                Silly forum software...

                Edited by: endasil on 3-Nov-2009 4:10 PM
                • 5. Re: Exception in thread "main" java.lang.NullPointerException
                  843789
                  [edit]
                  too late

                  Edited by: paulcw on Nov 3, 2009 1:10 PM
                  • 6. Re: Exception in thread "main" java.lang.NullPointerException
                    843789
                    I suggest you try a debugger and add a break point at the line which throws the exception.

                    It would appear that you haven't initialised the field user (note, you also have a local variable user which you do initialise), that would be obvious if you used a debugger. hint, hint.
                    • 7. Re: Exception in thread "main" java.lang.NullPointerException
                      843789
                      if you try to print the elements in user[], it will output the content. So I don't think user[] has null values.
                      • 8. Re: Exception in thread "main" java.lang.NullPointerException
                        843789
                        Batzi wrote:
                        if you try to print the elements in user[], it will output the content. So I don't think user[] has null values.
                        It depends on where you print the elements in user (and actually the suggestion isn't that user has null values, but rather that user is itself null).
                        Do you understand why?
                        • 9. Re: Exception in thread "main" java.lang.NullPointerException
                          843789
                          Batzi wrote:
                          if you try to print the elements in user[], it will output the content. So I don't think user[] has null values.
                          Reread what I, and others, said after that post you're replying to.

                          In your main method, you do in fact have a String[] called user, however, you're assigning it to a local variable (that is, one that has no significance outside of that method body), not your static class member. So it'll seem to work fine from your main method, but not from anywhere else.

                          As I already indicated, change
                          String user[] = ... //in your main method
                          To simply:
                          user = ...
                          Edit The same will apply to your other arrays (password, etc).

                          Edited by: endasil on 3-Nov-2009 4:40 PM
                          • 10. Re: Exception in thread "main" java.lang.NullPointerException
                            3004
                            Batzi wrote:
                            if you try to print the elements in user[], it will output the content. So I don't think user[] has null values.
                            What we told you is correct. Do this right before the line that's causing the NPE:
                            System.out.println(user);
                            System.out.println(user);
                            • 11. Re: Exception in thread "main" java.lang.NullPointerException
                              843789
                              dude that won't work. Anyway, I fixed it, it seems that once you declare your array as a global variable you just can't use that format in the main method:
                              user = {"a","b"}; //can't be in main once declared as global
                              it must be:
                              private static String user[] ={"a","b"};
                              Thanks anyway.

                              Problem solved.
                              • 12. Re: Exception in thread "main" java.lang.NullPointerException
                                3004
                                Batzi wrote:
                                dude that won't work.
                                Who are you talking to and what won't work?

                                If you're addressing me about my suggestion to add those prints, yes, it absolutely will work. It will perfectly perform it's job of telling you conclusively what it null.
                                Anyway, I fixed it, it seems that once you declare your array as a global variable you just can't use that format in the main method:
                                There's no such thing as a global variable in Java, but I assume what you're trying to say is what others told you: If you want to use a member variable in a method, don't redeclare it, because that creates a separate local variable that hides (or shadows--I forget which term is used in which cases) the member variable.
                                • 13. Re: Exception in thread "main" java.lang.NullPointerException
                                  843789
                                  Batzi wrote:
                                  dude that won't work. Anyway, I fixed it, it seems that once you declare your array as a global variable you just can't use that format in the main method:
                                  That's what people have been trying to tell you, except they got it right and you still have a fuzzy understanding.

                                  Read your Java text about "scope" and "scoping".
                                  • 14. Re: Exception in thread "main" java.lang.NullPointerException
                                    843789
                                    Batzi wrote:
                                    It seems that once you declare your array as a global variable you just can't use that format in the main method:
                                    Incorrect, you can use it in both, you just have to realise these are two different references.
                                    1 2 Previous Next