I am getting no such element exception for custom input

import java.util.*;

class GenericTree {
private class Node{
int data;
ArrayList childeren;
Node(int data){
this.data=data;
this.childeren=new ArrayList();
}

}
private Node root;
private int size;
GenericTree(){
 Scanner sc=new Scanner(System.in);
	this.root=takeInput(sc,null,0);
	
}
//to take input  of generic tree
private Node takeInput(Scanner sc, Node parent, int ithchild) {
	if(parent==null) {
		System.out.println("Enter the data for the root node ");
	//	root.data=sc.nextInt();
		
	}
	else {
		System.out.println("Enter the data for the "+ithchild+" th child of"+parent.data);
		
		
	}
	int nodedata=sc.nextInt();
	Node node=new Node(nodedata);
	this.size++;
	System.out.println("Enter the number of childeren  for "+node.data);
	int childn=sc.nextInt();
	//will be executed according to the value of children
	for(int i=0;i<childn;i++) {
		Node child=this.takeInput(sc,node,i);	
		node.childeren.add(child);
	}
	return node;
			
	
	
}

}
public class Main {
public static void main(String args[]) {
Scanner s=new Scanner(System.in);
GenericTree tree=new GenericTree();
int k=s.nextInt();

}

}

Hey @Vipin_coder
Please give custom input
and
private class Node {
int data;
ArrayList <Node > childeren;

	Node(int data) {
		this.data = data;
		this.childeren = new ArrayList<>();
	}

also Static scanner will be , dont use scanner in fun as parameter ya multiple Scanner ,( working fine on Eclipse But NoSuchElementException is found on online judge )
there’s no benefit to creating more than one Scanner object. It’s simply reading input from a stream, and having more than one reference to that stream isn’t necessary or beneficial to your operations.

import java.util.*; class GenericTree { //static Scanner sc=new Scanner(System.in); private class Node{ int data; ArrayList childeren; Node(int data){ this.data=data; this.childeren=new ArrayList(); } } private Node root; private int size; GenericTree(Scanner sc){ // Scanner sc=new Scanner(System.in); this.root=takeInput(sc,null,0); } //to take input of generic tree private Node takeInput(Scanner sc, Node parent, int ithchild) { if(parent==null) { // System.out.println("Enter the data for the root node "); // root.data=sc.nextInt(); } else { //System.out.println(“Enter the data for the “+ithchild+” th child of”+parent.data); } int nodedata=sc.nextInt(); Node node=new Node(nodedata); this.size++; // System.out.println("Enter the number of childeren for "+node.data); int childn=sc.nextInt(); //will be executed according to the value of children for(int i=0;i<childn;i++) { Node child=this.takeInput(sc,node,i); node.childeren.add(child); } return node; } public int sum(int k){ return this.sum(this.root, k); } private int sum(Node node, int k){ if(node==null) return 0; Queue q=new LinkedList(); q.add(node); int lev=0; int ans=0; while(!q.isEmpty()){ int siz=q.size(); for(int i=0;i<siz;i++){ if(lev==k){ Node curr=q.poll(); if(lev==k) ans+=curr.data; ArrayList child=curr.childeren; if(child.get(0)!=null) q.add(child.get(0)); if(child.get(1)!=null) q.add(child.get(1)); } } if(lev==k) break; lev++; } return ans; } } public class Main { public static void main(String args[]) { Scanner sc=new Scanner(System.in); GenericTree tree=new GenericTree(sc); int k=sc.nextInt(); System.out.println(tree.sum(k)); } }

please help me, what is wrong in my code?


Please paste the code and save then share the link