So, im programming with Bluej in school, and our aim was to use a generic list to graphically design a
Pokedex. Im having no problems designing the interface, but when filling the list, i have a problem.
--The List(self-made,so im providing it whole,but focused on some stuff the teacher told us needed to be in there)--
import basis.*;
public class List<ContentType>
{
private Node<ContentType> head;
private Node<ContentType> tail;
private Node<ContentType> current;
private int Listlenght;
private boolean isEmpty;
private boolean hasAccess;
public List(){
head = null;
tail = null;
current = null;
}
public int Listlenght(){
int counter= 0;
toFirst();
while(hasAccess()){
next();
counter++;
}
return counter;
}
public boolean isEmpty(){
return head== null;
}
public boolean hasAccess(){
return current!=null;
}
public void next(){
if(isEmpty())
System.out.println("Liste ist leer");
else{
if(!hasAccess())
System.out.println("Kein momentanes Current");
else{
current= current.getNachfolger();
}
}
}
public void toFirst(){
if(isEmpty())
System.out.println("Liste ist leer");
else
current=head;
}
public void toLast(){
if(isEmpty())
System.out.println("Liste ist leer");
else
current=tail;
}
public Node<ContentType> getCurrent(){
if(hasAccess)
return current;
else{
System.out.println("Kein momentanes Current");
return null;
}
}
public void setCurrent(Node<ContentType> pCurrent){
if(hasAccess){
if(pCurrent!= null)
current.setInhalt(pCurrent.getInhalt());
}
else
System.out.println("Kein momentanes Current");
}
public void append(ContentType pContent){
if(pContent != null){
Node<ContentType> N;
N =new Node<ContentType>();
N.setNachfolger(null);
N.setInhalt(pContent);
if(isEmpty){
head =N;
}
else
tail.setNachfolger(N);
tail = N;
}
}
public void insert(ContentType pContent){
if(pContent != null){
if(hasAccess()){
Node<ContentType> N;
N =new Node<ContentType>();
N.setInhalt(pContent);
N.setNachfolger(current);
getPrevious(current).setNachfolger(N);
}
else
System.out.println("Kein momentanes Current");
if(isEmpty())
append(pContent);
}
else{
System.out.println("Kein Inhalt");
}
}
public void concat(List<ContentType> pList){
if (pList != this && pList != null && !pList.isEmpty()) {
pList.toFirst();
int i;
for(i=0;i< pList.Listlenght();i++){
//append(pList.getCurrent().getInhalt());
pList.next();
}
}
}
public void remove(){
if(hasAccess ){
getPrevious(current).setNachfolger(current.getNachfolger());
next();
}
}
private Node<ContentType> getPrevious(Node<ContentType> pNode) {
if (pNode != null && pNode != head && !this.isEmpty()) {
Node<ContentType> temp = head;
while (temp != null && temp.getNachfolger() != pNode) {
temp = temp.getNachfolger();
}
return temp;
} else {
return null;
}
}
}
import basis.*;
public class Node <ContentTypeN> {
private ContentTypeN inhalt;
private Node<ContentTypeN> Nachfolger;
public Node(){
Nachfolger = null;
}
public void setInhalt(ContentTypeN ninhalt){
this.inhalt= ninhalt;
}
public void setNachfolger(Node nNachfolger){
this.Nachfolger= nNachfolger;
}
public ContentTypeN getInhalt(){
return inhalt;
}
public Node<ContentTypeN> getNachfolger(){
return Nachfolger;
}
}
-- My Object Class --
import basis.*;
public class Pokemon{
private int Index;
private String Name;
private String Typ;
public Pokemon(int pIndex,String pName, String pTyp){
Index = pIndex;
Name = pName;
Typ = pTyp;
}
public int Index(int X){
return X;
}
}
The main class
import basis.*;
public class Anwendung
{
private Fenster Window;
private Farbe Color;
private Maus mouse;
private List<Pokemon> Pokedex;
private TextFeld TFStatus;
private TextFeld TFName;
private TextFeld TFTyp;
private ZahlenFeld ZFNummer;
private Knopf Knext;
private Knopf KtoFirst;
private Knopf ktoLast;
public Anwendung(){
WindowDesign();
gottacatchthemall();
IhaveNoIdea();
}
public void IhaveNoIdea(){
Pokedex.toFirst();
Pokedex.getCurrent().getInhalt();
}
public void WindowDesign(){
Window= new Fenster(700,700);
//Window.setzeHintergrundFarbe(Color.GRUEN);
Window.setzeTitel("Pokedex");
Pokedex = new List<Pokemon>();
mouse = new Maus();
TFZFGeneration();
KnopfGeneration();
}
public void KnopfGeneration(){
Knext = new Knopf();
Knext.setzePosition(550, 300);
Knext.setzeText("next");
KtoFirst = new Knopf();
KtoFirst.setzePosition(550, 350);
KtoFirst.setzeText("toFirst");
ktoLast = new Knopf();
ktoLast.setzePosition(550, 400);
ktoLast.setzeText("toLast");
}
public void TFZFGeneration(){
TFStatus = new TextFeld();
TFStatus.setzeGroesse(120,20);
TFStatus.setzePosition(0,0);
TFStatus.setzeEditierbar(false);
TFStatus.setzeText("Methode");
TFName = new TextFeld();
TFName.setzeGroesse(120,20);
TFName.setzePosition(400,300);
TFName.setzeEditierbar(false);
TFName.setzeText("PokeName");
TFTyp = new TextFeld();
TFTyp.setzeGroesse(120,20);
TFTyp.setzePosition(400,350);
TFTyp.setzeEditierbar(false);
TFTyp.setzeText("PokeTyp");
}
public void gottacatchthemall(){
TFStatus.setzeText("gottacatchthemall");
Pokemon P;
P = new Pokemon(1,"Bisasam","Pflanze");
Pokedex.append(P);
P = new Pokemon(2,"Bisaknosp","Pflanze");
Pokedex.append(P);
P = new Pokemon(3,"Bisaflor","Pflanze");
Pokedex.append(P);
P = new Pokemon(4,"Glumanda","Pflanze");
Pokedex.append(P);
P = new Pokemon(5,"Glutexo","Feuer");
Pokedex.append(P);
P = new Pokemon(6,"Glurak","Feuer");
Pokedex.append(P);
P = new Pokemon(7,"Schiggy","Wasser");
Pokedex.append(P);
P = new Pokemon(8,"Schillok","Wasser");
Pokedex.append(P);
P = new Pokemon(9,"Turtok","Wasser");
Pokedex.append(P);
}
}
Now the Problem: Whenever i start the class "Anwendung"a error Occurs. it says:
java.lang.NullPointerException
at List.append(List.java:100)
at Anwendung.gottacatchthemall(Anwendung.java:88)
at Anwendung.<init>(Anwendung.java:25)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
at bluej.runtime.ExecServer$3.lambda$run$1(ExecServer.java:834)
at bluej.runtime.ExecServer.runOnTargetThread(ExecServer.java:930)
at bluej.runtime.ExecServer$3.run(ExecServer.java:832)
Apparently there is a problem with this line
}
else
tail.setNachfolger(N);
It seems that the tail is undefined,even though it gets defined a few lines down and this line isnt active since there is a if condition that blocks the else condition from going off when the list is empty. the list is empty at the start,i checked that.
When i remove this code it seems the list doesnt get filled at all,since the methode goFirst doesnt work at all.
The Interface
Any suggestions?
Im kinda stuck and my teacher couldnt explain it to me either, so im sorry if im a bit unspecific.
You said that "It seems that the tail is undefined,even though it gets defined a few lines down" - what do you mean by that? In Java you must first assign value - "few lines above".
if(isEmpty){
head =N;
}
else
tail.setNachfolger(N); // Use brackets
tail = N;
In the code above it seems that your are trying to call tail.setNachfolger(N) before initializing tail, so tail is null, therefore NullPointerException.
Also, please always do use brackets (near 'else'), because now it's not clear what was your intention.
It's hard to understand your code, but I think it should be:
else {
tail = N;
tail.setNachfolger(N);
}
Related
Here is a link-based Stack class.
import java.util.Iterator;
public class LStack<T>
{
private Link top;
private int size;
public LStack()
{
top = null;
size = 0;
}
#Override
public String toString()
{
StringBuffer sb = new StringBuffer();
sb.append("[");
for(Link nav = top; nav != null; nav=nav.getNext())
{
sb.append(nav.getDatum());
if(nav.getNext() != null)
{
sb.append(", ");
}
}
sb.append("]");
return sb.toString();
}
public void push(T newItem)
{
top = new Link(newItem, top);
size++;
}
public T pop()
{
if(isEmpty())
{
throw new EmptyStackException();
}
size--;
T out = top.getDatum();
top = top.getNext();
return out;
}
public T peek()
{
if(isEmpty())
{
throw new EmptyStackException();
}
return top.getDatum();
}
public void clear()
{
top = null;
size = 0;
}
public int size()
{
return size;
}
public boolean isEmpty()
{
return top == null;
}
public Iterator<T> iterator()
{
return new LStackIterator();
}
/********************aliens*******************/
class LStackIterator implements Iterator<T>
{
private Link current;
public LStackIterator()
{
current = top;
}
public T next()
{
T out = current.getDatum();
current = current.getNext();
return out;
}
public boolean hasNext()
{
return current.getNext() != null;
}
}
public static void main(String[] args)
{
LStack<String> elStack = new LStack<>();
elStack.push("A");
elStack.push("B");
elStack.push("C");
elStack.push("D");
for(String quack: elStack)
{
System.out.println(quack);
}
System.out.printf("The size is %d\n", elStack.size());
System.out.println(elStack);
System.out.println(elStack.pop());
System.out.println(elStack.pop());
System.out.println(elStack.pop());
System.out.println(elStack.pop());
System.out.println(elStack);
}
class Link
{
private T datum;
private Link next;
public Link(T datum, Link next)
{
this.datum = datum;
this.next = next;
}
public Link(T datum)
{
this(datum, null);
}
public T getDatum()
{
return datum;
}
public Link getNext()
{
return next;
}
}
}
I then try to run and get this.
$ javac LStack.java
LStack.java:95: error: incompatible types: Object cannot be converted to String
for(String quack: elStack)
^
1 error
What is happening here? Changing the type from String to Object causes it to work, but I am not using any raw types.
You must implement Iterable for "For-Each" loop
public class LStack<T> implements Iterable<T>
Hi I am currently working on a queue wait time simultaion, over the course of 12 hours that adds a random number of people per line every minute while removing three from the front every minute as well. After the twelve hours are over i will average the rate in which they entered and exited the line. I need to perform this 50 times to get a more accurate model simulation. I do not currently know how to properly implement this. If i could get some pointers on where to begin it would be most appreciated.
Linked List Class
public class LinkedListQueue<E>{
private Node<E> head;
private Node<E> tail;
private int size;
public LinkedListQueue() {
}
public void enqueue(E element) {
Node newNode = new Node(element, null);
if (size == 0) {
head = newNode;
} else {
tail.setNextNode(newNode);
}
tail = newNode;
size++;
}
public E dequeue() {
if (head != null) {
E element = head.getElement();
head = head.getNextNode();
size--;
if (size == 0) {
tail = null;
}
return element;
}
return null;
}
public E first() {
if (head != null) {
return head.getElement();
}
return null;
}
public int getSize() {
return size;
}
public void print() {
if (head != null) {
Node currentNode = head;
do {
System.out.println(currentNode.toString());
currentNode = currentNode.getNextNode();
} while (currentNode != null);
}
System.out.println();
}
}
Node Class
public class Node<E>{
private E element;
private Node<E> next;
public Node(E element, Node next) {
this.element = element;
this.next = next;
}
public void setNextNode(Node next) {
this.next = next;
}
public Node<E> getNextNode() {
return next;
}
public E getElement() {
return element;
}
public String toString() {
return element.toString();
}
}
Simulation Class
import java.util.Random;
public class Simulation {
private int arrivalRate;
//you'll need other instance variables
public Simulation(int arrivalRate, int maxNumQueues) {
this.arrivalRate = arrivalRate;
}
public void runSimulation() {
//this is an example for using getRandomNumPeople
//you are going to remove this whole loop.
for (int i = 0; i < 10; i++) {
int numPeople = getRandomNumPeople(arrivalRate);
System.out.println("The number of people that arrived in minute " + i + " is: " + numPeople);
}
}
//Don't change this method.
private static int getRandomNumPeople(double avg) {
Random r = new Random();
double L = Math.exp(-avg);
int k = 0;
double p = 1.0;
do {
p = p * r.nextDouble();
k++;
} while (p > L);
return k - 1;
}
//Don't change the main method.
public static void main(String[] args) {
Simulation s = new Simulation(18, 10);
s.runSimulation();
}
}
It looks like you haven't started this assignment at all.
First, start with the main() method. A new Simulation object is created. Follow the constructor call to new Simulation(18, 10). For starters, you will see that the constructor is incomplete
public Simulation(int arrivalRate, int maxNumQueues) {
this.arrivalRate = arrivalRate;
// missing the handling of maxNumQueues
}
So, for starters, you probably want to define a new variable of type integer (since that is what is the type of maxNumQueues according to the Simulation constructor) in the Simulation class. From there, you obviously want to get back into the constructor and set your new variable to reference the constructor call.
Example:
public class Simulation {
private int arrivalRate;
private int maxNumQueues; // keep track of the maxNumQueues
public Simulation(int arrivalRate, int maxNumQueues) {
this.arrivalRate = arrivalRate;
this.maxNumQueues = maxNumQueues; // initialize our new local variable maxNumQueues
}}
I am using the following code to make an immutable queue.
import java.util.NoSuchElementException;
import java.util.Queue;
public class ImmutableQueue<E> {
//Two stacks are used. One is to add items to the queue(enqueue) and
//other is to remove them(dequeue)
private ImmutableQueue(ReversableStack<E> order, ReversableStack<E> reverse) {
this.order = order;
this.reverse = reverse;
}
//initially both stacks are empty
public ImmutableQueue() {
this.order = ReversableStack.emptyStack();
this.reverse = ReversableStack.emptyStack();
}
public ImmutableQueue<E> enqueue(E e) {
if (null == e)
throw new IllegalArgumentException();
return new ImmutableQueue<E>(this.order.push(e), this.reverse);
}
public ImmutableQueue<E> dequeue() {
if (this.isEmpty())
throw new NoSuchElementException();
if (!this.reverse.isEmpty()) {
return new ImmutableQueue<E>(this.order, this.reverse.tail);
} else {
return new ImmutableQueue<E>(ReversableStack.emptyStack(),
this.order.getReverseStack().tail);
}
}
private static class ReversableStack<E> {
private E head; //top of original stack
private ReversableStack<E> tail; //top of reversed stack
private int size;
//initializing stack parameters
private ReversableStack(E obj, ReversableStack<E> tail) {
this.head = obj;
this.tail = tail;
this.size = tail.size + 1;
}
//returns a new empty stack
public static ReversableStack emptyStack() {
return new ReversableStack();
}
private ReversableStack() {
this.head = null;
this.tail = null;
this.size = 0;
}
//Reverses the original stack
public ReversableStack<E> getReverseStack() {
ReversableStack<E> stack = new ReversableStack<E>();
ReversableStack<E> tail = this;
while (!tail.isEmpty()) {
stack = stack.push(tail.head);
tail = tail.tail;
}
return stack;
}
public boolean isEmpty() {
return this.size == 0;
}
public ReversableStack<E> push(E obj) {
return new ReversableStack<E>(obj, this);
}
}
private ReversableStack<E> order;
private ReversableStack<E> reverse;
private void normaliseQueue() {
this.reverse = this.order.getReverseStack();
this.order = ReversableStack.emptyStack();
}
public E peek() {
if (this.isEmpty())
throw new NoSuchElementException();
if (this.reverse.isEmpty())
normaliseQueue();
return this.reverse.head;
}
public boolean isEmpty() {
return size() == 0;
}
//returns the number of items currently in the queue
public int size() {
return this.order.size + this.reverse.size;
}
public static void main(String[] args)
{
ImmutableQueue<Integer> newQueue = new ImmutableQueue<Integer>();
newQueue.enqueue(5);
newQueue.enqueue(10);
newQueue.enqueue(15);
int x = newQueue.size();
//ImmutableQueue<Integer> x = newQueue.dequeue();
System.out.println(x);
}
}
But whenever I try to do a dequeue, I get a NoSuchElementException. Also, the newQueue.size function also returns 0.
What am I doing wrong ?
Thanks
You are missing the new ImmutableQueue reference..
since enqueue() method returns a new instance of ImmutableQueue
public ImmutableQueue<E> enqueue(E e) {
if (null == e)
throw new IllegalArgumentException();
return new ImmutableQueue<E>(this.order.push(e), this.reverse);
}
But on your main method you are discarding that object
public static void main(String[] args)
{
ImmutableQueue<Integer> newQueue = new ImmutableQueue<Integer>();
newQueue.enqueue(5);
newQueue.enqueue(10);
newQueue.enqueue(15);
int x = newQueue.size();
//ImmutableQueue<Integer> x = newQueue.dequeue();
System.out.println(x);
}
change your call to:
newQueue = newQueue.enqueue(5);
int x = newQueue.size();
System.out.println(x);
and you will see the size will change
I created my own linked list, but when I tried to run it there is an error:
Exception in thread "main" java.lang.NullPointerException
at List.add(List.java:8) //if(t.val ==null)
at main.main(main.java:38) //linput.add(inputLine.split(" ")[i]);
Here is my List class:
class List{
String val;
List next=null;
private List t;
public void add(String word){
if(t.val ==null)
t.val=word;
else while(!t.next.val.equals(null))
{
t=t.next;
if(t.next.val.equals(null))
{
t.next.val=word;
break;
}
}
}
public int get(String word)
{
int i=0;
if(t.val.equals(word))
i=0;
else while(!t.next.val.equals(word))
{
t=t.next;
i++;
if(t.next.val.equals(word))
{
i++;
}
}
return i;
}
public String indexOf(int i)
{
int counter=0;
while(counter<i)
{
t=t.next;
counter++;
}
return t.val;
}
}
And here is my main function :
static public void main(String[] args)
{
List linput = new List();
String inputLine = "Hey look at me.";
for(int i = 0 ; i < inputLine.split(" ").length ; i++)
{
linput.add(inputLine.split(" ")[i]);
}
System.out.println(linput.indexOf(0)+" "+linput.indexOf(1)+" "+linput.indexOf(2));
}
I initialized t but next time there is an error like this:
private List t =new List();
Exception in thread "main" java.lang.StackOverflowError
at List.<init>(List.java:5)
at List.<init>(List.java:5)
at List.<init>(List.java:5)
Sorry. I can't give my full code, because the rest of my code is working well (reading from txt etc....).
The error seems to be related to the variable 't' (i.e., private List t).
Did you initialize this variable ? The if (t.val == null) seems to be cribbing this as t is null (uninitialized) at this point
You should have allocated object (using new) for this variable.
Can you share the full code for the constructor of List ?
Assuming you want to implement a simple forward list, rather than use the Java LinkedList class, you need to:
Change your implementation of the list to reference nodes in the list
handle traversal of the linked nodes in your word list
Here is an example:
WordList class
package com.example.words;
class WordList {
private WordNode head = null;
private int listSize = 0;
public void add(String word) {
// TODO add check for duplicate word
if (head == null) {
head = new WordNode();
head.setValue(word);
listSize++;
} else {
WordNode current = head;
while (current.getNext() != null) {
current = current.getNext();
}
WordNode newNode = new WordNode();
newNode.setValue(word);
current.setNext(newNode);
listSize++;
}
}
public int getWordIndex(String word) {
WordNode current = head;
int index = 0;
boolean found = false;
while (!found && current != null) {
found = current.getValue().equalsIgnoreCase(word);
if (!found) {
index++;
current = current.getNext();
}
}
if (found) {
return index;
} else {
return -1;
}
}
public String indexOf(int i) {
int index = 0;
WordNode current = head;
if (i <= listSize) {
while (index < i) {
current = current.getNext();
index++;
}
return current.getValue();
} else {
return null;
}
}
public int size() {
return listSize;
}
}
WordNode Class
package com.example.words;
public class WordNode {
private String value;
private WordNode next = null;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public WordNode getNext() {
return next;
}
public void setNext(WordNode link) {
next = link;
}
}
Test Driver
package com.example.words;
public class Test {
public static void main(String[] args) {
//TODO handle punctuation
WordList myList = new WordList();
String inputLine = "Hey look at me.";
String[] pieces = inputLine.split(" ");
for (int i=0; i < pieces.length; i++) {
myList.add(pieces[i]);
}
for (int i=0; i < pieces.length; i++) {
String value = myList.indexOf(i);
if (value.equalsIgnoreCase(pieces[i])) {
System.out.println("Following node is wrong:");
}
System.out.println ("node " + i + ". = " + value);
}
}
}
You tried to create t as a member variable of its own class like this:
class List {
[...]
private List t = new List();
[...]
}
This won't work because the constructor of List would be called indefinitely.
Try lazy instantiation of t instead. Replace all access of t with a getter:
private List getT() {
if (this.t == null) {
this.t = new List();
}
return t;
}
I have LinkedList with test program. As you can see in that program I add some Students to the list. I can delete them. If I choose s1,s2,s3 oraz s4 to delete everything runs well, and my list is printed properly and information about number of elements is proper. But if I delete last element (in this situation - s5) info about number of elements is still correct, but this element is still printed. Why is that so? Where is my mistake?
public class Lista implements List {
private Element head = new Element(null); //wartownik
private int size;
public Lista(){
clear();
}
public void clear(){
head.setNext(null);
size=0;
}
public void add(Object value){
if (head.getNext()==null) head.setNext(new Element(value));
else {
Element last = head.getNext();
//wyszukiwanie ostatniego elementu
while(last.getNext() != null)
last=last.getNext();
// i ustawianie jego referencji next na nowowstawiany Element
last.setNext(new Element(value));}
++size;
}
public Object get(int index) throws IndexOutOfBoundsException{
if(index<0 || index>size) throw new IndexOutOfBoundsException();
Element particular = head.getNext();
for(int i=0; i <= index; i++)
particular = particular.getNext();
return particular.getValue();
}
public boolean delete(Object o){
if(head.getNext() == null) return false;
if(head.getNext().getValue().equals(o)){
head.setNext(head.getNext().getNext());
size--;
return true;
}
Element delete = head.getNext();
while(delete != null && delete.getNext() != null){
if(delete.getNext().getValue().equals(o)){
delete.setNext(delete.getNext().getNext());
size--;
return true;
}
delete = delete.getNext();
}
return false;
}
public int size(){
return size;
}
public boolean isEmpty(){
return size == 0;
}
public IteratorListowy iterator() {
return new IteratorListowy();
}
public void wyswietlListe() {
IteratorListowy iterator = iterator();
for (iterator.first(); !iterator.isDone(); iterator.next())
{
System.out.println(iterator.current());
}
System.out.println();
}
public void infoOStanie() {
if (isEmpty()) {
System.out.println("Lista pusta.");
}
else
{
System.out.println("Lista zawiera " + size() + " elementow.");
}
}
private static final class Element{
private Object value;
private Element next; //Referencja do kolejnego obiektu
public Element(Object value){
setValue(value);
}
public void setValue(Object value) {
this.value = value;
}
public Object getValue() {
return value;
}
//ustawia referencję this.next na obiekt next podany w atgumencie
public void setNext(Element next) {
if (next != null)
this.next = next;
}
public Element getNext(){
return next;
}
}
private class IteratorListowy implements Iterator{
private Element current;
public IteratorListowy() {
current = head;
}
public void next() {
current = current.next;
}
public boolean isDone() {
return current == null;
}
public Object current() {
return current.value;
}
public void first() {
current = head.getNext();
}
}
}
test
public class Program {
public static void main(String[] args) {
Lista lista = new Lista();
Iterator iterator = lista.iterator();
Student s1 = new Student("Kowalski", 3523);
Student s2 = new Student("Polański", 45612);
Student s3 = new Student("Karzeł", 8795);
Student s4 = new Student("Pałka", 3218);
Student s5 = new Student("Konowałek", 8432);
Student s6 = new Student("Kłopotek", 6743);
Student s7 = new Student("Ciołek", 14124);
lista.add(s1);
lista.add(s2);
lista.add(s3);
lista.add(s4);
lista.add(s5);
lista.wyswietlListe();
lista.delete(s5);
lista.wyswietlListe();
lista.infoOStanie();
lista.clear();
lista.infoOStanie();
}
}
The problem is that your setNext(Element next) method does not set anything if next == null. And that is the case for the last element of your list.
So when you call delete.setNext(delete.getNext().getNext());, nothing is actually set because delete.getNext().getNext() is null!
Remove the if (next != null) condition in setNext and it will work.