在Library类中定义的方法编写时出现的警告是:通过公共API导出非公共类型
运行到logIn()方法时就走不下去了,指示空指针错误我寻思着是类内数据访问权限的问题,但总改不对,有高手给看看吧!
先谢谢了!
import java.util.*;public class Library
{
        static final int SMax=3;
static final int BMax=10;
private static Student [] students=new Student[SMax];
private static Book [] Ocean=new Book[BMax];
        private Book kind; public void logIn(Student astu)
{
            String n;
            String c;
            Scanner s = new Scanner(System.in);
            System.out.println("姓名:");
            n=s.nextLine();
            System.out.println("学号:");
            c=s.nextLine();
            for(Student e : students)
{
if(e.getcode().equals(c))
                        {
                            astu=e;
                            System.out.println("登陆成功!");
                            break;
                        }
                        else
{
e=new Student(n,c);
                                astu=e;
                                System.out.println("注册成功!转入功能菜单。");
break;
}
}
}
public static void helpPrint()
{
System.out.println("==================================================\n"
+"功能选项:\n"
+"\t1.借书\t2.还书\n"
+"=================================================\n");
}
public static void bookPrint()
{
//示例书
Ocean[0]=new Book("SC0001","高等数学《一》","图书馆");
Ocean[1]=new Book("SC0002","高等数学《二》","图书馆");
Ocean[2]=new Book("SC0003","线性代数","图书馆");
                                Ocean[3]=new Book("SC0004","微分方程","图书馆");
Ocean[4]=new Book("SC0004","离散数学","图书馆");
Ocean[5]=new Book("SC0005","复变函数","图书馆");
Ocean[6]=new Book("LA0001","汉语","图书馆");
Ocean[7]=new Book("LA0002","日语","图书馆");
Ocean[8]=new Book("LA0003","法语","图书馆");
Ocean[9]=new Book("LA0004","英语","图书馆");
//打印书单
                    for(Book e : Ocean)
                        e.Lay();
}
/* private String output()
        {            return
        }
        public void search()
        {        }   */
        public static void getBook(String n,Book l)
{
for(int i=0;i<BMax;i++)
{
if(Ocean[i].getBName().equals(n));
l=Ocean[i];
break;
}
}
public void borrowBook(String n,String x,Book y)
{
for(Student e:students)
{
if(e.getcode().equals(x))
{System.out.println("OK");
e.borrow(y);
y.isBorrowed(n);
break;
}
}
}
public void returnBook(String n,String x,Book y)
{
for(Student e:students)
{
if(e.getcode().equals(x))
{
e.returnB(y);
y.isBorrowed("图书馆");
break;
}
}
}
public static void main(String [] args)
{
//String n;
String bn;
int chose,m;
Book a=null;
                Student astudent=null;
System.out.println("***************************"
+"请输入您的姓名和学号登录!"
+"***************************\n");
Scanner s = new Scanner(System.in);
Library library=new Library();
library.logIn(astudent); Library.helpPrint(); chose=s.nextInt();
if(chose==1)
{ //提示信息
System.out.println("同学你好,以下是本馆图书信息,欢迎借阅!");
Library.bookPrint();
//借书
System.out.println("请输入要借阅的书本数:(不得多于六本)");
m=s.nextInt();
for(int i=0;i<m;i++)
{
System.out.println("请选择需要借阅的书籍名称:");
bn=s.nextLine();
Library.getBook(bn,a);
library.borrowBook(astudent.getname(),astudent.getcode(),a);
}
}
else
{
//提示信息
System.out.println("请输入要归还的书本数:");
m=s.nextInt();
for(int i=0;i<m;i++)
{
System.out.println("请输入所还书籍书号:");
bn=s.nextLine();
Library.getBook(bn,a);
library.borrowBook(astudent.getname(),astudent.getcode(),a);
}
}
}
}class Book
{
private String S_name;
private String B_name;
private String B_number;
public Book()
{}
public Book(String name1,String name2,String code)
{
S_name=name1;
B_name=name2;
B_number=code;
}
public void isBorrowed(String aperson)
{
S_name=aperson;
}
public String getPName()
{
return S_name;
}
public String getBName()
{
return B_name;
}
public String getNum()
{
return B_number;
}
public void Lay()
{
            System.out.println("书号\t书名\t借出信息\n");
                if(S_name.equals("图书馆"))
System.out.println(getNum()+"\t"+getBName()+"\t"+getPName()+"\n");
}
}class Student
{
private int MAX=6;
private int count=0;
private String S_name;
private String S_code;
private Book [] borrowed=new Book [MAX]; public Student()
{
            S_name=null;
            S_code=null;
        }
public Student(String name,String code)
{
S_name=name;
S_code=code;
borrowed=null;
}
public String getname()
{
return S_name;
}
public String getcode()
{
return S_code;
}
public void borrow(Book abook)
{
for(int i=0;i<MAX;i++)
{
while(borrowed[i]==null)
{
borrowed[i]=abook;
count++;
break;
}
}
}
public void returnB(Book abook)
{
for(int i=0;i<MAX;i++)
{
if(borrowed[i].getNum().equals(abook.getNum()))
borrowed[i]=null;
break;
}
}
public void studentInfo()
{
System.out.println("Student's ID is "+S_code+",name is "+S_name
+"\nBorrowed "+count+"\nList:\n");
Listof();
}
private void Listof()
{
for(int i=0;i<count;i++)
System.out.println(borrowed[i].getNum()+borrowed[i].getBName());
}
}

解决方案 »

  1.   

    public static void main(String [] args) 

    //String n; 
    String bn; 
    int chose,m; 
    Book a=null;
     
    Student astudent=null; //这个地方对象没有赋值System.out.println("***************************" 
    +"请输入您的姓名和学号登录!" 
    +"***************************\n"); 
    Scanner s = new Scanner(System.in); 
    Library library=new Library();
     
    library.logIn(astudent); //一个Null直接传进去,你还指望怎么着?e.getcode()当然是空指针了 
      

  2.   

    java中不能在主函数中定义一个变量作为参数传递给类方法么?例如这个题,怎样改动一点即得到正确的调用方法呢?
      

  3.   


    你的变量值是null,然后你在方法内部调用了e.getcode()
    在一个Null对象上调用任何方法都会报空指针异常
      

  4.   


    解决问题很简单,给变量赋一个非null的值就行了
      

  5.   

    Student astudent=null
    改为:Student astudent= new Student;
      

  6.   

    在Library里加了一个对数组的初始化方法后还是卡在那里能帮忙改改告诉我吗?谢谢!
      

  7.   


    if(e!=null&&e.getcode().equals(x)) {
      //做你自己想做的事情
    }
      

  8.   

    这样改:
    import java.util.*; public class Library 

            static final int SMax=3; 
    static final int BMax=10; 
    private static Student [] students=new Student[SMax]; 
    private static Book [] Ocean=new Book[BMax]; 
            private Book kind; 
    void inInt()
    { for (int i = 0;i<students.length ;i++ )
    {  students[i] = new Student("ren","1");
    }
    }public void logIn(Student astu) 

                String n; 
                String c; 
                Scanner s = new Scanner(System.in); 
                System.out.println("姓名:"); 
                n=s.nextLine(); 
                System.out.println("学号:"); 
                c=s.nextLine(); 
                for(Student e : students) 

    if(e.getcode().equals(c)) 
                            { 
                                astu=e; 
                                System.out.println("登陆成功!"); 
                                break; 
                            } 
                            else 

    e=new Student(n,c); 
                                    astu=e; 
                                    System.out.println("注册成功!转入功能菜单。"); 
    break; 



    public static void helpPrint() 

    System.out.println("==================================================\n" 
    +"功能选项:\n" 
    +"\t1.借书\t2.还书\n" 
    +"=================================================\n"); 

    public static void bookPrint() 

    //示例书 
    Ocean[0]=new Book("SC0001","高等数学《一》","图书馆"); 
    Ocean[1]=new Book("SC0002","高等数学《二》","图书馆"); 
    Ocean[2]=new Book("SC0003","线性代数","图书馆"); 
                                    Ocean[3]=new Book("SC0004","微分方程","图书馆"); 
    Ocean[4]=new Book("SC0004","离散数学","图书馆"); 
    Ocean[5]=new Book("SC0005","复变函数","图书馆"); 
    Ocean[6]=new Book("LA0001","汉语","图书馆"); 
    Ocean[7]=new Book("LA0002","日语","图书馆"); 
    Ocean[8]=new Book("LA0003","法语","图书馆"); 
    Ocean[9]=new Book("LA0004","英语","图书馆"); 
    //打印书单 
                        for(Book e : Ocean) 
                            e.Lay(); 

    /* private String output() 
            {             return 
            } 
            public void search() 
            {         }  */ 
            public static void getBook(String n,Book l) 

    for(int i=0;i <BMax;i++) 

    if(Ocean[i].getBName().equals(n)); 
    l=Ocean[i]; 
    break; 


    public void borrowBook(String n,String x,Book y) 

    for(Student e:students) 

    if(e.getcode().equals(x)) 
    {System.out.println("OK"); 
    e.borrow(y); 
    y.isBorrowed(n); 
    break; 



    public void returnBook(String n,String x,Book y) 

    for(Student e:students) 

    if(e.getcode().equals(x)) 

    e.returnB(y); 
    y.isBorrowed("图书馆"); 
    break; 



    public static void main(String [] args) 

    //String n; 
    String bn; 
    int chose,m; 
    Book a=null; 
    Student astudent=new Student(); 
    System.out.println("***************************" 
    +"请输入您的姓名和学号登录!" 
    +"***************************\n"); 
    Scanner s = new Scanner(System.in); 
    Library library=new Library();
    library.inInt();
    library.logIn(astudent); Library.helpPrint(); chose=s.nextInt(); 
    if(chose==1) 
    { //提示信息 
    System.out.println("同学你好,以下是本馆图书信息,欢迎借阅!"); 
    Library.bookPrint(); 
    //借书 
    System.out.println("请输入要借阅的书本数:(不得多于六本)"); 
    m=s.nextInt(); 
    for(int i=0;i <m;i++) 

    System.out.println("请选择需要借阅的书籍名称:"); 
    bn=s.nextLine(); 
    Library.getBook(bn,a); 
    library.borrowBook(astudent.getname(),astudent.getcode(),a); 


    else 

    //提示信息 
    System.out.println("请输入要归还的书本数:"); 
    m=s.nextInt(); 
    for(int i=0;i <m;i++) 

    System.out.println("请输入所还书籍书号:"); 
    bn=s.nextLine(); 
    Library.getBook(bn,a); 
    library.borrowBook(astudent.getname(),astudent.getcode(),a); 



    } class Book 

    private String S_name; 
    private String B_name; 
    private String B_number; 
    public Book() 
    {} 
    public Book(String name1,String name2,String code) 

    S_name=name1; 
    B_name=name2; 
    B_number=code; 

    public void isBorrowed(String aperson) 

    S_name=aperson; 

    public String getPName() 

    return S_name; 

    public String getBName() 

    return B_name; 

    public String getNum() 

    return B_number; 

    public void Lay() 

                System.out.println("书号\t书名\t借出信息\n"); 
                    if(S_name.equals("图书馆")) 
    System.out.println(getNum()+"\t"+getBName()+"\t"+getPName()+"\n"); 

    } class Student 

    private int MAX=6; 
    private int count=0; 
    private String S_name; 
    private String S_code; 
    private Book [] borrowed=new Book [MAX]; public Student() 

                S_name=null; 
                S_code=null; 
            } 
    public Student(String name,String code) 

    S_name=name; 
    S_code=code; 
    borrowed=null; 

    public String getname() 

    return S_name; 

    public String getcode() 

    return S_code; 

    public void borrow(Book abook) 

    for(int i=0;i <MAX;i++) 

    while(borrowed[i]==null) 

    borrowed[i]=abook; 
    count++; 
    break; 



    public void returnB(Book abook) 

    for(int i=0;i <MAX;i++) 

    if(borrowed[i].getNum().equals(abook.getNum())) 
    borrowed[i]=null; 
    break; 


    public void studentInfo() 

    System.out.println("Student's ID is "+S_code+",name is "+S_name 
    +"\nBorrowed "+count+"\nList:\n"); 
    Listof(); 

    private void Listof() 

    for(int i=0;i <count;i++) 
    System.out.println(borrowed[i].getNum()+borrowed[i].getBName()); 

    }
      

  9.   

    还有Ocean都没初始化,自己改吧
      

  10.   

    在问一句
    Ocean在bookprint方法中不是初始化了么。。
      

  11.   

    package library;import java.util.Scanner;public class Library {
       static final int SMax = 3;
       static final int BMax = 10;
       private static Student[] students = new Student[SMax];//需要赋值,这里只是给数组分配了3个空间,初始化都为null 所以回报null指针异常
      
       private static Book[] Ocean = new Book[BMax];
       private Book kind;
       public void logIn(Student astu){
       String n;
       String c;
       Scanner s = new Scanner(System.in);
       System.out.println("姓名:");
       n = s.nextLine();
       System.out.println("学号:");
       c = s.nextLine();
       students[0] = new Student("aa","001");//我在这里赋值了
       students[1] = new Student("bb","002");
       students[2] = new Student("cc","003");
       for(Student e:students){
       if(e.getcode().equals(c)){
       astu = e;
       System.out.println("登陆成功!");
       break;
       }else{
       e = new Student(n,c);
       astu = e;
       System.out.println("注册成功!转让功能菜单。");
       break;
       }
       }
       }
       public static void helpPrint(){
       System.out.println("=============================\n"+
       "功能选项:\n"
       +"\t1.借书\t2.还书\n"
       +"========================================\n");
       }
       public static void bookPrint(){
     //示例书     Ocean[0]= new Book("SC0001","高等数学《一》","图书馆");
       Ocean[1]= new Book("SC0002","高等数学《一》","图书馆");
       Ocean[2]= new Book("SC0003","线性代数","图书馆");
       Ocean[3]=new Book("SC0004","微分方程","图书馆"); 
       Ocean[4]=new Book("SC0004","离散数学","图书馆"); 
       Ocean[5]=new Book("SC0005","复变函数","图书馆"); 
       Ocean[6]=new Book("LA0001","汉语","图书馆"); 
       Ocean[7]=new Book("LA0002","日语","图书馆"); 
       Ocean[8]=new Book("LA0003","法语","图书馆"); 
       Ocean[9]=new Book("LA0004","英语","图书馆"); 
     //打印书单 
        for(Book e:Ocean){
         e.Lay();
        }   }
       public static void getBook(String n,Book l){
       for(int i=0;i<BMax;i++){
       if(Ocean[i].getBName().equals(n));
       l = Ocean[i];
       break;
       }
       }
       public void borrowBook(String n,String x,Book y){
       for(Student e:students){
       if(e.getcode().equals(x)){
       System.out.println("OK");
       e.borrow(y);
       y.isBorrowed(n);
       break;
       }
       }
       }
       public void returnBook(String n,String x,Book y){
       for(Student e:students){
       if(e.getcode().equals(x)){
       e.returnB(y);
       y.isBorrowed("图书馆");
       break;
       }
       }
       }
       public static void main(String[] args){
       String bn;
       int chose,m;
       Book a = null;
       Student astudent = null;
       System.out.println("***********************"
       +"请输入您的姓名和学号登录!"
       +"************************\n");
       Scanner s = new Scanner(System.in);
       Library library = new Library();
       library.logIn(astudent);
       Library.helpPrint();
       chose = s.nextInt();
       if(chose==1){
       System.out.println("同学你好,以下是本馆图书信息,欢迎借阅!");
       Library.bookPrint();
       //借书
       System.out.println("请输入要借阅的书本数:(不得多于六本)");
       m = s.nextInt();
       for(int i=0;i<m;i++){
       System.out.println("请选择需要借阅的书籍名称:");
       bn = s.nextLine();
       Library.getBook(bn, a);
       library.borrowBook(astudent.getname(), astudent.getcode(), a);//还会报null指针异常,你太多的没有赋值
       }
       }else{
       System.out.println("请输入要归还的书本数:");
       m = s.nextInt();
       for(int i=0;i<m;i++){
       System.out.println("请输入所还书籍书号:");
       bn = s.nextLine();
       Library.getBook(bn, a);
       library.borrowBook(astudent.getname(), astudent.getcode(), a);
       }
       }
       }
    }
    public class Book {
      private String S_name;
      private String B_name;
      private String B_number;
      public Book(){}
      public Book(String name1,String name2,String code){
      this.S_name = name1;
      this.B_name = name2;
      this.B_number = code;
      }
      public void isBorrowed(String aperson){
      S_name = aperson;
      }
      public String getPName(){
      return S_name;
      }
      public String getBName(){
      return B_name;
      }
      public String getNum(){
      return B_number;
      }
      public void Lay(){
      System.out.println("书号\t书名\t借出信息\n");
     // if(S_name.equals("图书馆")) 这里是干什么呢?
      System.out.println(getNum()+"\t"+getBName()+"\t"+getPName()+"\n");
      }
    }运行结果:
    ***********************请输入您的姓名和学号登录!************************姓名:
    aa
    学号:
    001
    登陆成功!
    =============================
    功能选项:
    1.借书 2.还书
    ========================================1
    同学你好,以下是本馆图书信息,欢迎借阅!
    书号 书名 借出信息图书馆 高等数学《一》 SC0001书号 书名 借出信息图书馆 高等数学《一》 SC0002书号 书名 借出信息图书馆 线性代数 SC0003书号 书名 借出信息图书馆 微分方程 SC0004书号 书名 借出信息图书馆 离散数学 SC0004书号 书名 借出信息图书馆 复变函数 SC0005书号 书名 借出信息图书馆 汉语 LA0001书号 书名 借出信息图书馆 日语 LA0002书号 书名 借出信息图书馆 法语 LA0003书号 书名 借出信息图书馆 英语 LA0004请输入要借阅的书本数:(不得多于六本)
    1
    Exception in thread "main" 请选择需要借阅的书籍名称:
    java.lang.NullPointerException
    at library.Library.main(Library.java:110)