Another Object Question

In the main method of the attached class I'm instantiating an object called a node ( which will be used in a LinkedList). From the object, I'm trying to call the methods that alter the data included in the node, but as before, I'm having trouble getting the compiler to work. It keeps giving me a "cannot find symbol" error. Can you please have a look at these classes to determine why I'm not able to call a simple method here? I used jgrasp on the driver and found that it instantiates the node correctly, and sets the field values to null as the constructor is supposed to.

The driver:

import packages.assignments.util.*;

public class Lab8 {

public static void main (String[] Args) {

LinkedList LinkedList1 = new LinkedList();
System.out.println(LinkedList1);

Node theNode = new Node();

//This is the line that generated the compiler error.
theNode.setNodeValue("Blue");

}
}

The Node class and LinkedList class follow:

//////The Node Class:////

package packages.assignments.util;

public class Node {
public T nodeValue;
public Node link;

// Sets the value of the data in a node.
public void setNodeValue(T item) {
this.nodeValue = item;
}

// Sets the address of the next node in the list.
public void setLink(Node item) {
this.link = item;
}

// Gets the address of the next node in the list.
public Node getLink() {
return this.link;
}

// Gets the data value in the node.
public T getNodeValue() {
return this.nodeValue;
}

// Default Constructor for Node class.
public Node() {
nodeValue = null;
link = null;
}

//Constructor for Node class.
public Node(T item) {
nodeValue = item;
link = null;
}

}
////The Linked List CLass/////
package packages.assignments.util;
public class LinkedList {

private Node head;
private Node pointer;
private int count;

//clears the entire list
public void clear() {
this.head = null;
this.count = 0;
}

//returns true if the list is empty.
public boolean isEmpty() {
return (count==0);
}

// Constructor for the Linked List class.
public LinkedList() {
head = null;
pointer = head;
count=0;
}
public void addNode(int newData) {
head = new Node(newData);
pointer = head;
count = 1;
}
public T getData() {
return this.pointer.getNodeValue();
}
public Node getNext() {
return this.pointer;
}

}

Comments

  • : In the main method of the attached class I'm instantiating an object
    : called a node ( which will be used in a LinkedList). From the
    : object, I'm trying to call the methods that alter the data included
    : in the node, but as before, I'm having trouble getting the compiler
    : to work. It keeps giving me a "cannot find symbol" error. Can you
    : please have a look at these classes to determine why I'm not able to
    : call a simple method here? I used jgrasp on the driver and found
    : that it instantiates the node correctly, and sets the field values
    : to null as the constructor is supposed to.
    :
    : The driver:
    :
    : import packages.assignments.util.*;
    :
    : public class Lab8 {
    :
    : public static void main (String[] Args) {
    :
    : LinkedList LinkedList1 = new LinkedList();
    : System.out.println(LinkedList1);
    :
    : Node theNode = new Node();
    :
    : //This is the line that generated the compiler error.
    : theNode.setNodeValue("Blue");
    :
    : }
    : }
    :
    : The Node class and LinkedList class follow:
    :
    : //////The Node Class:////
    :
    : package packages.assignments.util;
    :
    : public class Node {
    : public T nodeValue;
    : public Node link;
    :
    : // Sets the value of the data in a node.
    : public void setNodeValue(T item) {
    : this.nodeValue = item;
    : }
    :
    : // Sets the address of the next node in the list.
    : public void setLink(Node item) {
    : this.link = item;
    : }
    :
    : // Gets the address of the next node in the list.
    : public Node getLink() {
    : return this.link;
    : }
    :
    : // Gets the data value in the node.
    : public T getNodeValue() {
    : return this.nodeValue;
    : }
    :
    : // Default Constructor for Node class.
    : public Node() {
    : nodeValue = null;
    : link = null;
    : }
    :
    : //Constructor for Node class.
    : public Node(T item) {
    : nodeValue = item;
    : link = null;
    : }
    :
    : }
    : ////The Linked List CLass/////
    : package packages.assignments.util;
    : public class LinkedList {
    :
    : private Node head;
    : private Node pointer;
    : private int count;
    :
    : //clears the entire list
    : public void clear() {
    : this.head = null;
    : this.count = 0;
    : }
    :
    : //returns true if the list is empty.
    : public boolean isEmpty() {
    : return (count==0);
    : }
    :
    : // Constructor for the Linked List class.
    : public LinkedList() {
    : head = null;
    : pointer = head;
    : count=0;
    : }
    : public void addNode(int newData) {
    : head = new Node(newData);
    : pointer = head;
    : count = 1;
    : }
    : public T getData() {
    : return this.pointer.getNodeValue();
    : }
    : public Node getNext() {
    : return this.pointer;
    : }
    :
    : }
    :
    Where does the compiler give the error?
  • : : In the main method of the attached class I'm instantiating an object
    : : called a node ( which will be used in a LinkedList). From the
    : : object, I'm trying to call the methods that alter the data included
    : : in the node, but as before, I'm having trouble getting the compiler
    : : to work. It keeps giving me a "cannot find symbol" error. Can you
    : : please have a look at these classes to determine why I'm not able to
    : : call a simple method here? I used jgrasp on the driver and found
    : : that it instantiates the node correctly, and sets the field values
    : : to null as the constructor is supposed to.
    : :
    : : The driver:
    : :
    : : import packages.assignments.util.*;
    : :
    : : public class Lab8 {
    : :
    : : public static void main (String[] Args) {
    : :
    : : LinkedList LinkedList1 = new LinkedList();
    : : System.out.println(LinkedList1);
    : :
    : : Node theNode = new Node();
    : :
    : : //This is the line that generated the compiler error.
    : : theNode.setNodeValue("Blue");
    : :
    : : }
    : : }
    : :
    : : The Node class and LinkedList class follow:
    : :
    : : //////The Node Class:////
    : :
    : : package packages.assignments.util;
    : :
    : : public class Node {
    : : public T nodeValue;
    : : public Node link;
    : :
    : : // Sets the value of the data in a node.
    : : public void setNodeValue(T item) {
    : : this.nodeValue = item;
    : : }
    : :
    : : // Sets the address of the next node in the list.
    : : public void setLink(Node item) {
    : : this.link = item;
    : : }
    : :
    : : // Gets the address of the next node in the list.
    : : public Node getLink() {
    : : return this.link;
    : : }
    : :
    : : // Gets the data value in the node.
    : : public T getNodeValue() {
    : : return this.nodeValue;
    : : }
    : :
    : : // Default Constructor for Node class.
    : : public Node() {
    : : nodeValue = null;
    : : link = null;
    : : }
    : :
    : : //Constructor for Node class.
    : : public Node(T item) {
    : : nodeValue = item;
    : : link = null;
    : : }
    : :
    : : }
    : : ////The Linked List CLass/////
    : : package packages.assignments.util;
    : : public class LinkedList {
    : :
    : : private Node head;
    : : private Node pointer;
    : : private int count;
    : :
    : : //clears the entire list
    : : public void clear() {
    : : this.head = null;
    : : this.count = 0;
    : : }
    : :
    : : //returns true if the list is empty.
    : : public boolean isEmpty() {
    : : return (count==0);
    : : }
    : :
    : : // Constructor for the Linked List class.
    : : public LinkedList() {
    : : head = null;
    : : pointer = head;
    : : count=0;
    : : }
    : : public void addNode(int newData) {
    : : head = new Node(newData);
    : : pointer = head;
    : : count = 1;
    : : }
    : : public T getData() {
    : : return this.pointer.getNodeValue();
    : : }
    : : public Node getNext() {
    : : return this.pointer;
    : : }
    : :
    : : }
    : :
    : Where does the compiler give the error?

    The error happens at the following line in the driver program:

    theNode.setNodeValue("Blue");

    I can comment out this line and it compiles just fine. As soon as I try to compile without it I get the "cannot find symbol" and the compiler identified this line as the culprit. The compiler error also says the following:

    symbol : method setNodeValue(java.lang.string)
    location: class Node
    theNode.setNodeValue("Blue");

  • : : : In the main method of the attached class I'm instantiating an object
    : : : called a node ( which will be used in a LinkedList). From the
    : : : object, I'm trying to call the methods that alter the data included
    : : : in the node, but as before, I'm having trouble getting the compiler
    : : : to work. It keeps giving me a "cannot find symbol" error. Can you
    : : : please have a look at these classes to determine why I'm not able to
    : : : call a simple method here? I used jgrasp on the driver and found
    : : : that it instantiates the node correctly, and sets the field values
    : : : to null as the constructor is supposed to.
    : : :
    : : : The driver:
    : : :
    : : : import packages.assignments.util.*;
    : : :
    : : : public class Lab8 {
    : : :
    : : : public static void main (String[] Args) {
    : : :
    : : : LinkedList LinkedList1 = new LinkedList();
    : : : System.out.println(LinkedList1);
    : : :
    : : : Node theNode = new Node();
    : : :
    : : : //This is the line that generated the compiler error.
    : : : theNode.setNodeValue("Blue");
    : : :
    : : : }
    : : : }
    : : :
    : : : The Node class and LinkedList class follow:
    : : :
    : : : //////The Node Class:////
    : : :
    : : : package packages.assignments.util;
    : : :
    : : : public class Node {
    : : : public T nodeValue;
    : : : public Node link;
    : : :
    : : : // Sets the value of the data in a node.
    : : : public void setNodeValue(T item) {
    : : : this.nodeValue = item;
    : : : }
    : : :
    : : : // Sets the address of the next node in the list.
    : : : public void setLink(Node item) {
    : : : this.link = item;
    : : : }
    : : :
    : : : // Gets the address of the next node in the list.
    : : : public Node getLink() {
    : : : return this.link;
    : : : }
    : : :
    : : : // Gets the data value in the node.
    : : : public T getNodeValue() {
    : : : return this.nodeValue;
    : : : }
    : : :
    : : : // Default Constructor for Node class.
    : : : public Node() {
    : : : nodeValue = null;
    : : : link = null;
    : : : }
    : : :
    : : : //Constructor for Node class.
    : : : public Node(T item) {
    : : : nodeValue = item;
    : : : link = null;
    : : : }
    : : :
    : : : }
    : : : ////The Linked List CLass/////
    : : : package packages.assignments.util;
    : : : public class LinkedList {
    : : :
    : : : private Node head;
    : : : private Node pointer;
    : : : private int count;
    : : :
    : : : //clears the entire list
    : : : public void clear() {
    : : : this.head = null;
    : : : this.count = 0;
    : : : }
    : : :
    : : : //returns true if the list is empty.
    : : : public boolean isEmpty() {
    : : : return (count==0);
    : : : }
    : : :
    : : : // Constructor for the Linked List class.
    : : : public LinkedList() {
    : : : head = null;
    : : : pointer = head;
    : : : count=0;
    : : : }
    : : : public void addNode(int newData) {
    : : : head = new Node(newData);
    : : : pointer = head;
    : : : count = 1;
    : : : }
    : : : public T getData() {
    : : : return this.pointer.getNodeValue();
    : : : }
    : : : public Node getNext() {
    : : : return this.pointer;
    : : : }
    : : :
    : : : }
    : : :
    : : Where does the compiler give the error?
    :
    : The error happens at the following line in the driver program:
    :
    : theNode.setNodeValue("Blue");
    :
    : I can comment out this line and it compiles just fine. As soon as I
    : try to compile without it I get the "cannot find symbol" and the
    : compiler identified this line as the culprit. The compiler error
    : also says the following:
    :
    : symbol : method setNodeValue(java.lang.string)
    : location: class Node
    : theNode.setNodeValue("Blue");
    :
    :
    Appearantly your compiler doesn't compile a string litaral as a String object. Try to use
    [code]
    theNode.setNodeValue(new String("Blue"));
    [/code]
    instead.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories