Results 1 to 4 of 4
  1. #1
    patstan is offline Member
    Join Date
    Oct 2014
    Posts
    13
    Rep Power
    0

    Default Getting NullPointer Exception Error in Simple ArrayList program

    Hi,

    I am creating a simple ArrayList program that would enable one to input their username to it using a scanner. However, i am getting this error:

    "Exception in thread "main" java.lang.NullPointerException
    at home.Members.addUser(Members.java:16)
    at home.Main.main(Main.java:14)"


    Here is the code! :

    Main.java class
    Java Code:
    import java.util.Scanner;
    
    public class Main {
    
    	public static void main(String[] args) {
    		String username;
    		Members online = new Members();
    		Scanner input = new Scanner(System.in);
    		
    		System.out.println("Enter your username");
    		username = input.nextLine();		
    		online.addUser(username);
    		
    		
    
    	}
    }
    Members.java class
    Java Code:
    package home;
    import java.util.ArrayList;
    
    public class Members {
    	
    	public ArrayList<String> users;
    	
    	public Members() {
    		
    		ArrayList<String> users = new ArrayList<>();
    		
    	}
    	
    	public void addUser(String username) {
    		
    		users.add(username);
    		
    	}	
    
    }
    Im guessing this is a really simple mistake somewhere but i cant figure it out.

    Thank you!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Getting NullPointer Exception Error in Simple ArrayList program

    You are redeclaring your list inside your constructor. That hides your instance declaration of the same.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    patstan is offline Member
    Join Date
    Oct 2014
    Posts
    13
    Rep Power
    0

    Default Re: Getting NullPointer Exception Error in Simple ArrayList program

    Quote Originally Posted by jim829 View Post
    You are redeclaring your list inside your constructor. That hides your instance declaration of the same.

    Regards,
    Jim
    Thank you, but how do i fix it? I always thought you had to initialize it inside the constructor for it to be created?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Getting NullPointer Exception Error in Simple ArrayList program

    Initialization and declaration are different. Here is a declaration.
    Java Code:
    int a;
    here is an initialization.

    Java Code:
    a = 1;
    here are both together.

    Java Code:
    int a = 1;
    Remove the declaration of users from inside the Members constructor. Just initialize what you have already declared.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. nullpointer exception
    By empire in forum Eclipse
    Replies: 3
    Last Post: 06-12-2013, 03:31 AM
  2. NULLPointer Exception
    By phyzix5761 in forum New To Java
    Replies: 7
    Last Post: 11-08-2012, 04:53 AM
  3. Nullpointer exception
    By Games2Design in forum New To Java
    Replies: 4
    Last Post: 10-20-2012, 12:15 AM
  4. Nullpointer exception
    By naveen516 in forum New To Java
    Replies: 2
    Last Post: 12-19-2011, 04:36 AM
  5. NullPointer exception
    By bdario1 in forum New To Java
    Replies: 15
    Last Post: 03-17-2010, 04:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •