Need help Solving static methods

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nomad
    Recognized Expert Contributor
    • Mar 2007
    • 664

    #1

    Need help Solving static methods

    Hi everyone:
    I need help Please I'm still learning Java...
    Here is part of my code where I'm having problems.
    Problem is at public static void main(String[] args) {
    The method main cannot be declared static; static methods can only be declared in a static or top level type..

    Code:
    Code:
    	
    		public static void main(String[] args) {
    			Employee emp = new Employee();
    			Scanner kbd = new Scanner(System.in);
    			int choice;
    			System.out.println("Make a Section: ");
    			System.out.println("1. Enter ");
    			System.out.println("2. Find ");
    			System.out.println("3. Exit ");
    			System.out.print("\nPlease press Enter afer each response");
    			System.out.println("Enter your chose please: ");
    			choice = kbd.nextInt();
    			kbd.nextLine();
    			if (choice == 1) { // if 1 is select go to makePerson
    				emp.inputEmployee();
    
    			} // close the if loop
    
    			if (choice == 2) { // if 2 is select go to find
    				emp.displayMatch();
    
    			}// close the choice==2
    			if (choice == 3) {
    				System.out.printf("Good bye");
    			}// close the choice == 3
    
    		}// close public static void
    thanks
    nomad
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by nomad
    Hi everyone:
    I need help Please I'm still learning Java...
    Here is part of my code where I'm having problems.
    Problem is at public static void main(String[] args) {
    The method main cannot be declared static; static methods can only be declared in a static or top level type. Given the indentation level there should
    be some other code before your main method definition.

    Code:
    Code:
    	
    		public static void main(String[] args) {
    			Employee emp = new Employee();
    			Scanner kbd = new Scanner(System.in);
    			int choice;
    			System.out.println("Make a Section: ");
    			System.out.println("1. Enter ");
    			System.out.println("2. Find ");
    			System.out.println("3. Exit ");
    			System.out.print("\nPlease press Enter afer each response");
    			System.out.println("Enter your chose please: ");
    			choice = kbd.nextInt();
    			kbd.nextLine();
    			if (choice == 1) { // if 1 is select go to makePerson
    				emp.inputEmployee();
    
    			} // close the if loop
    
    			if (choice == 2) { // if 2 is select go to find
    				emp.displayMatch();
    
    			}// close the choice==2
    			if (choice == 3) {
    				System.out.printf("Good bye");
    			}// close the choice == 3
    
    		}// close public static void
    thanks
    nomad
    Check your curly braces in your source code *before* the definition of your main
    method. I'm sure you forgot one of them and it upsets the compiler thinking
    that you are trying to define your main method somewhere else (reread that
    compiler diagnostic message again).

    kind regards,

    Jos

    Comment

    • nomad
      Recognized Expert Contributor
      • Mar 2007
      • 664

      #3
      Thanks Jos, I found the mistake.
      Now I have a Exception in thread "main" java.lang.NullP ointerException
      at company.Employe e.<init>(Employ ee.java:141)
      at company.Employe e.main(Employee .java:250)

      I think it has to due to my arraylist.

      line 141 is
      Code:
      int empid = employees.size();
      line 250 is
      Code:
      Employee emp = new Employee();
      this is class for my arraylist
      Code:
      public class Employee {
      		
      		public ArrayList<Employee_listing> employees;// ArrayList of
      
      		// contact
      
      		int empid = employees.size();
      
      		public void createEmployeeDB() {
      			employees = new ArrayList<Employee_listing>();
      		}
      can you please help me.
      thanks
      nomad

      Comment

      • nomad
        Recognized Expert Contributor
        • Mar 2007
        • 664

        #4
        Never mind I solved that problem I did not need that statement.
        But now when I run the program I get a
        Exception in thread "main" java.lang.NullP ointerException
        at company.Employe e.displayMatch( Employee.java:2 17)
        at company.Employe e.main(Employee .java:266)

        thanks
        nomad

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by nomad
          Never mind I solved that problem I did not need that statement.
          But now when I run the program I get a
          Exception in thread "main" java.lang.NullP ointerException
          at company.Employe e.displayMatch( Employee.java:2 17)
          at company.Employe e.main(Employee .java:266)

          thanks
          nomad
          Here's a bit of a tip: when you see that blob of an exception stack trace
          find the line nearest to the top that looks familiar (i.e. it resembles your own
          code). Check the line number and just above that line print out everything
          you think can be of interest. A NullPointerExce ption usually means that you
          forgot to instantiate an object (using 'new ...'). Run your program again and
          add more System.out.prin tln( ... ) statements to find the bug.

          kind regards,

          Jos

          Comment

          • nomad
            Recognized Expert Contributor
            • Mar 2007
            • 664

            #6
            OK I did that.
            I know it has has to be the next statement which is a find statement.
            Made a System.out.prin tln("HERE IS THE MISTAKE:");
            after that I get the NullPointerExce ption.
            can you please give be a clue or help me

            Here is my code

            Code:
            			Scanner input_flag = new Scanner(System.in);
            			System.out.println("Enter Employee Id Number AB1234==>");
            			String empid = input_flag.next();
            			String id_flag = "";
            			System.out.println("HERE IS THE MISTAKE:"); 
            			boolean notfound = true;
            			for (Employee_listing e : employees) {
            				String emp = e.getEmpId();
            				if (emp.equals(id_flag)) {
            					System.out.println("Hello" + ("e.lname"));
            					notfound = false;
            				}
            			}
            			if (notfound == true) {
            
            			System.out.print("Enter Last Name Doe==>");
            			String ln = input_flag.next();
            Thanks Jos

            nomad

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by nomad
              OK I did that.
              I know it has has to be the next statement which is a find statement.
              Made a System.out.prin tln("HERE IS THE MISTAKE:");
              after that I get the NullPointerExce ption.
              can you please give be a clue or help me

              Here is my code

              Code:
              			Scanner input_flag = new Scanner(System.in);
              			System.out.println("Enter Employee Id Number AB1234==>");
              			String empid = input_flag.next();
              			String id_flag = "";
              			System.out.println("HERE IS THE MISTAKE:"); 
              			boolean notfound = true;
              			for (Employee_listing e : employees) {
              				String emp = e.getEmpId();
              				if (emp.equals(id_flag)) {
              					System.out.println("Hello" + ("e.lname"));
              					notfound = false;
              				}
              			}
              			if (notfound == true) {
              
              			System.out.print("Enter Last Name Doe==>");
              			String ln = input_flag.next();
              Thanks Jos

              nomad
              My guess is that your 'employees' variable is null. Print that one out before you
              start the loop, so you're sure about that. Don't just print out a bit of text but
              print out all the variable values you suspect to be incorrect.

              kind regards,

              Jos

              Comment

              • nomad
                Recognized Expert Contributor
                • Mar 2007
                • 664

                #8
                OK did to things.
                1. I made a system print on empid and ln fields. It gave the the throw after empid.
                2. I ask the a system count and after empid. it again came back with a throw.
                3. I made a (what I call it) a tester program and I drop some data into it .
                I just added the empid only. Same results...in fact it did not even see the data that I add. I did a test asking for a count...

                What do I do next. Remember I'm just a beginner.

                thanks
                nomad
                Also is this better find statement. that is once if figure this problem

                empid has a Can only iterate over an array or an instance of java.lang.Itera ble

                Code:
                	
                Scanner input_flag = new Scanner(System.in);
                System.out.println("Enter Employee Id Number AB1234==>");
                			int idx =0;
                			String empid = input_flag.next();
                			boolean notfound=true;
                
                			for(String id : empid){
                				if(id.equals(input_flag)){
                					System.out.println("Employee is in the db please go back to Menu");
                					notfound = false;
                				}
                					idx++;
                				}
                				if(notfound==true){
                does the rest of my code...
                }

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by nomad
                  OK did to things.
                  1. I made a system print on empid and ln fields. It gave the the throw after empid.
                  2. I ask the a system count and after empid. it again came back with a throw.
                  3. I made a (what I call it) a tester program and I drop some data into it .
                  I just added the empid only. Same results...in fact it did not even see the data that I add. I did a test asking for a count...

                  What do I do next. Remember I'm just a beginner.

                  thanks
                  nomad
                  Also is this better find statement. that is once if figure this problem

                  empid has a Can only iterate over an array or an instance of java.lang.Itera ble

                  Code:
                  	
                  Scanner input_flag = new Scanner(System.in);
                  System.out.println("Enter Employee Id Number AB1234==>");
                  			int idx =0;
                  			String empid = input_flag.next();
                  			boolean notfound=true;
                   
                  			for(String id : empid){
                  				if(id.equals(input_flag)){
                  					System.out.println("Employee is in the db please go back to Menu");
                  					notfound = false;
                  				}
                  					idx++;
                  				}
                  				if(notfound==true){
                  does the rest of my code...
                  }

                  What, if I may ask, is this line supposed to be doing?
                  Code:
                   for(String id : empid){

                  Comment

                  • nomad
                    Recognized Expert Contributor
                    • Mar 2007
                    • 664

                    #10
                    Originally posted by r035198x
                    What, if I may ask, is this line supposed to be doing?
                    Code:
                     for(String id : empid){

                    The program starts looping throught the array empid using a for() loop. It first compares the variable being set (id) with the employee id (empid) being searched for (input_flag), If they are eqaul then the name is printed, if not found then it continues with the enter info.

                    nomad

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by nomad
                      The program starts looping throught the array empid using a for() loop. It first compares the variable being set (id) with the employee id (empid) being searched for (input_flag), If they are eqaul then the name is printed, if not found then it continues with the enter info.

                      nomad
                      But you have
                      Code:
                       String empid = input_flag.next();
                      and so empid may not be the array you think it is.

                      Comment

                      • nomad
                        Recognized Expert Contributor
                        • Mar 2007
                        • 664

                        #12
                        can you help me solve this.
                        How do I get the scanner (input_flag) to find empid in a arraylist?

                        nomad

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by nomad
                          can you help me solve this.
                          How do I get the scanner (input_flag) to find empid in a arraylist?

                          nomad
                          What object types do you have in the ArrayList? Are they Strings or are they Employees?

                          Comment

                          • nomad
                            Recognized Expert Contributor
                            • Mar 2007
                            • 664

                            #14
                            They are strings for empid, lname, fname

                            nomad

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #15
                              Originally posted by nomad
                              They are strings for empid, lname, fname

                              nomad
                              Then your design is looking very bad. You should use an Employee class to group the data and store the values as Employees.

                              With your current design, you may end up comparing an empid with a lname which is probably not what you want to do. You may have to make sure the id is at the 1st, 4th, 7th position e.t.c for the comparison to work properly

                              Comment

                              Working...