Java 为什么nullpointerexception不断出现?我最后写下了错误。我还提到了发生错误的那一行

Java 为什么nullpointerexception不断出现?我最后写下了错误。我还提到了发生错误的那一行,java,exception,error-handling,nullpointerexception,getter,Java,Exception,Error Handling,Nullpointerexception,Getter,调用getter函数时发生空指针异常。请尽快帮忙。我最后写下了错误。我还提到了发生错误的那一行。但部分输出是正确的 import java.util.Scanner; public class Solution { public static void main(String args[] ) throws Exception { /* Do not alter code in main method */ Shirt[] shirts =

调用getter函数时发生空指针异常。请尽快帮忙。我最后写下了错误。我还提到了发生错误的那一行。但部分输出是正确的

import java.util.Scanner;
public class Solution 
{
    public static void main(String args[] ) throws Exception 
    {
        /* Do not alter code in main method */
         Shirt[] shirts = new Shirt[5];

         Scanner sc = new Scanner(System.in);

         for(int i = 0;i<5;i++)
         {
            int tag = sc.nextInt();sc.nextLine();
            String brand = sc.nextLine();
            double price = sc.nextDouble();sc.nextLine();
            char g = sc.nextLine().charAt(0);
            shirts[i] = new Shirt(tag,brand,price,g);
         }
         double price = sc.nextDouble();
         for(Shirt s: shirts)
         {
              System.out.println(getDiscountPrice(s));            
         }
         Shirt[] result = getShirtWithMoreThanSpecificPrice(shirts,price);
         for(Shirt s: result)
         {
             System.out.println(s.getTag()+" "+s.getPrice()+" "+s.getBrand());//This is where the exception occurs
         }
    }
    public static double getDiscountPrice(Shirt s)
    {
        double p;
        if(s.g=='m')
        {
            p=s.price-((0.1*s.price));
            return p;
        }
        if(s.g=='f')
        {
            p=s.price-((0.2*s.price));
            return p;
        }
        if(s.g=='u')
        {
            p=s.price-((0.3*s.price));
            return p;
        }
            return 0.0;
    }
    public static Shirt[] getShirtWithMoreThanSpecificPrice(Shirt[] shirts,double price)
   {
        Shirt[] sh=new Shirt[5];
        for(int i=0;i<5;i++)
        {
            if(shirts[i].price>price)
            {
                sh[i]=shirts[i];
            }
        }
        return sh;
  }
  }
 class Shirt
 {
    //define the class as per details shared in the question
    int tag;
    String brand; 
    double price;
    char g;
    public Shirt(int tag,String brand,double price,char g)
    {
    this.brand=brand;
    this.g=g;
    this.price=price;
    this.tag=tag;
    }
    public void setG(char g) 
    {
    this.g = g;
    }
    public void setTag(int tag) 
    {
    this.tag = tag;
    }
    public void setBrand(String brand) 
    {
    this.brand = brand;
    }

    public void setPrice(double price) 
    {
    this.price = price;
    }
    public int getTag() 
    {
    return tag;
    }
    public double getPrice() 
    {
    return price;
    }
    public String getBrand() 
    {
    return brand;
    }
     public char getG() 
    {
    return g;
    }
}
错误:

线程主java.lang.NullPointerException中的异常位于 Solution.mainSolution.java:32


错误发生在第32行。我试图初始化变量。没有一次成功

当使用Shirt[]sh=new Shirt[5]在GetShirtWithmore-Specific Price中分配一个新数组时,它将用五个空值初始化

然后分配元素,但仅限于条件:if shirts[i].price>price sh[i]=shirts[i];。因此,某些值可能仍然为空。在main中循环此数组时,无论s是否为null,都必须签入Shirt s:result。这是可能发生的,这就是这里发生的事情


现在,最好返回一个仅包含非空值的数组,即那些衬衫[i].price>price的衬衫。如果您设法在数组sh的开头存储非空值,而不带孔,则可以使用列表来代替仅附加非空值或Arrays.copyOf。

当您在GetShirtThmorethanSpecificPrice中使用Shirt[]sh=new Shirt[5]分配新数组时,它将用五个空值初始化

然后分配元素,但仅限于条件:if shirts[i].price>price sh[i]=shirts[i];。因此,某些值可能仍然为空。在main中循环此数组时,无论s是否为null,都必须签入Shirt s:result。这是可能发生的,这就是这里发生的事情


现在,最好返回一个仅包含非空值的数组,即那些衬衫[i].price>price的衬衫。如果您设法在数组sh的开头存储非空值,而不带孔,则可以使用列表来代替仅附加非空值或Arrays.copyOf。

尝试用以下代码替换for循环代码:

for (Shirt s : result) {
    if(s != null) {
        System.out.println(s.getTag() + " " + s.getPrice() + " " + s.getBrand());// This is where the exception
    }
                                                                                        // occurs
}

由于您在初始化数组时未指定值,因此该数组的默认值为null。

尝试用以下代码替换for循环代码:

for (Shirt s : result) {
    if(s != null) {
        System.out.println(s.getTag() + " " + s.getPrice() + " " + s.getBrand());// This is where the exception
    }
                                                                                        // occurs
}

由于您在初始化数组时未指定值,因此它的默认值为null。

请问代码的哪一行是第32行?在这里发布的代码中计算行数最多是不可靠的,所以您最好为我们指出这一点。编辑您的问题,并在第32行后添加适当的注释,或在错误后再次引用该行。请问代码的哪一行是第32行?在这里发布的代码中计算行数最多是不可靠的,所以您最好为我们指出这一点。编辑您的问题,并在第32行后添加适当的注释,或在错误后再次引用该行。