nullpointerexception
jeg har et objekt der af en eller anden grund giver mig en nullpointerexception når jeg prøver at køre en metode på den, jeg havde ikke dette problem før jeg startede med at "hæfte" GUI på. Måske er det bare et tilfælde.Her følger den relevante kode (jeg er ved at lave en kalender implementeret som hægtet liste, en herlig skoleopgave :-D)
package Mayland;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* Mayland.Kontrol
* <P>
* @author IT-afdelingen
*/
public class Kontrol {
ListReferenceBased lrb;
static Kontrol ct;
int i;
/**
* Constructor
*/
public Kontrol() {
Frame frame = new Boundary(ct);
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width)/2, (screenSize.height - frameSize.height)/2);
frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } });
frame.setVisible(true);
ct.createAppointmentBook();
}
public void createAppointmentBook() {
lrb = new ListReferenceBased();
}
public boolean isAppointment(int dato, int tid) {
i = 1;
boolean temp = false;
boolean res = false;
while (temp == false && i <= lrb.size()) {
if (lrb.find(i).getItem().getDato() == dato && lrb.find(i).getItem().getTid() == tid) {
temp = true;
res = true;
}
i++;
}
return res;
}
public boolean insertAppointment(int dato, int tid, String purpose) {
if (ct.isAppointment(dato, tid) == false)
return lrb.add(lrb.size()+1, new Appointment(dato, tid, purpose));
return false;
}
public boolean cancelAppointment(int dato, int tid) {
if (ct.isAppointment(dato, tid) == true){
lrb.remove(--i);
return true;
}
else{
return false;
}
}
public String checkAppointment(int dato, int tid) {
if(isAppointment(dato, tid) == true)
return lrb.find(--i).getItem().getPurpose();
else
return null;
}
public boolean changeAppointment(int oldDate, int oldTime, int newDate, int newTime) {
if(isAppointment(oldDate, oldTime) == false)
return false;
if(isAppointment(newDate, newTime))
return false;
lrb.find(--i).getItem().setDato(newDate);
lrb.find(i).getItem().setTid(newTime);
return true;
}
/**
* main
* @param args
*/
public static void main(String[] args) {
ct = new Kontrol();
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
e.printStackTrace();
}
}
}
package Mayland;
// ****************************************************
// Reference-based implementation of ADT list.
// ****************************************************
public class ListReferenceBased implements ListInterface {
// reference to linked list of items
private Node head;
private int numItems; // number of items in list
public ListReferenceBased() {
numItems = 0;
head = null;
} // end default constructor
public boolean isEmpty() {
return numItems == 0;
} // end isEmpty
public int size() {
return numItems;
} // end size
public Node find(int index) {
// --------------------------------------------------
// Locates a specified node in a linked list.
// Precondition: index is the number of the desired
// node. Assumes that 1 <= index <= numItems+1
// Postcondition: Returns a reference to the desired
// node.
// --------------------------------------------------
Node curr = head;
for (int skip = 1; skip < index; skip++) {
curr = curr.getNext();
} // end for
return curr;
} // end find
public Appointment get(int index)
throws ListIndexOutOfBoundsException {
if (index >= 1 && index <= numItems) {
// get reference to node, then data in node
Node curr = find(index);
Appointment dataItem = curr.getItem();
return dataItem;
}
else {
throw new ListIndexOutOfBoundsException(
"List index out of bounds exception on get");
} // end if
} // end get
public boolean add(int index, Appointment item)
//throws ListIndexOutOfBoundsException
{
if (index >= 1 && index <= numItems+1) {
if (index == 1) {
// insert the new node containing item at
// beginning of list
Node newNode = new Node(item, head);
head = newNode;
}
else {
Node prev = find(index-1);
// insert the new node containing item after
// the node that prev references
Node newNode = new Node(item, prev.getNext());
prev.setNext(newNode);
} // end if
numItems++;
return true;
}
else {
//throw new ListIndexOutOfBoundsException(
// "List index out of bounds exception on add");
return false;
} // end if
} // end add
public void remove(int index)
throws ListIndexOutOfBoundsException {
if (index >= 1 && index <= numItems) {
if (index == 1) {
// delete the first node from the list
head = head.getNext();
}
else {
Node prev = find(index-1);
// delete the node after the node that prev
// references, save reference to node
Node curr = prev.getNext();
prev.setNext(curr.getNext());
} // end if
numItems--;
} // end if
else {
throw new ListIndexOutOfBoundsException(
"List index out of bounds exception on remove");
} // end if
} // end remove
public void removeAll() {
// setting head to null causes list to be
// unreachable and thus marked for garbage
// collection
head = null;
numItems = 0;
} // end removeAll
} // end ListReferenceBased
på forhånd tak for hjælpen