Java Hakkında Yardım.
-
Eclipse ile java kodlamaya çalışıyorum. Programı çalıştırdığımda Exception in thread "main" java.lang.NullPointerException hatasını alıyorum. Neden olabileceği hakkında biri yardım edebilir mi?
-
edit yanlış anlamışım hata kodunu.. araştırim bir
-
hocam kodu buraya kopyalasan da öyle yardımcı olsak ?
-
Kod çok uzun valla bakmak çok zamanınızı alabilir. Ben kısaca açıklayayım. Yazmaya çalıştığım program ödevim oluyo aynı zamanda. SortedLinkList programı yazıyorum. Ekleme, silme, aratma falan var. Bi test dosyası verilmiş benden Sortedlinklist.java içerisine gerekli kodları yazmam isteniyo. Diğer java dosyalarını hoca yazmış hata olcanı sanmam :) Sortedlinklist içeriği de :
/********************************************************************* SortedLinkedList ADT: Stores integers in a doubly linked list in
* ascending order of key values
* Supported Operations:
*
*
********************************************************************/
public class SortedLinkedList {
public LinkedListNode head, tail; /* Head and tail of the list of nodes */
public int noOfNodes; /* Current number of nodes in the list */
/*******************************************************
* Constructor: Initializes the linked list
*******************************************************/
public SortedLinkedList(){
Clear();
} //end-SortedLinkedList
/*******************************************************
* Removes all of the nodes from this list.
*******************************************************/
public void Clear(){
head = tail = null;
noOfNodes = 0;
} // end-Clear
/*******************************************************
* Returns the number of nodes in the list
*******************************************************/
public int NoOfNodes(){return noOfNodes;}
/*******************************************************
* Inserts the given key in ascending order in the list
*******************************************************/
public void Add(int key){
LinkedListNode newNode = LinkedListNode();
newNode.key = key;
if (head == null){
head = newNode;
tail = newNode;
}
else{
LinkedListNode curr = head;
while (curr != null && curr.key < newNode.key){
curr =curr.next;
}//end while
if (curr == null){
//Add end.
newNode.prev = tail;
tail.next = newNode;
tail = newNode;
noOfNodes++;
}
else if (curr == head){
//Add top
newNode.next = curr;
curr.prev = newNode;
head = newNode;
noOfNodes++;
}
else{
//Add between
newNode.prev = curr.prev;
newNode.next = curr;
curr.prev.next = newNode;
curr.prev = newNode;
noOfNodes++;
}//end else if
}//end if
noOfNodes++;
} //end-Add
private LinkedListNode LinkedListNode() {
// TODO Auto-generated method stub
return null;
}
/*******************************************************
* Removes the node that contains the key from the list (if the key is found)
*******************************************************/
public void Remove(int key){
LinkedListNode removeNode = new LinkedListNode();
removeNode.key = key;
LinkedListNode curr = head;
while (curr != null && curr.key != removeNode.key){
curr = curr.next;
}
if (curr == null){
System.out.println("Key is not found !");
}
else if (curr==head){
head = curr.next;
head.prev = null;
noOfNodes--;
}
else if (curr==tail){
tail = curr.prev;
tail.next = null;
noOfNodes--;
}
else{
curr.next.prev = curr.prev;
curr.prev.next = curr.next;
noOfNodes--;
}
} //end-Remove
/*******************************************************
* Removes a node from the list given a pointer to the node
*******************************************************/
public void Remove(LinkedListNode node){
Remove(noOfNodes);
} //end-Remove
/*******************************************************
* Searches a key in the list and returns a pointer
* to the list node that contains the key
*******************************************************/
public LinkedListNode Find(int key){
int i=0;
LinkedListNode removeNode = new LinkedListNode();
removeNode.key = key;
LinkedListNode curr = head;
while (curr != null && curr.key != removeNode.key){
curr = curr.next;
i++;
}
if (curr == null){
System.out.println("Key is not found !");
}
else{
System.out.println("The key is on "+i+". slot.");
}
return null;
} //end-Find
};
-
Hatayı aldığın ekranda hatanın stack trace i çıkar. Buradaki ifadeye göre hata SortedLinkedList dosyasında 67. satırda peki o satırda ne var.
newNode.key = key;
bu ifadede görünürde bir hata yok ama aldığın hata NullPointerException yani büyük ihtimalle tanımlanmamış bir nesneye erişmeye çalışıyorsun, hemen geriye gidip newNode ve key değişkenlerinin tanımlanıp tanımlanmadıklarına bakıyorsun.
key zaten fonksiyon parametresi olarak geliyor onda sorun yok. Sorun newNode da
LinkedListNode newNode = LinkedListNode();
ifadesinde constructor dan önce new ifadesi unutulmuş.
LinkedListNode newNode = new LinkedListNode();
Başlıkları daha açıklayıcı açarsan mesela "Java NullPointerException hatası" gibi daha güzel olur diye düşünüyorum.
Kolay gelsin...
-
Teşekkür ederim hocam sen olmasan bu ödevi çok zor yapardım hatta yapamayabilirdim :)