为什么我的程序在红色部分会出现nullPointerException?
import java.io.*;
import java.util.*;
public class Main {
class segment implements Comparable<segment>
{
public int start, end;
public int compareTo(segment s)
{
return this.end - s.end;
}
public segment()
{
this.start = 0;
this.end = 0;
}
}
public static void main(String[] args) {
Scanner cin = new Scanner(new BufferedInputStream(System.in));
int n;
n = cin.nextInt();
segment[] seg = new segment[n];boolean[] vis = new boolean[n];
Arrays.fill(vis, false);
int[] ans = new int[n];
int cnt = 0;
for(int i=0; i<n; i++)
{
seg[i].start = cin.nextInt();
seg[i].end = cin.nextInt();}
Arrays.sort(seg);
for(int i=0;i<n;i++)
{
if(!vis[i])
{
ans[cnt++] = seg[i].end;
for(int j=0;j<n;j++)
{
if(seg[i].start < seg[i].end)
vis[j] = true;
else break;
}
}
}
System.out.println(cnt);
for(int i=0;i<cnt;i++)
{
System.out.printf(i == 0? "%d" : " %d", ans[i]);
}
System.out.println();
}
}
调试了一下,发现在蓝色部分实例化后,数组中的每个成员指针都是NULL,请问要怎么做才能正确地使数组中的每个成员都分配一块空间?
[解决办法]