KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I tried using try-catch block to catch NullPointerException but still the following program is giving errors. Am I doing something wrong or is there any other way to catch NullPointerException in the following program. Any help is highly appreciated. public class Circular_or_not { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub try { LinkedListNode[] nodes = new LinkedListNode[10]; for (int i = 0; i < 10; i++) { nodes[i] = new LinkedListNode(i, null, i > 0 ? nodes[i - 1] : null); } // Create loop; // nodes[9].next = nodes[3]; Boolean abc= Check_Circular(nodes[0]); System.out.print(abc); } catch(NullPointerException e) { System.out.print("NullPointerException caught"); } } public static boolean Check_Circular(LinkedListNode head) { LinkedListNode n1 = head; LinkedListNode n2 = head; // Find meeting point while (n2.next != null) { n1 = n1.next; n2 = n2.next.next; if (n1 == n2) { return true; } } return false; } }
Tags (comma-separated)
Save Edits
Cancel