在静态数组中调用索引时为空指针

时间:2019-06-17 02:29:03

标签: java nullpointerexception

我有一个游戏,顾客进入餐厅并要求特定的汉堡包订单,我必须将这些订单保存在一个数组中,以便我可以根据要求检查玩家做出了什么,以查看所准备的餐点是否正确。当我在保存订单的数组中调用现货时,我得到一个空指针。

类声明不是问题,我只是没有在这些代码块中包括它。

这是我保存订单信息的地方。唯一重要的是保存的调用,其他所有内容仅是上下文:

public void actionPerformed(ActionEvent e){
    if(y == 40 && vely < 0)
        vely = 0;
    else if(y == 119 && vely > 0)
        vely = 0;
    if(x == 0 && velx < 0)
        velx = 0;
    else if(x == 440 && velx > 0)
        velx = 0;
    x += velx;
    y += vely;

    if(checkCount(count) && stool1.getIcon() == null)
    {
         stool1.setIcon(randomCustomer());
         oneText.setText(c.orderTxt());
         new save(c, 0);
         c = new customer();
         count = 0;
    }
    if(checkCount(count) && stool2.getIcon() == null)
    {
         stool2.setIcon(randomCustomer());
         twoText.setText(c.orderTxt());
         new save(c, 1);
         c = new customer(); 
         count = 0;
    }
    if(checkCount(count) && stool3.getIcon() == null)
    {
         stool3.setIcon(randomCustomer());
         threeText.setText(c.orderTxt());
         new save(c, 2);
         c = new customer(); 
         count = 0;
    }
    if(checkCount(count) && stool4.getIcon() == null)
    {
         stool4.setIcon(randomCustomer());
         fourText.setText(c.orderTxt());
         new save(c, 3);
         c = new customer(); 
         count = 0;
    }
    if(checkCount(count) && stool5.getIcon() == null)
    {
         stool5.setIcon(randomCustomer());
         fiveText.setText(c.orderTxt());
         new save(c, 4);
         c = new customer(); 
         count = 0;
    }
    if(checkCount(count) && stool6.getIcon() == null)
    {
         stool6.setIcon(randomCustomer());
         sixText.setText(c.orderTxt());
         new save(c, 5);
         c = new customer(); 
         count = 0;
    }
    count++;
    repaint(); 
}

这是保存类:

public class save
{
    private static customer[] orders; 

    public save(customer c, int stool)
    {
        orders = new customer[6]; 
        orders[stool] = c; 
    }

    public save(int stool)
    {
        orders[stool] = null; 
    }

    public save(){}

    public customer getRequest(int index)
    {
        return orders[index]; 
    }
}

这是客户类别,尽管这对于解决问题可能并不重要,但可能是:

public class customer 
{
    private String[] toppings;
    private ArrayList<String> order; 

public customer()
{
    toppings = new String[]{"double", "mustard", "ketchup", "lettuce", "tomato", "onion", "bacon", "cheese"};
    order = getOrder(); 
}

public ArrayList<String> getOrder()
{
    ArrayList<String> arr = new ArrayList<String>();
    int num = (int)(Math.random() * 4); 
    for(int i = 0; i < toppings.length; i++)
    {
        if(num == 0) arr.add(toppings[i]);
        num = (int)(Math.random() * 4);
    }
    return arr;
}

public String orderTxt()
{
    String result = "<html>";
    for(String s : order)
    {
        result += s + "<br/>";
    }
    return result + "<html>"; 
}

}

在这里我进行保存调用,并因此进行数组排序,这给了我空指针。

if(key == KeyEvent.VK_SPACE && y == 119)
     {
         if(x < 462 && x > 417)
         {
             System.out.println(s.getRequest(0).orderTxt()); 
         }
         else if(x < 382 && x > 337)
         {
             System.out.println(s.getRequest(1).orderTxt()); 

         }
         else if(x < 302 && x > 257)
         {
             System.out.println(s.getRequest(2).orderTxt()); 

         }
         else if(x < 222 && x > 157)
         {
             System.out.println(s.getRequest(3).orderTxt()); 

         }
         else if(x < 142 && x > 97)
         {
             System.out.println(s.getRequest(4).orderTxt()); 

         }
         else if(x < 62 && x > 17)
         {
             System.out.println(s.getRequest(5).orderTxt()); 

         }
     }

问题发生在我在最后一块代码中打印的地方。

我要打印是因为我在解决问题上遇到了麻烦,所以问题肯定出在静态数组上,但是我给人的印象是,因为它是静态的,所以每次我打电话给新客户时都不会成为新客户[] save(),因此它将保存所有客户价值,但我不知道。

任何帮助将不胜感激!

0 个答案:

没有答案