Click to See Complete Forum and Search --> : Linked List dont work completely...


Tekdg
November 6th, 2009, 09:09 PM
Well, my problem is with the list... when i called number() to insert the number to the list, and print it out it works
but when i called display() i got NullPointerExcpetion error....can anyone see why my listed turn empty?

PS: dont worry about extra stuffs tho

Program is below

Tekdg
November 6th, 2009, 09:11 PM
Sorry, i accidently took out row and col

public class Grid
{
private Node head;
private Node tail;
//private Node current;
int row = 10;
int col = 6;

public Grid()
{
head = null;
tail = null;
}

public Grid(Value n)
{
head = new Node(n);
tail = head;
//current = head;
}

private void insert(Value n)
{
Node cell = new Node(n); //create node

if(head == null)
{
head = cell;
tail = cell;
}
else
{
tail.setRight(cell);
//cell.setRight(current);
tail = cell;
}
}

public void display()
{
System.out.println(head.getValue()+", ");
Node temp = head;
while(temp!=null) //loops forward displaying node's data
{
System.out.print(temp.getValue()+", ");
temp = temp.getRight(); //move to the next node in the list
}
}

public void number()
{
for(double i = 0; i <= row*col - 1; i++)
{
insert(new Value(Double.toString(i)));
System.out.print(tail.getValue() + " ");
}

System.out.println("");

System.out.println(tail.getValue()+", ");
Node temp = head;
while(temp!=null) //loops forward displaying node's data
{
System.out.print(temp.getValue() + ", ");
temp = temp.getRight(); //move to the next node in the list
}

}

}

dlorde
November 7th, 2009, 03:19 AM
When posting code, use the [CODE]...[/CODE] tags to keep it formatted and readable. If you want help with errors, post the full error message text and stack trace if present. The error message should tell you on which line the error occurs.

As far as I can see, if Node and Value work correctly (I don't know, you didn't post that code), the Grid display method will only throw a NullPointerException if the Grid is empty (and, for some reason, when it isn't empty it always prints the first node twice).

Unless you post the full text of the exception, that's all I can say.

Experience is a poor teacher: it gives its tests before it teaches its lessons...
Anon.

Tekdg
November 7th, 2009, 09:58 AM
Thanks dlord,
I found my problem, it was in my main class
At first i accidently put my Grid grid = new Grid(); inside the loop, so basicly i creted a new linked each time i loop. Well, if u got any suggestion please let me know.


public static void main(String[] args) throws IOException
{
Grid grid = new Grid();

int loop = 0;
String line;

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

do {
System.out.println("Operations");
System.out.println(" display dis assign cell as");
System.out.println(" fill f number n");
System.out.println(" add cells a subtract cells s");
System.out.println(" multiply cells m divide cells d");
System.out.println(" add rows ar subtract rows sr");
System.out.println(" multiply rows mr divide rows dr");
System.out.println(" add columns ac subtract columns sc");
System.out.println(" multiply columns mc divide columns dc");
System.out.println(" insert row ir insert column ic");
System.out.println(" delete row delr delete column delc");
System.out.println(" quit q");
System.out.print("-> ");


try {
//Grid grid = new Grid();
line = in.readLine();

if(line.equals("dis")) {
grid.display();
}
else if(line.equals("n")) {
grid.number();
}
else if(line.equals("q")){ //quit program
System.exit (0);
}
}

catch (IOException ioe)
{
System.out.println("IO error trying to read your name!");
System.exit(1);
}
} while(loop == 0);

dlorde
November 8th, 2009, 12:23 PM
Well, if u got any suggestion please let me know.
Suggestion about what? If you have a question, ask it.

A prudent question is one-half of wisdom...
F. Bacon