Working on a class at the moment in NetBeans however I have a problem with a section of code;
Code:
public void init() {
itemList = new ArrayList();
GUI gui1 = new GUI();
getContentPane().add(gui1, "Center");
}
When built it brings up the following error;
Code:
NetBeansProjects\ToDoList\src\ToDoList.java:26: cannot find symbol
symbol : method add(ToDoList.GUI,java.lang.String)
location: class java.awt.Container
getContentPane().add(gui1, "Center");
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
I've included the full code below;
Code:
/* Small application implementing a To-Do List.
* Uses a mobile phone-like interface.
*/
// first we create an instance of this class
class GUI{}
public ToDoList() {
// create a graphical user interface,
// passing it a reference to this tdl program object
// initialisation goes here:
}
public void init() {
itemList = new ArrayList();
GUI gui1 = new GUI();
getContentPane().add(gui1, "Center");
}
public ArrayList getList() {
// dummy return value of an empty list,
// just to get the program to compile initially
return itemList;
}
public ArrayList addToList(String s) {
itemList.add(s);
return itemList;
}
public ArrayList getSortedList() {
// dummy return value of an empty list,
// just to get the program to compile initially
Collections.sort(itemList);
return itemList;
}
public ArrayList performReverse() {
// dummy return value of an empty list,
// just to get the program to compile initially
Collections.reverse(itemList);
return itemList;
}
public ArrayList performDeletion(int i) {
// dummy return value of an empty list,
// just to get the program to compile initially
if(i < itemList.size())
itemList.remove(i);
return itemList;
}
public ArrayList performSwap(int i) {
// dummy return value of an empty list,
// just to get the program to compile initially
if(i < itemList.size() && itemList.size() >=2)
{
String s = (String)itemList.remove(i);
if(i < itemList.size())
itemList.add(i + 1, s);
else
itemList.add(itemList.size() - 1, s);
}
return itemList;
}
private GUI gui;
private ArrayList itemList;
}
Suggestions would be appreciated!
10-13-2009, 04:36 AM
rohitsharma52
Hi,
First of this problem has got nothing to do with NetBeans. Please use correct heading for the problem description.
Coming to the compilation error, this is due to the reason that wrong method signature is being used here. The correct signature is:
Code:
add(String name, Component comp)
So you can fix this error by using the above signature. Another important thing is that your GUI class should be of type
Code:
Component
.
For example :
Code:
class GUI extends Component {}
Regards.
10-13-2009, 05:51 AM
p3t3r_uk
Thanks for your reply. I have declared;
Code:
import java.awt.Component;
and changed the troublesome block of code as follows;
Code:
public class GUI extends Component {
public void init() {
itemList = new ArrayList();
GUI gui1 = new GUI();
add(Component, gui1);
}
}
The issue is still with add(Component, gui1); it has now underlined "Component" with the "cannot find symbol" error.
10-13-2009, 06:32 AM
rohitsharma52
Hi,
Please pay attention to the method signature you are trying to use:
Code:
add(String name, Component comp)
The first argument should be of type
Code:
java.lang.String
for example: "Center".
The second argument should be of type
Code:
java.awt.Component
for example
Code:
class GUI extends Component
.
In your case the second argument is rightly specified as 'gui1', but its the first argument, which should be a String.
Regards.
10-13-2009, 08:26 AM
p3t3r_uk
Hi,
Thanks for all your help, I'm very much a beginner with this! I seem to have fixed all the errors however now my applet just runs as a blank window, no graphic displayed.
I've included all of the latest code below;
ToDoList.java
Code:
/* Small application implementing a To-Do List.
* Uses a mobile phone-like interface.
*/
// first we create an instance of this class
public ToDoList() {
// create a graphical user interface,
// passing it a reference to this tdl program object
// initialisation goes here:
}
class GUI extends Component {
public void init() {
itemList = new ArrayList();
GUI gui1 = new GUI();
getContentPane().add(gui1, "Center");
}
}
public ArrayList getList() {
// dummy return value of an empty list,
// just to get the program to compile initially
return itemList;
}
public ArrayList addToList(String s) {
itemList.add(s);
return itemList;
}
public ArrayList getSortedList() {
// dummy return value of an empty list,
// just to get the program to compile initially
Collections.sort(itemList);
return itemList;
}
public ArrayList performReverse() {
// dummy return value of an empty list,
// just to get the program to compile initially
Collections.reverse(itemList);
return itemList;
}
public ArrayList performDeletion(int i) {
// dummy return value of an empty list,
// just to get the program to compile initially
if(i < itemList.size())
itemList.remove(i);
return itemList;
}
public ArrayList performSwap(int i) {
// dummy return value of an empty list,
// just to get the program to compile initially
if(i < itemList.size() && itemList.size() >=2)
{
String s = (String)itemList.remove(i);
if(i < itemList.size())
itemList.add(i + 1, s);
else
itemList.add(itemList.size() - 1, s);
}
return itemList;
}