数组列表中出现空指针异常-对象如何工作?

06odsfpq  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(289)

这个问题在这里已经有答案了

什么是nullpointerexception,如何修复它(12个答案)
四天前关门了。
我有空指针异常。你能帮助我吗?数组列表中的对象有问题。我不知道问题出在哪里。
主要类别
公共类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());

 }

}
这是我的用户类:
公共类用户{

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\12.3\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\User\AppData\Local\NetBeans\Cache\12.3\executor-snippets\run.xml:68: Java returned: 1
BUILD FAILED (total time: 0 seconds)
mefy6pfw

mefy6pfw1#

把这个从s转换成name

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());

相关问题