日期:2014-05-20  浏览次数:20771 次

java的空指针异常?明明没有显示,却总是错误
本帖最后由 u011027784 于 2013-12-28 17:13:06 编辑
我是java初学者,写了个老师给的作业,但是如题,明明都没有红线画出,却总是显示:
Exception in thread "main" java.lang.NullPointerException
at mywares.Mywares.main(Mywares.java:59)
这是什么缘故呢?我现在只大概知道是 ware[i].setId(a1)那部分出了问题,能帮忙解决一下吗,大神们,求助啊,如何能成功运行我的代码。
              
package mywares;

/**
 *
 * @author Administrator
 */

import java.io.*;
import java.util.*;

public  class Ware implements Serializable
{
    public int id;
    private static final long serialVersionUID = 1L;

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 89 * hash + this.id;
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Ware other = (Ware) obj;
        if (this.id != other.id) {
            return false;
        }
        return true;
    }
    
    public String name;
    public int num;
    public int my_money;
    public int sell_money;

    public  Ware(int id, String name, int num, int my_money, int sell_money) 
    {
        this.id = id;
        this.name = name;
        this.num = num;
        this.my_money = my_money;
        this.sell_money = sell_money;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public int getMy_money() {
        return my_money;
    }

    public void setMy_money(int my_money) {
        this.my_money = my_money;
    }

    public int getSell_money() {
        return sell_money;
    }

    public void setSell_money(int sell_money) {
        this.sell_money = sell_money;
    }

    @Override
    public String toString() {
        return "商品:" + "商品号:" + id + ",商品名:" + name + ", 库存数量:" + num + ", 入库价格:" + my_money + ", 销售价格:" + sell_money ;
    }
    
}

import java.util.Scanner;
import java.util.HashMap; 
import java.util.Iterator;
import java.io.* ;
import java.text.*;
import java.util.*;
import java.awt.*;