I'm writing an android application and i have a problem. The app use a doubly concatenated list. An activity create a record and at the click of the button save call a method that pass the created record at the list's add method. It work only first time, then it throw a NullPointerException calling the constructor of List Node. The error occur only in android, on the pc it work well. Here is the code and the thrown exception.
public boolean add(Object data) {
boolean control = false;
try {
if (isEmpty()) {
ListNode newNode = new ListNode(data);
setFirstNode(newNode);
setCurrentNode(newNode);
incrementNumberOfNodes();
control = true;
}
else {
ListNode newNode = new ListNode(data, currentNode.getNextNode(), currentNode);
if (currentNode.getNextNode() != null) {
currentNode.getNextNode().setPreviousNode(newNode);
}
currentNode.setNextNode(newNode);
currentNode = newNode;
incrementNumberOfNodes();
control = true;
}
} catch (NullPointerException e) {
System.err.println(e);
}
return control;
}
public void save(View v){
String action = getIntent().getAction();
if(action.compareTo(GlobalVariables.ACTION_NEW) == 0) {
newRecord();
System.out.println("Save finish with new Record");
}
}
private void newRecord(){
EditText title = (EditText) findViewById(R.id.title);
EditText password = (EditText) findViewById(R.id.password);
int key = GlobalVariables.firstFreePosition();
Record newRecord = new Record(key,title.getText().toString());
newRecord.setPassword(password.getText().toString());
System.out.printf("RECORD CREATO: %s\n", newRecord);
// Aggiungo il record alla lista
boolean control = GlobalVariables.recordsList.add(newRecord);
System.out.println("New Record = " + control);
}
Here is the constructors:
protected ListNode(Object data, ListNode nextNode, ListNode previousNode){
setData(data);
setNextNode(nextNode);
setPreviousNode(previousNode);
}
And here is the exception (list.java's 120th line is the call of list node constructon) :
09-09 21:27:39.711 19605-19605/cf.portaChiavi E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
Caused by: java.lang.NullPointerException
at cf.list.List.add(List.java:120)
at cf.portaChiavi.NewRecord.newRecord(NewRecord.java:161)
at cf.portaChiavi.NewRecord.save(NewRecord.java:90)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
Please help me. Thanks.