Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

NullPointer Exception

Hi, Im doing a project of my own but it is similar to intro to Data Structures project. I've written a method to read user messages from a list.

public void read(String user){
        List<Post> userPosts = wall.getPostsByUser(user);
        List<String> userMessages = new ArrayList<>();
        for(Post post : userPosts){
            userMessages.add(post.getMessage());
        }
       System.out.print(userMessages);
    }

When I run my test it returns with NullPointer Exception.

To sum up questions:

  1. How do I read System.out.print()
  2. Does this method work as intended?

2 Answers

  1. If you want each post printed out, you could move System.out.print() into your for method above. And change it to System.out.print(post.getMessage())

  2. It looks like it should work, but hard to say without seeing the whole class file. For example, first line in your method: List<Post> userPosts = wall.getPostsByUser(user); I can not see where wall is defined.

Thanks for the response!

Actions.java

package com.home.menu;

import com.home.model.Post;
import com.home.model.Wall;
import com.home.userIO.UserInput;

import java.util.ArrayList;
import java.util.List;

public class Actions {
    private UserInput ui;
    private Wall wall;

    public Actions(){
    ui = new UserInput();
    wall = new Wall();

    }

    public void read(String user){
       List<Post> userPosts =  wall.getPostsByUser(user);
       List<String> messagesInPosts = new ArrayList<>();
       for(Post post : userPosts){
           messagesInPosts.add(post.getMessage());
       }
       for(String message : messagesInPosts) {
           System.out.print(message);
       }
    }

    public void post(String input){
        Post post = ui.createPostFromInput(input);
        wall.addPost(post);
    }

    public void wall(String user){

    }
    public void follow(String user,String targetUser){}
}

Maybe your wall is null. When you test this, are you creating a user and some posts on the user's wall?

yeah I do:

ActionTest.java

package com.home.menu;


import com.home.model.Post;
import com.home.model.Wall;
import com.home.userIO.UserInput;
import org.junit.Before;
import org.junit.Test;
import java.time.LocalDateTime;

import static junit.framework.TestCase.fail;
import static org.junit.Assert.assertEquals;


public class ActionsTest {

    private Actions action;
    private UserInput ui;
    private Post post0;
    private Post post1;
    private Wall wall;

    @Before
    public void setUp() {
        ui = new UserInput();
        action = new Actions();
        LocalDateTime time = LocalDateTime.now();
        post0 = new Post("Alex", "Hello World!", time);
        post1 = new Post("Alex,", "working hard", time);
        wall = new Wall();
        wall.addPost(post0);
        wall.addPost(post1);
    }

    //TODO: Prep mock for reading sout.
    @Test
    public void readPrintsUserMessages() {
        fail();
    }

    @Test
    public void postingAddsPostToWall(){
        action.post("Adele -> smoking outside");
        assertEquals(1,  wall.getPostsByUser("Adele").size());
    }

    @Test
    public void somethign(){
        Post superPost = ui.createPostFromInput("Adele -> smoking outside");
        wall.addPost(superPost);
        assertEquals("smoking outside", wall.usersAndPosts().get("Adele").get(0).getMessage());
    }
}