Amar Amar - 1 year ago 99
Java Question

Get all elements in a LinkedList, exception

I need to get all the elements in the linked list. I do this by calling the

method in my buffer (CharacterBuffer).

But every time I try to get the elements I get a nullPointerException. I don't know how to solve this.

public class Reader extends Thread {
private GUIMutex gui;
private CharacterBuffer buffer;

public Reader(GUIMutex gui, CharacterBuffer buffer) {
this.gui = gui;
this.buffer = buffer;

public void run() {
String data = "test";
while (true) {

try {
gui.setReaderText(data + "\n");
} catch (InterruptedException e) {


public class CharacterBuffer {

private char ch;
private LinkedList buffer = new LinkedList();
private boolean filled = true;

public void put(char ch) {

public Object get() throws InterruptedException {
while (buffer.isEmpty()) {

// return buffer.removeFirst();
return buffer.getFirst();


Answer Source

First thing You can not use char in Linked List it only accept Wrapper type. And in your put method you are adding and removing character at the same time. No value will remain in your linkedlist.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download