|
-
November 6th, 2009, 10:09 PM
#1
Linked List dont work completely...
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
Last edited by Tekdg; November 7th, 2009 at 11:01 AM.
-
November 6th, 2009, 10:11 PM
#2
Re: Linked List dont work completely...
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
}
}
}
-
November 7th, 2009, 04:19 AM
#3
Re: Linked List dont work completely...
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.
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
November 7th, 2009, 10:58 AM
#4
Re: Linked List dont work completely...
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.
Code:
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);
-
November 8th, 2009, 01:23 PM
#5
Re: Linked List dont work completely...
 Originally Posted by Tekdg
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
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|