java - 为什么这个空指针异常不断出现?我最后写了错误。我还提到了发生错误的行

标签 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 = 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;
    }
}

错误:

Exception in thread "main" java.lang.NullPointerException at Solution.main(Solution.java:32

错误发生在第 32 行。我尝试初始化变量。没有一个是成功的。

最佳答案

当您使用 Shirt[] sh = new Shirt[5]getShirtWithMoreThanSpecificPrice 中分配一个新数组时,它会使用五个 null 值进行初始化.

然后你分配元素,但只是有条件的:if (shirts[i].price > price) sh[i] = shirts[i];。因此,某些值可能仍为 null。当您在 main 中循环此数组时,您必须检查 for (Shirt s: result) s 是否为 null 或不。它可能会发生,这就是这里发生的事情。

现在,最好返回一个仅包含非空值的数组,即 shirts[i].price > price 的那些衬衫。您可以改用 List(仅附加非空值)或 Arrays.copyOf(如果您设法在数组的开头存储非空值 sh,没有孔)。

关于java - 为什么这个空指针异常不断出现?我最后写了错误。我还提到了发生错误的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59342379/

相关文章:

ios - 错误代码1886681407 AudioFileGetProperty给什么?

java - JSF 2 下载具有西里尔文名称的文件

java - 关于Spring Boot上传文件大小的问题

java - 我得到一个 PersistenceException,我不明白为什么

php - 如果出现$ Error,PHP将不显示网格

php - 使用PHP在MVC View 中显示自定义错误消息

java - 我的 GUI 中使用 ActionListener 时出现错误

java - 创建名为 'entityManagerFactory' 的 bean 时出错 - 启动错误

java - Hibernate 二级缓存使用 redis 用于多个容器

delphi - 无效的指针操作 - Delphi XE