1
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)

Umang
  • 109
  • 2
  • 10

3 Answers3

0

I think that here is your problem:

for(int i=0;i<4;i++)
    {
        if(v[i].getVoterAge()<30)
            temp[c++]=v[i];
    }

When the voter's age is greater than 29, temp[c++] is going to store null. Also, is there any reason why you initialize temp to have only 4 positions?

A possible solution would be in your main:

public static void main(String args[])
{
   Vote[] search = searchVoteByAge(v);
     if(search == null)
        System.out.println("No such voters");
     else
        {
        for(int i=0;i<search.length;i++)
            if(serch[i]==null){
                  //Your code
            }else{
                  System.out.println(search[i].getVoterId()); // line 40 as per my IDE
            }
        }
}
LunaM
  • 13
  • 2
0

The problem in the fixes size array:

Vote temp[] = new Vote[4];

You create an array with 4 nulls. Then you fill this array with values from v[], but if there are not enough values to fill whole temp[], some of its positions remain null, so you end up on line 40 calling null.getVoterId();

Michal Krasny
  • 5,434
  • 7
  • 36
  • 64
0

The Issue is from:

public static Vote[] searchVoteByAge(Vote v[])
    {
        Vote temp[] = new Vote[4]; // this is the issue
        Vote t;
        int c=0;
   .....
   return temp;
}

Arrays have a fixed size and if there is no value for an object that goes in an array, null will be added (for primitives boolean=false, int=0 ....).

Solution

  1. You have to add null check on search[i] or
  2. Create a new array with a reasonable size and return it instead of temp.
Aman
  • 1,627
  • 13
  • 19