Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: I Seriously Need your Serious Help.

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy What to do with returning null ?



    Newbie Question Coming thru. And to sir JPS I am greatfully thank you and sorry. I've fixed it now.
    TO the point. This Code now gives me nullpointerExceptions. I tried putting String to it so It won't return as null but it still won't budge.


    import java.util.Scanner;
    public class RegistrationofEmployees {
     public static final	String emp[][] = new String[4][4];
        public static void main(String[] args) {
     
           	Scanner input = new Scanner(System.in);
     
        	System.out.println("Registration of Employee");
     
        	startPage();
        	System.out.println("\nProgram Terminated...");
     
        }
     
     
     	public static void startPage(){
     
    	String employee ="";
     
    			Scanner input = new Scanner(System.in);
    			System.out.println("Registration of Employee");
    			System.out.println("============================================");
    			System.out.println("Enter your Choice: ");
    			System.out.println("============================================");
    			System.out.println("\t\t[a] Register Employee");
    			System.out.println("\t\t[b] Time Keeping");
    			System.out.println(" ");
    			System.out.print("Choose Letter: ");
    			String choice1 = input.next();
    			if(choice1.length() == 1){
    				char choice2 = choice1.charAt(0);
    			if (choice2 == 'a' || choice2 == 'A'){
    				System.out.println("Lalalalala");
     
    			}else if(choice2 == 'b' || choice2 == 'B'){
    				searchEmployee(employee);
     
     
     
    			}else{
    				System.out.println("============================================");
    				System.out.println("Invalid Letter");
    				System.out.println("============================================");
    			}
    			}else{
    				System.out.println("Invalid String..!");
    			}
     
    	public static String[] searchEmployee(String empNumber){
    		Scanner input= new Scanner(System.in);
    		System.out.println("Enter Employee Number: ");
    		String empNumber1 = input.next();
    		String[] employee = new String[4];
    		if(empNumber1.length() < 5 || empNumber1.length() > 5){
    			System.out.println("Criteria is invalid.");
    			return null;
     
    		}else{
    			for(int x=0;x<emp.length;x++){
    				if(!emp[x][0].equals("")){
    					if(emp[x][0].equals(empNumber1)){
    						employee[0] = emp[x][0];
    						employee[1] = emp[x][1];
    						employee[2] = emp[x][2];
    						employee[3] = emp[x][3];
    						return employee;
    						}
    					}
    			}
    		}	System.out.println("Employee number provided not found!");
    			return null;
     
     
     
     
     
    	}
    	public static void data(String emp[][]){
     
    		emp[0][0] = "10000";
    		emp[0][1] = "John Loudwig";
    		emp[0][2] = "Alonsagay";
    		emp[0][3] = "Regular Employee";
     
    		emp[1][0] = "10001";
    		emp[1][1] = "Gienna";
    		emp[1][2] = "Belonio";
    		emp[1][3] = "Manager";
     
    		emp[2][0] = "10002";
    		emp[2][1] = "James";
    		emp[2][2] = "Arcilla";
    		emp[2][3] = "Supervisor";
    }
    The code is giving me three NullPointerExceptions at these lines :startPage();
      public static void main(String[] args) {
     
           	Scanner input = new Scanner(System.in);
     
        	System.out.println("Registration of Employee");
     
        	startPage();
        	System.out.println("\nProgram Terminated...");
     
        }
    At the searchemployee(employee);
    if(choice1.length() == 1){
    				char choice2 = choice1.charAt(0);
    			if (choice2 == 'a' || choice2 == 'A'){
    				System.out.println("Lalalalala");
     
    			}else if(choice2 == 'b' || choice2 == 'B'){
    				searchEmployee(employee);
    annnd at this loop under the searchEmployee method
    	if(!emp[x][0].equals("")){
    It gives me a nullPointerException at those three lines. I tried making a boolean argument on the startpage(); so that when it returns a null, the system will do something.. but it didnt work. I tried try and catch but I have really no idea what to do with it.

    Please help me.
    Last edited by MsBottledDream; October 12th, 2012 at 08:35 PM. Reason: I've fixed some stuff.

  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: I Seriously Need your Serious Help.

    Hi MsBottledDream and welcome to the forum.
    Please see the announcements page for the use of code tags and other facts about the forum.

    giving me JUST one error saying it's incompatible
    It is easier to offer assistance if you provide the copy-paste version of the error message.

    Edit the post above, fix the code and include the error message. Then it will be much easier to offer advice.


    Edit
    Oh, and.. What does that title have to do with the question or the code, seriously. See if you can find a better title for the post.