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 4 of 4

Thread: Java nulpointer errsors

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java nulpointer errsors

    Hi I have some problem with my java coding. I cannot figure out hot to point the an object. the code looks like this. I have two classes. User and Users. Users class, which manages a user list as Linked List, is the class that adds, removes and so on. I have to code a method as well than sends message from one user to another user. the method live in User class and the also another send method is located in Users class which will be called by User - send() to access the list. since main constructor of Users is set to drive whole program. I have to write another constructor. But I am stuck here.

    public class User{
            private Users users;
            private String name;
            ...
            public void send(){
                users.send();
            }
        }
     
        public class Users{
            private LinkedList<Users> users = new LinkedList<User>();
            public static void main(String[] args){
                new Users();
        }
            private void send(){
            }
        }
    I am getting Nullpointer errsors[COLOR="Silver"]
     
    --- Update ---
     
    [/COLOR]package assignment;
     
    import java.util.*;
     
    public class Users 
    {
    	public static void main(String[] args)
    	{
    		new Users();
    	}
     
    	private LinkedList<User> users = new LinkedList<User>();
    	private LinkedList<Email> emails = new LinkedList<Email>();
     
    	public Users()
    	{		
    		menu();
    	}
    	public Users(User user)
    	{
    		users.add(new User(this));
    	}
     
    	private void menu()
    	{
    		char c = readChoice();
    		while (!isEnd(c))
    		{
    			execute(c);
    			c = readChoice();
    		}
    	}
    	private char readChoice()
    	{
    		System.out.print("Choice (a/d/g/u/x): ");
    		return In.nextChar();
    	}
    	private boolean isEnd(char c)
    	{
    		return c == 'x';
    	}
    	private void execute(char c)
    	{
    		switch(c)
    		{
    		case 'a': add(); break;
    		case 'd': delete(); break;
    		case 'g': break;
    		case 'u': use(); break;
    		default : System.out.println("    Invalid choice");
    		}
    	}
     
    	private void add()
    	{
    		User user = new User();
    		if (!exists(user.getName()))
    		{	
    			users.add(user);
    			System.out.println("    User " + getIndex(user) + ": " + user.getName());
    		}
    		else
    			System.out.println("already exists");
    	}
     
    	private int getIndex(User user)
    	{
    		int index = users.indexOf(user) + 1;
    		return index;
    	}
     
    	private void delete()
    	{
    		User user = user(readName());
    		if (user != null)
    			users.remove(user);
    		else
    			System.out.println("  No such name");
    	}
     
    	public void send()
    	{
    		//System.out.println("hi");
    		if (user(readName()) != null)
    			System.out.println("I am here");
    		else
    			System.out.println("sorry not here");
    	}
     
    	private void use()
    	{
    		User user = new User();
    		if (exists(user.getName()))
    			user.use();
    		else
    			System.out.println("    No such user");
    	}
     
    	private boolean exists(String name)
    	{
    		return user(name) != null;
    	}
     
    	private User user(String name)
    	{
    		for (User user: users)
    			if (user.matches(name))
    				return user;
    		return null;
    	}
     
    	private String readName()
    	{
    		System.out.print("  Names: ");
    		return In.nextLine();
    	}
    }[COLOR="Silver"]
     
    --- Update ---
     
    [/COLOR]package assignment;
     
    import java.util.*;
     
    public class User 
    {
    	private String name;
    	private Users users;
     
    	public User()
    	{
    		this.name = readName();
    	}
    	public User(Users users)
    	{
    		this.users = users;
    	}
    	private String readName()
    	{
    		System.out.print("  Name: ");
    		return In.nextLine();
    	}
    	public boolean matches(String name)
    	{
    		return this.name.equals(name);
    	}
    	public String getName()
    	{
    		return this.name;
    	}
     
    	public void use()
    	{
    		char c = readChoice();
    		while (!isEnd(c))
    		{
    			execute(c);
    			c = readChoice();
    		}
    	}
    	private char readChoice()
    	{
    		System.out.print("  Choice (l/r/s/d/x): ");
    		return In.nextChar();
    	}
    	private boolean isEnd(char c)
    	{
    		return c == 'x';
    	}
    	private void execute(char c)
    	{
    		switch(c)
    		{
    		case 'l': look(); break;
    		case 'r': read(); break;
    		case 's': send(); break;
    		default : System.out.println("    Invalid choice");
    		}
    	}
    	private void look()
    	{
     
    	}
    	private void read()
    	{
     
    	}
    	private void send()
    	{
    		users = new Users(this);
    		users.send();
    	}
     
    }


    --- Update ---

    these are the actual codes


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java nulpointer errsors

    I have to write another constructor. But I am stuck here.
    Please explain the problem.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java nulpointer errsors

    when I try to use send method in the User class I am getting Null pointer exception as it is not initialized with new User(). The reason I have not done that is that new User bill bring me back to the begin of the program as it meant to be so. I'll have to write another constructor to point the users.send() from User class to Users class. However, I don't know how to do the second constructor.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java nulpointer errsors

    The posted code will not compile for testing. The ...
    Can you make a small complete program that compiles, executes and shows the problem?

    Also copy the full text of the error message and post it here.
    If you don't understand my answer, don't ignore it, ask a question.

Tags for this Thread