can you help me to clear the error?

import java.util.*;
 class Node{
    protected Object data;
	protected static ArrayList child;
    protected Node next;

    public Node(){
        next = null;
        data = null;
		child=new ArrayList();
    }
    public Node(Object d,Node n){
        data = d;
        next = n;
		n.child=new ArrayList();
    }
    
}

public class Tree{
    protected Node head;

    public Tree(){
        head = null;
    }
    public boolean isEmpty(){
        return head == null;
    }
    public void insert(Object obj){
		if(isEmpty()){
			Node.child.add(obj);
			head = new Node(obj,head);
		}
    }
	public static void main(String args[])
	{
		Tree t=new Tree();
		Integer j = null;
        int i;
        System.out.println("starting...");
        for(i=0;i<5;i++){
            j = new Integer((int)(Math.random() * 100));
            t.insert(j);
            System.out.println("insert: " + j);
        }
        System.out.println("Done ;-)");

	}
}

Edited 4 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.

help me to clear the error?

You forgot to post the error.
Please copy and paste here the full text of the error message.

Edited 4 Years Ago by NormR1: n/a

Please edit your post and wrap the code in code tags. Use the [code] icon above the input box.

Edited 4 Years Ago by NormR1: n/a

Pl

import java.util.*;
 class Node{
    protected Object data;
	protected static ArrayList child;
    protected Node next;

    public Node(){
        next = null;
        data = null;
		child=new ArrayList();
    }
    public Node(Object d,Node n){
        data = d;
        next = n;
		n.child=new ArrayList();
    }
    
}

public class Tree{
    protected Node head;

    public Tree(){
        head = null;
    }
    public boolean isEmpty(){
        return head == null;
    }
    public void insert(Object obj){
		if(isEmpty()){
			Node.child.add(obj);
			head = new Node(obj,head);
		}
    }
	public static void main(String args[])
	{
		Tree t=new Tree();
		Integer j = null;
        int i;
        System.out.println("starting...");
        for(i=0;i<5;i++){
            j = new Integer((int)(Math.random() * 100));
            t.insert(j);
            System.out.println("insert: " + j);
        }
        System.out.println("Done ;-)");

	}
}

ease edit your post and wrap the code in code tags. Use the

icon above the input box.[/QUOTE]

the error message is: C:\Users\Sugirthan\Desktop\Tree>javac Tree.java Note: Tree.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.

C:\Users\Sugirthan\Desktop\Tree>java Tree starting... Exception in thread "main" java.lang.NullPointerException at Tree.insert(Tree.java:32) at Tree.main(Tree.java:44)

C:\Users\Sugirthan\Desktop\Tree>[code]
icon above the input box.

the error message is:
C:\Users\Sugirthan\Desktop\Tree>javac Tree.java
Note: Tree.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

C:\Users\Sugirthan\Desktop\Tree>java Tree
starting...
Exception in thread "main" java.lang.NullPointerException
at Tree.insert(Tree.java:32)
at Tree.main(Tree.java:44)

C:\Users\Sugirthan\Desktop\Tree>

Exception in thread "main" java.lang.NullPointerException
at Tree.insert(Tree.java:32)

There is a variable on line 32 with a null value. Look at line 32 and find the variable with the null value then backtrack in the code to see why that variable does not have a valid, non-null value.

Please edit your post and wrap the code in code tags. Use the [code] icon above the input box.

There is a variable on line 32 with a null value. Look at line 32 and find the variable with the null value then backtrack in the code to see why that variable does not have a valid, non-null value.

Please edit your post and wrap the code in code tags. Use the [code] icon above the input box.

here one line is missing. so you have to consider one line above

yes but this code also have same error.

import java.util.*;
 class Node{
    protected int data;
	protected static ArrayList child;
    protected Node next;

    public Node(){
        next = null;
		child=new ArrayList();
    }
    public Node(int d,Node n){
        data = d;
        next = n;
		n.child=new ArrayList();
    }
    
}

public class Tree{
    protected Node head;

    public Tree(){
        head = null;
    }
    public boolean isEmpty(){
        return head == null;
    }
    public void insert(int obj){
		if(isEmpty()){
			Node.child.add(new Integer(1));
			//head = new Node(obj,head);
		}
    }
	public static void main(String args[])
	{
		Tree t=new Tree();
		Integer j = null;
        int i;
        System.out.println("starting...");
        for(i=0;i<5;i++){
            j = new Integer((int)(Math.random() * 100));
            t.insert(i);
			//Node.child.add(new Integer(i));
            System.out.println("insert: " + j);
        }
        System.out.println("Done ;-)");
		System.out.println("size "+Node.child.size());
	}
}

Did you find the variable with the null value?

error message:
C:\Users\Sugirthan\Desktop\Tree>javac Tree.java
Note: Tree.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

C:\Users\Sugirthan\Desktop\Tree>java Tree
starting...
Exception in thread "main" java.lang.NullPointerException
at Tree.insert(Tree.java:30)
at Tree.main(Tree.java:42)

Did you look at line 20 in Tree.java to see what variable is null? You must do this if you are going to find the problem and fix it.
When you find the variable, you need to backtrack in the code to see why that variable does not have a valid value.

This article has been dead for over six months. Start a new discussion instead.