The problem is in your print() method: while(t.next!=null) should be
while(t != null)
On another note, the way you’re writing your code will drive anyone mad trying to read it. Try writing it something like this:
import java.util.*;
class Main{
public static void main(String arr[])throws Exception{
Scanner sc=new Scanner(System.in);
int temp = sc.nextInt();
int i,j;
List l = new List();
while(temp != 42){
l.insert(temp);
temp=sc.nextInt();
}
l.print();
}//end main
}//END Main class
class Node{
int n;
Node next;
Node(int num){
n=num;
next=null;
}//end constructor
}//END Node class
class List{
Node first;
Node temp;
int n;
List(){
first=null;
temp=null;
}//end constructor
void insert(int n1){
Node n = new Node(n1);
if(first==null){
first=n;
first.next=null;
temp=first;
}
else{
temp.next=n;
temp=n;
temp.next=null;
}
}//end insert()
void print(){
Node t = first;
while(t != null){
System.out.println(t.n);
t = t.next;
}
}//end print()