返回列表 发帖

《Java从入门到实践》4.6本章实例怎么不能运行

Book的源代码为:
package cn.com.jaking.project;
public class Book {
        private int id;                           //图书编号
        private String name;                      //图书名称
        private Booktype type;                    //图书类别
        private String author;                    //作者
        private String translat;                  //译者
        private String publishing_company;        //出版社
        private double price;                    //价格
        private int stock;                       //库存数量
        //无参数构造法
        public Book () {
        }
        //带参数构造法
        public Book (int id, String name, Booktype type, String author, String translat, String publishing_company, double price, int stock) {
                this.id = id;
                this.name = name;
                this.type = type;
                this.author = author;
                this.translat = translat;
                this.publishing_company = publishing_company;
                this.price = price;
                this.stock = stock;
        }
public String getAuthor () {
         return author;
}
public void setAuthor (String author) {
         this.author = author;
}
public int getId () {
         return id;
}
public void setId (int id) {
         this.id = id;
}
public Booktype getType () {
         return type;
}
public void setType (Booktype type) {
         this.type = type;
}
public String getName () {
         return name;
}
public void setName (String name) {
         this.name = name;
}
public double getPrice () {
         return price;
}
public void setPrice (double price) {
         this.price = price;
}
public String getPublishing_company () {
         return publishing_company;
}
public void setPublishing_company (String publishing_company) {
         this.publishing_company = publishing_company;
}
public int getStock () {
         return stock;
}
public void setStock (int stock) {
         this.stock = stock;
}
public String getTranslat () {
         return translat;
}
public void setTranslat (String translat) {
         this.translat = translat;
}
}
Booktype的源代码为:
package cn.com.jaking.project;

public class Booktype {
        private int id;
        private String name;
        private int days;
        public Booktype () {
        }
        public Booktype (int id, String name, int days) {
                this.id = id;
                this.name = name;
                this.days = days;
        }
        public int getDays() {
                return days;
        }
        public void setDays (int days) {
                this.days = days;
        }
        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;
        }
}

Bookmanager的源代码为:
package cn.com.jaking.project;
import java.util.List;
public class Bookmanager {
        //将图书book增加到数据库
        public void add (Book book) {
                //...
        }
        //从数据库中删除图书book
        public void delete (Book book) {
                //...
        }
        //更新数据库中图书book的信息
        public void update (Book book) {
                //...
        }
        //从数据库中查询编号为bookId的图纸
        public Book query (int bookId) {
                Book book = null;
                //...
                return book;
        }
        //从数据库中查询满足条件的所有图书
        public List query (String sql) {
                List bookList = null;
                //...
                return bookList;
        }
}
Test的源代码为:
package cn.com.jaking.project;

public class Test {
        public static void main (String args []) {
                Bookmanager manager = new Bookmanager();
                //增加图书
                Booktype type = new Booktype (1, "科技类", 30); //创建图书类别
                Book book = new Book ();
                book.setId(1);
                book.setName("Java语言入门");
                book.setAuthor("赵大");
                book.setPublishing_company("电子工业出版社");
                book.setPrice(49.80);
                book.setStock(5);
                book.setType(type);
                manager.add(book);
               
                //查询并修改图书
                book = manager.query(1);
                book.setStock(8);
                manager.update(book);               
        }
}
        在Eclipse中,Book,Booktype,Bookmanager是不可以运行的,只有Test可以运行,但运行时,控制台会显示:
Exception in thread "main" java.lang.NullPointerException
at cn.com.jaking.project.Test.main(Test.java:20)
所有的代码在Eclipse中均没有错误标记,这是怎么回事啊?
分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友

你好。你用的光盘中的代码吗?书中代码只给出了部分。请参看一下光盘代码。

TOP

我对过光盘了,把光盘的程序覆盖我的程序,在Eclipse中,Book,Booktype,Bookmanager是不可以运行的,只有Test可以运行,控制台什么都没有显示,这是怎么回事啊?

TOP

Book类,Booktype类是实体类,是用来封装数据的 只能被调用  不可以运行

Bookmanager类中有添加、修改、删除和查询方法
但是这个类中没有main方法
所以不能运行,在Test类中调用这些方法

TOP

在Eclipse中,Book,Booktype,Bookmanager是不可以运行的,只有Test可以运行

TOP

返回列表