java.lang.NullPointerException即使得到正确的结果


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;
}
}

在此代码中,我试图返回年龄小于30岁的选民的数组对象(按升序排列)。由于我对这个概念不熟悉,所以我没有使用List和Collection。我选择了简单的冒泡排序。

显示的结果完全正确。我还得到了额外的一行Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Vote.getVoterId()" because "search[i]" is null at Solution5.main(Solution5.java:40)

我认为这是你的问题:

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

当选民的年龄大于29岁时,temp[c++]将存储null。此外,是否有任何原因,你初始化temp只有4个位置?

一个可能的解决方案是在你的主:

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
}
}
}

问题来自:

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

数组有固定的大小,如果数组中没有对象的值,则会添加null(对于原语boolean=false, int=0....)。

<<p>解决方案/strong>
  1. 您必须在search[i]
  2. 上添加null检查
  3. 创建一个合理大小的新数组,并返回它而不是temp

修复size数组中的问题:

Vote temp[] = new Vote[4];

创建一个包含4个null的数组。然后用v[]的值填充这个数组,但是如果没有足够的值填充整个temp[],它的一些位置仍然是null,所以你最终在第40行调用null.getVoterId();

相关内容

最新更新