Structurally Identical Binary Tree: why giving null pointer exception?

import java.util.*;

public class Main{

public static void main(String [] args)
{
	BinaryTree tree1 = new BinaryTree();
	BinaryTree tree2 = new BinaryTree();

	System.out.println(tree1.StructurelyIdentical(tree2));
}



//Binary Tree

private static class BinaryTree
{

	//Node 
	private class Node{
		int data;
		Node left;
		Node right;

		Node(int data, Node left, Node right)
		{
			this.data = data;
			this.left = left;
			this.right = right;
		}

	}


	private Node root;

	//BinaryTree construction
	BinaryTree()
	{
		Scanner s = new Scanner(System.in);
		this.root = construct(s, null, false);

	}
	private Node construct(Scanner s, Node Parent, Boolean ilc)
	{

		nodedata = s.nextInt();
		
		Node node = new Node(nodedata, null, null);

		boolean choice = s.nextBoolean();

		if(choice)
			node.left = construct(s, node, true);


		choice = s.nextBoolean();

		if(choice)
			node.right = construct(s, node, false);

		return node;
	}



	public boolean StructurelyIdentical(BinaryTree other)
	{
		return this.StructurelyIdentical(this.root, other.root);
	}
	private boolean StructurelyIdentical(Node node1, Node node2)
	{
		if((node1 != null && node2 == null) || (node1 == null && node2 != null))
			return false;
		
		if(node1 == null && node2 == null)
			return true;

		if(node1 != null && node2 != null)
		{
			if(!StructurelyIdentical(node1.left, node2.left))
				return false;
			
			if(!StructurelyIdentical(node1.right, node2.right))
				return false;
		}

		return true;
		
	}

}

}

Hey @arjunsabu99
Make Scanner static
correct code :
import java.util.*;

public class Main{

public static void main(String [] args)
{
BinaryTree tree1 = new BinaryTree();
BinaryTree tree2 = new BinaryTree();

System.out.println(tree1.StructurelyIdentical(tree2));

}

//Binary Tree

private static class BinaryTree
{

//Node 
private class Node{
	int data;
	Node left;
	Node right;

	Node(int data, Node left, Node right)
	{
		this.data = data;
		this.left = left;
		this.right = right;
	}

}


private Node root;

//BinaryTree construction

static Scanner s = new Scanner(System.in);
BinaryTree()
{

	this.root = construct(s, null, false);

}
private Node construct(Scanner s, Node Parent, Boolean ilc)
{

	int nodedata = s.nextInt();
	
	Node node = new Node(nodedata, null, null);

	boolean choice = s.nextBoolean();

	if(choice)
		node.left = construct(s, node, true);


	choice = s.nextBoolean();

	if(choice)
		node.right = construct(s, node, false);

	return node;
}



public boolean StructurelyIdentical(BinaryTree other)
{
	return this.StructurelyIdentical(this.root, other.root);
}
private boolean StructurelyIdentical(Node node1, Node node2)
{
	if((node1 != null && node2 == null) || (node1 == null && node2 != null))
		return false;
	
	if(node1 == null && node2 == null)
		return true;

	if(node1 != null && node2 != null)
	{
		if(!StructurelyIdentical(node1.left, node2.left))
			return false;
		
		if(!StructurelyIdentical(node1.right, node2.right))
			return false;
	}

	return true;
	
}

}
}

Sir why did u make Scanner static?

@arjunsabu99
Give Exception
Exception in thread “main” java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Main$BinaryTree.construct(Main.java:48)
at Main$BinaryTree.(Main.java:42)
at Main.main(Main.java:8)

Sir can u please explain the error, I am not able to get it ? Can we have only one scanner in a class for all its objects?

This exception comes when there are no more integers to be inputted. A check if there are any more integers left in input.

sir i can’t understand, why the previous code had an error?

logic is fine
your code Will check
if there are any more integers left in input.
then you got
Exception in thread “main” java.util.NoSuchElementException

sir static scanner kyu rakha ? pehle bhi to scanner tha wo bhi to input le hi rha tha