import java.util.*;
public class Solution5 {
    public static void main(String args[])
    {
        int voterId;
        String voterName;
        int voterAge;
        boolean isVoteCasted;
        String constituency;
        
        Scanner sc = new Scanner(System.in);
        
        Vote v[] = new Vote[4];
        
        for(int i=0;i<4;i++)
        {
            voterId = sc.nextInt(); sc.nextLine();
            voterName = sc.nextLine();
            voterAge = sc.nextInt();
            isVoteCasted = sc.nextBoolean(); sc.nextLine();
            constituency = sc.nextLine();
            v[i] = new Vote(voterId, voterName, voterAge, isVoteCasted, constituency);
        }
        String con = sc.nextLine();
        sc.close();
        
        int total = findTotalVotesCastedByConstituency(v, con);
        if(total == 0)
            System.out.println("No votes casted");
        else
            System.out.println(total);
        
        Vote[] search = searchVoteByAge(v);
        if(search == null)
            System.out.println("No such voters");
        else
            {
            for(int i=0;i<search.length;i++)
                System.out.println(search[i].getVoterId());
            }
    }
    
    public static int findTotalVotesCastedByConstituency(Vote v[], String con)
    {
        int c=0;
        for(int i=0;i<4;i++)
        {
            if(con.equalsIgnoreCase(v[i].getConstituency()))
            {
                if(v[i].getIsVoteCasted())
                    c++;
            }
        }
        return c;
    }
    
    public static Vote[] searchVoteByAge(Vote v[])
    {
        Vote temp[] = new Vote[4];
        Vote t;
        int c=0;
        for(int i=0;i<4;i++)
        {
            if(v[i].getVoterAge()<30)
                temp[c++]=v[i];
        }
        //for(int i=0;i<c;i++)
        //  System.out.println(temp[i].getVoterAge());
        if(c==0)
            return null;
        for(int i=0;i<c;i++)
        {
            for(int j=0;j<c-i-1;j++)
            {
                if(temp[j].getVoterAge() > temp[j+1].getVoterAge())
                {
                    t = temp[j];
                    temp[j] = temp[j+1];
                    temp[j+1] = t;
                }
            }
        }
        return temp;
    }
}
class Vote
{
    private int voterId;
    private String voterName;
    private int voterAge;
    private boolean isVoteCasted;
    private String constituency;
    
    public Vote(int voterId, String voterName, int voterAge, boolean isVoteCasted, String constituency)
    {
        this.voterId = voterId;
        this.voterName = voterName;
        this.voterAge = voterAge;
        this.isVoteCasted = isVoteCasted;
        this.constituency = constituency;
    }
    
    public int getVoterId()
    {
        return voterId;
    }
    public String getVoterName()
    {
        return voterName;
    }
    public int getVoterAge()
    {
        return voterAge;
    }
    public boolean getIsVoteCasted()
    {
        return isVoteCasted;
    }
    public String getConstituency()
    {
        return constituency;
    }
}
In this code, I am trying to return array objects (in ascending order) of those voters whose age is less than 30. Since I am new to this concept, so I did not use List and Collection. Rather opted for simple bubble sort.
The results displayed are perfectly correct. Still I get an additional line which says: Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Vote.getVoterId()" because "search[i]" is null at Solution5.main(Solution5.java:40)
 
     
     
    