Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“线程中的异常”;“主要”;java数组上的java.lang.NullPointerException_Java_Arrays_Nullpointerexception - Fatal编程技术网

“线程中的异常”;“主要”;java数组上的java.lang.NullPointerException

“线程中的异常”;“主要”;java数组上的java.lang.NullPointerException,java,arrays,nullpointerexception,Java,Arrays,Nullpointerexception,我从我的代码中得到此错误: 线程“main”java.lang.NullPointerException中出现异常 MainClass.main(MainClass.java:20) 有人能识别出这个错误吗?我想这与初始化我的数组有关 MainClass.java public class MainClass { public static void main(String[] args) { //dummy vars to simulate user input doub

我从我的代码中得到此错误:

线程“main”java.lang.NullPointerException中出现异常 MainClass.main(MainClass.java:20)

有人能识别出这个错误吗?我想这与初始化我的数组有关

MainClass.java

public class MainClass {


public static void main(String[] args) {

    //dummy vars to simulate user input
    double price = 2.75;

    //declare an array of wincalcs 
    WinCalc[] staging1;
    staging1 = new WinCalc[100];


    for (int x=0; x<staging1.length; x++ ) {
        staging1[x].price = price;
        staging1[x].quantity = x+1;
        staging1[x].calcTotal();    
    }



}
public class WinCalc {

public double price;
public double quantity;
public double total;

public WinCalc () {
    price= 0;
    quantity = 0;
    total = 0;
}

public void calcTotal() {
    this.total = price * quantity;
}

}您忘记创建对象了

for (int x=0; x<staging1.length; x++ ) {
    staging1[x] = new WinCalc();   
    // ...
}

for(int x=0;x您忘记创建对象了

for (int x=0; x<staging1.length; x++ ) {
    staging1[x] = new WinCalc();   
    // ...
}

for(int x=0;x用以下命令更新代码:

public class MainClass {

public static void main(String[] args) {

    //dummy vars to simulate user input
    double price = 2.75;

    //declare an array of wincalcs 
    WinCalc[] staging1;
    staging1 = new WinCalc[100];


    for (int x=0; x<staging1.length; x++ ) {
        staging1[x] = new WinCalc(); 
        staging1[x].price = price;
        staging1[x].quantity = x+1;
        staging1[x].calcTotal();    
    }
}
public类MainClass{
公共静态void main(字符串[]args){
//模拟用户输入的虚拟变量
双倍价格=2.75;
//声明一个WinCalc数组
WinCalc[]阶段1;
阶段1=新WinCalc[100];

对于(int x=0;x使用以下命令更新代码:

public class MainClass {

public static void main(String[] args) {

    //dummy vars to simulate user input
    double price = 2.75;

    //declare an array of wincalcs 
    WinCalc[] staging1;
    staging1 = new WinCalc[100];


    for (int x=0; x<staging1.length; x++ ) {
        staging1[x] = new WinCalc(); 
        staging1[x].price = price;
        staging1[x].quantity = x+1;
        staging1[x].calcTotal();    
    }
}
public类MainClass{
公共静态void main(字符串[]args){
//模拟用户输入的虚拟变量
双倍价格=2.75;
//声明一个WinCalc数组
WinCalc[]阶段1;
阶段1=新WinCalc[100];

对于(int x=0;x当您分配数组时,它最初由空条目填充。为了使它包含实际对象,您必须手动填充新分配的对象:

WinCalc[] staging1;
staging1 = new WinCalc[100];

for(int n = 0; n < 100; n ++)
{
    stanging1[n] = new WinClac();
}
WinCalc[]阶段1;
阶段1=新WinCalc[100];
对于(int n=0;n<100;n++)
{
stanging1[n]=新WinClac();
}

这是因为java中的所有对象都是默认情况下不指向任何地方的引用。

当您分配数组时,它最初会填充空项。为了使它包含实际对象,您必须手动填充新分配的对象:

WinCalc[] staging1;
staging1 = new WinCalc[100];

for(int n = 0; n < 100; n ++)
{
    stanging1[n] = new WinClac();
}
Cases that we get NullPointerException are accessing/modifying the field of null object or accessing/modifying the slot of null as if it were an array or taking the length of null as if it were an array.
//Let us have a Person class 
public class Person {
          private String name;
          private int age;
    public Person(String name, int age) {
    this.name = name;
    this.age = age;
          }
    public String getName(){
      return name;
     }
   public int getAge(){
      return age;
    }
  public String toString(){
      return "[Name->"+ getName() +" ,Age->"+getAge()+"]";
     }
}
//The main class simulate collection of persons using array
import java.util.Arrays;
public class ListOfPersonIn {
public static void arrayManipulation() 
{
    Person[] persons=new Person[3]; // Array of Person to conatain 3 persons
              Person titi=new Person("Titi", 35);
    Person beti=new Person("Beti", 10);
    Person nati=new Person("nati", 18);
     // Display list of persons
     for(Person person:persons){
        System.out.println(person.toString());
      }
   //Double array size, copy the old value to the new array and add new persons 
    Person[]newPersons=copyArraySize(persons);
    System.out.println("Loop through a new Array ");
    for(Person person: newPersons){
        System.out.println(person.toString());
     }
    }
   // Private method to resize array, copy the old array to the new array and add new list of persons
private static Person [] copyArraySize(Person [] persons)
{
               Person[]newPersons=Arrays.copyOf(persons,  persons.length*2);
// newPersons[persons.length]=new Person("meti", 50); in this case we get NullPointerException   because the new array has length 6 but only 4 data is populated the reaming 2 indices are not populated i.e newArray[4] and newArray[5] are null value so it raised NullPointerException. Not to get NullPointerException  just populate all array indices with data 
     for(int i=persons.length;i< newPersons.length;i++){
          newPersons[i]=new Person("meti", 50);//duplicate data, array can’t maintain uniqueness like set 
        }
        return newPersons;
  }
  public static void main(String[] args) {
            arrayManipulation();
     }
}
WinCalc[]阶段1;
阶段1=新WinCalc[100];
对于(int n=0;n<100;n++)
{
stanging1[n]=新WinClac();
}
这是因为java中的所有对象都是默认情况下不指向任何地方的引用。

我们得到NullPointerException的情况是访问/修改null对象的字段,或者访问/修改null对象的插槽,就像它是数组一样,或者将null的长度当作数组一样。
Cases that we get NullPointerException are accessing/modifying the field of null object or accessing/modifying the slot of null as if it were an array or taking the length of null as if it were an array.
//Let us have a Person class 
public class Person {
          private String name;
          private int age;
    public Person(String name, int age) {
    this.name = name;
    this.age = age;
          }
    public String getName(){
      return name;
     }
   public int getAge(){
      return age;
    }
  public String toString(){
      return "[Name->"+ getName() +" ,Age->"+getAge()+"]";
     }
}
//The main class simulate collection of persons using array
import java.util.Arrays;
public class ListOfPersonIn {
public static void arrayManipulation() 
{
    Person[] persons=new Person[3]; // Array of Person to conatain 3 persons
              Person titi=new Person("Titi", 35);
    Person beti=new Person("Beti", 10);
    Person nati=new Person("nati", 18);
     // Display list of persons
     for(Person person:persons){
        System.out.println(person.toString());
      }
   //Double array size, copy the old value to the new array and add new persons 
    Person[]newPersons=copyArraySize(persons);
    System.out.println("Loop through a new Array ");
    for(Person person: newPersons){
        System.out.println(person.toString());
     }
    }
   // Private method to resize array, copy the old array to the new array and add new list of persons
private static Person [] copyArraySize(Person [] persons)
{
               Person[]newPersons=Arrays.copyOf(persons,  persons.length*2);
// newPersons[persons.length]=new Person("meti", 50); in this case we get NullPointerException   because the new array has length 6 but only 4 data is populated the reaming 2 indices are not populated i.e newArray[4] and newArray[5] are null value so it raised NullPointerException. Not to get NullPointerException  just populate all array indices with data 
     for(int i=persons.length;i< newPersons.length;i++){
          newPersons[i]=new Person("meti", 50);//duplicate data, array can’t maintain uniqueness like set 
        }
        return newPersons;
  }
  public static void main(String[] args) {
            arrayManipulation();
     }
}
//让我们来上个人课 公共阶层人士{ 私有字符串名称; 私人互联网; 公众人物(字符串名称,整数年龄){ this.name=名称; 这个。年龄=年龄; } 公共字符串getName(){ 返回名称; } 公共整数getAge(){ 回归年龄; } 公共字符串toString(){ 返回“[Name->”+getName()+”,Age->“+getAge()+”]; } } //主类使用数组模拟人员的集合 导入java.util.array; personin的公共类列表{ 公共静态无效数组操作() { Person[]persons=新的Person[3];//要输入3个人的Person数组 人titi=新人(“titi”,35); 人员beti=新人员(“beti”,10); 人员nati=新人员(“nati”,18); //显示人员名单 用于(人:人){ System.out.println(person.toString()); } //将数组大小加倍,将旧值复制到新数组并添加新人员 Person[]newPersons=copyraraysize(人); System.out.println(“通过新数组循环”); 适用于(个人:新人){ System.out.println(person.toString()); } } //Private方法调整数组大小,将旧数组复制到新数组并添加新的人员列表 私人静态人员[]copyraraySize(人员[]人) { Person[]newPersons=数组.copyOf(persons,persons.length*2); //newPersons[persons.length]=new Person(“meti”,50);在这种情况下,我们得到NullPointerException,因为新数组的长度为6,但只填充了4个数据。铰孔2索引未填充,即newArray[4]和newArray[5]是空值,因此它引发了NullPointerException。不获取NullPointerException,只需使用数据填充所有数组索引 for(inti=persons.length;i
我们得到NullPointerException的情况是访问/修改null对象的字段,或者像访问数组一样访问/修改null的插槽,或者像访问数组一样使用null的长度。
//让我们来上个人课
公共阶层人士{
私有字符串名称;
私人互联网;
公众人物(字符串名称,整数年龄){
this.name=名称;
这个。年龄=年龄;
}
公共字符串getName(){
返回名称;
}
公共整数getAge(){
回归年龄;
}
公共字符串toString(){
返回“[Name->”+getName()+”,Age->“+getAge()+”];
}
}
//主类使用数组模拟人员的集合
导入java.util.array;
personin的公共类列表{
公共静态无效数组操作()
{
Person[]persons=新的Person[3];//要输入3个人的Person数组
人titi=新人(“titi”,35);
人员beti=新人员(“beti”,10);
人员nati=新人员(“nati”,18);
//显示人员名单
用于(人:人){
System.out.println(person.toString());
}
//将数组大小加倍,将旧值复制到新数组并添加新人员
Person[]newPersons=copyraraysize(人);
System.out.println(“通过新数组循环”);
适用于(个人:新人){
System.out.println(person.toString());
}
}
//Private方法调整数组大小,将旧数组复制到新数组并添加新的人员列表
私人静态人员[]copyraraySize(人员[]人)
{
Person[]newPersons=数组.copyOf(persons,persons.length*2);
//newPersons[persons.length]=new Person(“meti”,50);在这种情况下,我们得到NullPointerException,因为新数组的长度为6,但只填充了4个数据。铰孔2索引未填充,即newArray[4]和newArray[5]