数组列表中的空指针异常 - 对象如何工作?
Null Pointer Exception in Array List- How do objects work?
我有空指针异常。你能帮帮我吗?
我在处理数组列表中的对象时遇到问题。不知道哪里出了问题。
主要Class
public class JavaApplication3 {
public static void main(String[] args) {
ArrayList<user> object=new ArrayList<user>();
user[] obj=new user[3];
obj[0].setName("S");
obj[0].setNumber(5);
obj[1].setName("7");
obj[1].setNumber(6);
object.add(obj[0]);
object.add(obj[1]);
System.out.println(object.get(0).getName());
System.out.println(object.get(1).getName());
}
}
这是我的用户 class:
public class 用户 {
private int no;
private String name;
public int getNo() {
return this.no;
}
public void setNumber(int no) {
this.no = no;
}
public String getName(){
return this.name;
}
public void setName(String S){
this.name=name;
}
}
这是我的错误:
Picked up _JAVA_OPTIONS: -Djava.net.preferIPv4Stack=true
Exception in thread "main" java.lang.NullPointerException
at javaapplication3.JavaApplication3.main(JavaApplication3.java:26)
C:\Users\User\AppData\Local\NetBeans\Cache.3\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\User\AppData\Local\NetBeans\Cache.3\executor-snippets\run.xml:68: Java returned: 1
BUILD FAILED (total time: 0 seconds)
只需将其从 S 转换为名称
public void setName(String name){
this.name=name;
}
并更改为此代码
ArrayList<user> object=new ArrayList<user>();
user[] obj=new user[3];
obj[0]=new user();
obj[2]=new user();
obj[0].setName("S");
obj[0].setNumber(5);
obj[1]=new user();
obj[1].setName("7");
obj[1].setNumber(6);
object.add(obj[0]);
object.add(obj[1]);
System.out.println(object.get(0).getName()); System.out.println(object.get(1).getName());
我有空指针异常。你能帮帮我吗? 我在处理数组列表中的对象时遇到问题。不知道哪里出了问题。
主要Class
public class JavaApplication3 {
public static void main(String[] args) { ArrayList<user> object=new ArrayList<user>(); user[] obj=new user[3]; obj[0].setName("S"); obj[0].setNumber(5); obj[1].setName("7"); obj[1].setNumber(6); object.add(obj[0]); object.add(obj[1]); System.out.println(object.get(0).getName()); System.out.println(object.get(1).getName()); }
}
这是我的用户 class:
public class 用户 {
private int no; private String name; public int getNo() { return this.no; } public void setNumber(int no) { this.no = no; } public String getName(){ return this.name; } public void setName(String S){ this.name=name; }
}
这是我的错误:
Picked up _JAVA_OPTIONS: -Djava.net.preferIPv4Stack=true
Exception in thread "main" java.lang.NullPointerException
at javaapplication3.JavaApplication3.main(JavaApplication3.java:26)
C:\Users\User\AppData\Local\NetBeans\Cache.3\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\User\AppData\Local\NetBeans\Cache.3\executor-snippets\run.xml:68: Java returned: 1
BUILD FAILED (total time: 0 seconds)
只需将其从 S 转换为名称
public void setName(String name){
this.name=name;
}
并更改为此代码
ArrayList<user> object=new ArrayList<user>();
user[] obj=new user[3];
obj[0]=new user();
obj[2]=new user();
obj[0].setName("S");
obj[0].setNumber(5);
obj[1]=new user();
obj[1].setName("7");
obj[1].setNumber(6);
object.add(obj[0]);
object.add(obj[1]);
System.out.println(object.get(0).getName()); System.out.println(object.get(1).getName());