T
Taria
Hello all,
I am creating a linked list module, and am currently developing the
linked list insert section of my program. I am still testing the
logic but I've encountered a runtime error
In short, I create a tempList of LinkedList class, I insert a value
using the insert method and when it comes back, the value is not
shown in the tempList like I thought it should have.
I think I'm calling it correctly to be initialized, and I'm surprised
the debugger I'm using hasn't blown up by now, I've ran it so many
times. I'm not understanding why the value of '3' is not reflected in
the tempList's data after calling the insert method. It returns as
null. Because it's null that's why I encounter a runtime error for
the 2nd insertation.
I included a copy of my program, stripped of all extraneous lines. I
apology in advance for the rough style of my program. I tried very
hard to put it into the form that SSCCE dictates. I'm not concerned
as much as to whether the linked list's logic is working as I am with
the technical aspect of objects and how to manipulate them .
(objects...I dream about this stuff now lol.)
Any help is appreciated,
-t
import java.lang.*;
public class MyProg{
public static void main(String[] arguments) {
int size = 10; //size to be read in from input of file later
LinkedList [] bucketArray = new LinkedList[ size ];
Integer three = new Integer(3);
Integer four = new Integer(4);
LinkedList tempList = new LinkedList();
tempList.insert ( three, 1 );
tempList.insert ( four, 2 );
bucketArray[0] = tempList;
}
//end of main
}
class LinkedList{
private Node head;
private int length;
public LinkedList() {
this.head = null;
this.length = 0;
}
public Object getItemAtPosition(int position) {
if (position<0 || position >= this.length){
return null;
}
Node curr = this.head;
for (int i=0;i<position;i++){
curr = curr.getNext();
}
return curr.getData();
}
public Node getNodeAtPosition(int position) {
if (position<0 || position > this.length){
return null;
}
Node curr = this.head;
for (int i=0;i < position;i++){
curr = curr.getNext(); // <-- null pointer exception
}
return curr;
}
public boolean insert (Object data,int position){
if (position<0 || position-1 > this.length ){
return false;
}
Node previous = this.getNodeAtPosition(position-1);
Node newNode = new Node(data);
if (position - 1 == 0) { //list is empty
newNode.setNext(null);
newNode.setData(data);
}
else {
newNode.setNext(previous.getNext());
previous.setNext(newNode);
}
length++;
return true;
}
}
class Node {
private Object data;
private Node next;
public Node(Object data){
this.data = data;
}
public Node(Object data,Node next){
this.data = data;
this.next = next;
}
public void setNext(Node next){
this.next = next;
}
public void setData(Object data){
this.data = data;
}
public Node getNext(){
return this.next;
}
public Object getData(){
return this.data;
}
}
I am creating a linked list module, and am currently developing the
linked list insert section of my program. I am still testing the
logic but I've encountered a runtime error
In short, I create a tempList of LinkedList class, I insert a value
using the insert method and when it comes back, the value is not
shown in the tempList like I thought it should have.
I think I'm calling it correctly to be initialized, and I'm surprised
the debugger I'm using hasn't blown up by now, I've ran it so many
times. I'm not understanding why the value of '3' is not reflected in
the tempList's data after calling the insert method. It returns as
null. Because it's null that's why I encounter a runtime error for
the 2nd insertation.
I included a copy of my program, stripped of all extraneous lines. I
apology in advance for the rough style of my program. I tried very
hard to put it into the form that SSCCE dictates. I'm not concerned
as much as to whether the linked list's logic is working as I am with
the technical aspect of objects and how to manipulate them .
(objects...I dream about this stuff now lol.)
Any help is appreciated,
-t
import java.lang.*;
public class MyProg{
public static void main(String[] arguments) {
int size = 10; //size to be read in from input of file later
LinkedList [] bucketArray = new LinkedList[ size ];
Integer three = new Integer(3);
Integer four = new Integer(4);
LinkedList tempList = new LinkedList();
tempList.insert ( three, 1 );
tempList.insert ( four, 2 );
bucketArray[0] = tempList;
}
//end of main
}
class LinkedList{
private Node head;
private int length;
public LinkedList() {
this.head = null;
this.length = 0;
}
public Object getItemAtPosition(int position) {
if (position<0 || position >= this.length){
return null;
}
Node curr = this.head;
for (int i=0;i<position;i++){
curr = curr.getNext();
}
return curr.getData();
}
public Node getNodeAtPosition(int position) {
if (position<0 || position > this.length){
return null;
}
Node curr = this.head;
for (int i=0;i < position;i++){
curr = curr.getNext(); // <-- null pointer exception
}
return curr;
}
public boolean insert (Object data,int position){
if (position<0 || position-1 > this.length ){
return false;
}
Node previous = this.getNodeAtPosition(position-1);
Node newNode = new Node(data);
if (position - 1 == 0) { //list is empty
newNode.setNext(null);
newNode.setData(data);
}
else {
newNode.setNext(previous.getNext());
previous.setNext(newNode);
}
length++;
return true;
}
}
class Node {
private Object data;
private Node next;
public Node(Object data){
this.data = data;
}
public Node(Object data,Node next){
this.data = data;
this.next = next;
}
public void setNext(Node next){
this.next = next;
}
public void setData(Object data){
this.data = data;
}
public Node getNext(){
return this.next;
}
public Object getData(){
return this.data;
}
}