I'm having a small issue with this program. It's an appointment book, and the idea is to be able to add contacts and events.
Here is the first part of my code and the one I'm having trouble with:
String aux = " ";
String aux2 = " ";
long aux3 = 0;
Arraylist <Person> cole1 = new Arraylist();
do{
System.out.println("Welcome");
System.out.println("- Book");
System.out.println("1. Add contact");
System.out.println("2. See contacts");
System.out.println("3. Remove contacts");
switch(Integer.parseInt(teclado.readLine())){
case 1:
for(int i = 0; i<cole1.size();i++){
System.out.println("Write the name");
aux = teclado.readLine();
System.out.println("Write the email");
aux2 = teclado.readLine();
System.out.println("Write the phone number");
aux3 = Integer.parseInt(teclado.readLine());
cole1.add(new Person(aux, aux2, aux3));
}
break;
default:
System.out.println("error");
break;
}
}while(true);
Here's my person class:
public class Person {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public float getNum() {
return num;
}
public void setNum(long num) {
this.num = num;
}
private String name;
private String email;
private long num;
public Persona(String n, String e, long nu){
this.name=n;
this.email=e;
this.num=nu;
}
}
This part should be able to create a new person with the name, phone and email attributes. The problem is when I press 1 in my switch for the add option i get the following error:
Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet.
at agenda.Arraylist.size(Arraylist.java:15)
at agenda.AgendaTest.main(AgendaTest.java:40)
C:\Users\melis\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 2 seconds)
I can't figure out if its a logic or syntax problem. I've read a few other questions with people having a similar problem. The difference is, all of them where apparently using the Arrays.asList property, which if i understand correctly, Arrays.asList doesn't support add/remove operations.
Any idea on what is going wrong?
Thanks in advance! And sorry if my English is bad.
The problem is that you have created your own Arraylist
class (full name agenda.Arraylist
), and your class does not implement the add
operation. This is clear from the stacktrace.
I am guessing that your implementation is something like this:
package agenda;
import java.util.AbstractList;
public class Arraylist<T> extends AbstractList<T> {
...
// No override for `add(<T>)`, `add(int, <T>)`, etcetera.
}
If you don't implement an add
method, the default implementation throws that exception.
Solutions:
agenda.Arraylist
class.Arraylist
. Use the standard java.util.ArrayList
instead. (Unless you have a very good reason, you shouldn't implement your own versions of standard classes.)UPDATE
(I didn't notice the subtle ArrayList
vs Arraylist
difference. My eyesight isn't as good as it used to be. Sorry. I've updated the above ...)
Your stacktrace says this:
Exception in thread "main" java.lang.UnsupportedOperationException: Not supported yet.
at agenda.Arraylist.size(Arraylist.java:15)
at agenda.AgendaTest.main(AgendaTest.java:40)
C:\Users\melis\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 2 seconds)
The second line of that says that the exception is being thrown in a class whose fully qualified name is agenda.Arraylist
.
Note:
java.util.ArrayList
.Arraylist
is ... different.Arraylist
class, andsize()
method.Even if you do import the real ArrayList
class, it will make no difference. You are not using it because you are using the wrong simple name for it.
We have no way of knowing for sure how you got to your current state, but the stacktrace does not lie, and neither does your source code.
(If I was to guess, it would be that at some point you used your IDE's "suggest a correction" functionality to get a fix for an undefined symbol compilation error for your miss-spelled Arraylist
identifier. But you picked the wrong correction ... and your IDE helpfully generated a skeletal implementation of the Arraylist
class rather than spell-correcting the name to ArrayList
and adding the necessary import. But that's just a guess ...)