当前位置: 代码迷 >> Eclipse >> 小程序,出现“NullPointerException”,求解答解决方案
  详细解决方案

小程序,出现“NullPointerException”,求解答解决方案

热度:112   发布时间:2016-04-23 13:53:01.0
小程序,出现“NullPointerException”,求解答
import java.util.Scanner;

class PointNode{  
  public int x,y;  
  public int tag;  


public class test3 {
public static void main(String args[]){
Scanner scanner=new Scanner(System.in);
PointNode[] point=new PointNode[3];
for (int k = 0; k < 3; k++) { 
point[k].x=scanner.nextInt();
point[k].y=scanner.nextInt(); 
}
for (int i = 0; i < 3; i++) { 
System.out.println(point[i].x);
System.out.println(point[i].y);
}
  }
}


------解决方案--------------------
point[k] = new Point(); 没有初始化
------解决方案--------------------
Java code
import java.util.Scanner;class PointNode{     public int x,y;     public int tag;   }  public class Test14 {    public static void main(String args[]){        Scanner scanner=new Scanner(System.in);        PointNode[] point=new PointNode[3];        for (int k = 0; k < 3; k++) {                          //要对数组每个元素实例化            point[k] = new PointNode();            point[k].x=scanner.nextInt();            point[k].y=scanner.nextInt();          }        for (int i = 0; i < 3; i++) {              System.out.println(point[i].x);            System.out.println(point[i].y);        }    }}
  相关解决方案