java的空指针异常?明明没有显示,却总是错误

猫咪爷爷 2013-12-28 05:05:58
我是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.*;

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++;
}
}


}
}
...全文
180 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
猫咪爷爷 2013-12-28
  • 打赏
  • 举报
回复
3楼的答案完美执行,本来还想去百度发的,看来不用了
javakeffer 2013-12-28
  • 打赏
  • 举报
回复
main方法里是死循环吧
Rdxer 2013-12-28
  • 打赏
  • 举报
回复
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 对象到数组里 然后把前面的 赋值的都注释掉...
teemai 2013-12-28
  • 打赏
  • 举报
回复
ware[i] = new Ware();
teemai 2013-12-28
  • 打赏
  • 举报
回复
Ware [] ware=new Ware[100]; 这里面的ware没有初始化

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧