java的空指针异常?明明没有显示,却总是错误
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.*;
public class Mywares {
public static void write(String path,Object obj)throws IOException
{
File f = new File(path) ;
OutputStream out = new FileOutputStream(f,true) ;
ObjectOutputStream oos = new ObjectOutputStream(out) ;//写入对象
oos.writeObject(obj) ;
oos.close() ;
System.out.println("-----写入成功....");
}
public static Object read(String path) throws Exception
{
File f = new File(path) ;
InputStream in = new FileInputStream(f) ;
ObjectInputStream ois = new ObjectInputStream(in) ;//读取对象
Object obj = ois.readObject();
ois.close();
return obj ;
}
public static void main(String[] args) throws Exception,IOException
{
Scanner in=new Scanner(System.in);
Ware [] ware=new Ware[100];
int i=0;
String path="f:\\test.txt";
HashMap<Integer,Ware> map=new HashMap<Integer,Ware>();//哈希表内<>必须有类,inteGer是一个包装类
int s=in.nextInt();
if(s==1)
{
while(true)
{
System.out.println("输入商品id");
int a1=in.nextInt();
ware[i].setId(a1);
System.out.println("输入商品名称");
String a2=in.next();
ware[i].setName(a2);
System.out.println("输入商品入库价");
int a3=in.nextInt();
ware[i].setMy_money(a3);
System.out.println("输入商品数量");
int a4=in.nextInt();
ware[i].setNum(a4);
System.out.println("输入商品出售价格");
int a5=in.nextInt();
ware[i].setSell_money(a5);
System.out.println("已成功输入商品 "+ware[i]);
map.put(ware[i].getId(), ware[i]);
write(path,ware[i]);
i++;
}
}
}
}
[解决办法]
ware[i]=new Ware(a1, a2,a3, a4,a5);
write(path,ware[i]);
System.out.println("已成功输入商品 "+ware[i]);
map.put(ware[i].getId(), ware[i]);
后面改成这个啊
先new 对象到数组里
然后把前面的 赋值的都注释掉...