NullPointerException

Moliay 2018-11-18 01:35:26
package javaPractice;

import java.util.*;

public class TestBook {
public static void main(String[] argsment) {
Book[] book = new Book[10];
int j=0,k=0;
double[] arr = new double[5];
String[] name1 = {"Python","C","Linux/Unix","how to form Linux/Unix","Linux Shell"};
String[] name2 = {"ISBN 962-215-001-2","ISBN 962-215-001-3","ISBN 962-215-001-4","ISBN 962-215-001-5","ISBN 962-215-001-6","ISBN 962-215-001-8","ISBN 962-215-002-21"};
Random random = new Random();
for(int i=0;i<book.length;i++) {
j = random.nextInt(name1.length);
k = random.nextInt(name2.length);
book[i].setAuthor(name1[j]);
book[i].setISBN(name2[k]);;
book[i].setPrice((double)(Math.random()*200));
arr[i] = book[i].getPrice();
}
Arrays.sort(arr);
for(int i=0;i<book.length/2;i++) {
System.out.print("author:"+book[i].getAuthor());
System.out.print("\nISBN:"+book[i].getISBN());
System.out.println("\nprice:"+book[i].getPrice());
}
}
}

class Book{
private String author;
private String ISBN;//书号
private double price;

public String getAuthor() {return this.author;}
public void setAuthor(String author) {this.author = author;}

public String getISBN() {return this.ISBN;}
public void setISBN(String ISBN) {this.ISBN = ISBN;}

public double getPrice() {return this.price;}
public void setPrice(double price) {this.price = price;}
}
...全文
417 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
16行空指针异常。因为你new了一个book数组( Book[] book = new Book[10];),未初始化前引用变量book中的值book[i]默认为null,变量为null不能使用。你可以通过 book[i] = new Book();为每一个book[i]初始化一个对象。
TJkaklf 2018-11-21
  • 打赏
  • 举报
回复
arr数组长度是5 但是你的的循环次数是10 但i=5的时候 arr[5]根本就不存在了。
zhjl1989 2018-11-21
  • 打赏
  • 举报
回复
Book[] book = new Book[10]; 只是定义了数组的长度 此时数组会有一个默认值 里面回事10个null,你在16行去调用set方法 肯定就报NullPointerException
何必要如此 2018-11-20
  • 打赏
  • 举报
回复
arr数组长度是5 但是你的的循环次数是10 但i=5的时候 arr[5]根本就不存在了。
_jant 2018-11-19
  • 打赏
  • 举报
回复
记得初始化啊 老铁
小宇 2018-11-19
  • 打赏
  • 举报
回复
Book[] book = new Book[10];这个数组里是十个null, 解决办法:在Book类加个构造函数, 在这行k = random.nextInt(name2.length);下面加个创建对象 book[i] = new Book(); 不会空指针了
Nihility/ 2018-11-19
  • 打赏
  • 举报
回复
引用 2 楼 qq_16127313 的回复:

 public static void main(String[] argsment)
    {
        Book[] book = new Book[10];
        for (int i = 0; i < 10; i++)
        {
            book[i] = new Book();
        }
        int j = 0, k = 0;
        double[] arr = new double[10];
        String[] name1 = {"Python", "C", "Linux/Unix", "how to form Linux/Unix", "Linux Shell"};
        String[] name2 = {"ISBN 962-215-001-2", "ISBN 962-215-001-3", "ISBN 962-215-001-4", "ISBN 962-215-001-5", "ISBN 962-215-001-6", "ISBN 962-215-001-8", "ISBN 962-215-002-21"};
        Random random = new Random();
        for (int i = 0; i < book.length; i++)
        {
            j = random.nextInt(name1.length);
            k = random.nextInt(name2.length);
            
            book[i].setAuthor(name1[j]);
            book[i].setISBN(name2[k]);
            book[i].setPrice((double)(Math.random() * 200));
            arr[i] = book[i].getPrice();
        }
        Arrays.sort(arr);
        for (int i = 0; i < book.length / 2; i++)
        {
            System.out.print("author:" + book[i].getAuthor());
            System.out.print("\nISBN:" + book[i].getISBN());
            System.out.println("\nprice:" + book[i].getPrice());
        }
    }



正解
随缘心 2018-11-19
  • 打赏
  • 举报
回复
你应该使用ArrayList容器装载Book类,for循环时每次new Book() 再对book赋值.另外你如果想排序的话 应该让Book类实现Comparable接口
qq_39936465 2018-11-19
  • 打赏
  • 举报
回复
是 类没有初始化 需要 book[i]=new Book();
qq_39936465 2018-11-19
  • 打赏
  • 举报
回复
哦,是我看错了
qq_39936465 2018-11-19
  • 打赏
  • 举报
回复
你book类里根本没写set 只有get
月幻真实 2018-11-19
  • 打赏
  • 举报
回复

import java.util.*;

public class TestBook {
public static void main(String[] argsment) {
Book[] book = new Book[10];
int j = 0, k = 0;
double[] arr = new double[5];
String[] name1 = { "Python", "C", "Linux/Unix", "how to form Linux/Unix", "Linux Shell" };
String[] name2 = { "ISBN 962-215-001-2", "ISBN 962-215-001-3", "ISBN 962-215-001-4", "ISBN 962-215-001-5",
"ISBN 962-215-001-6", "ISBN 962-215-001-8", "ISBN 962-215-002-21" };
Random random = new Random();
for (int i = 0; i < book.length; i++) {
j = random.nextInt(name1.length);
k = random.nextInt(name2.length);
if (book[i] == null) {
book[i] = new Book();
}
book[i].setAuthor(name1[j]);
book[i].setISBN(name2[k]);
;
book[i].setPrice((double) (Math.random() * 200));
if (i < arr.length) {
arr[i] = book[i].getPrice();
}
}
Arrays.sort(arr);
for (int i = 0; i < book.length / 2; i++) {
System.out.print("author:" + book[i].getAuthor());
System.out.print("\nISBN:" + book[i].getISBN());
System.out.println("\nprice:" + book[i].getPrice());
}
}
}

class Book {
private String author;
private String ISBN;// 书号
private double price;

public String getAuthor() {
return this.author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getISBN() {
return this.ISBN;
}

public void setISBN(String ISBN) {
this.ISBN = ISBN;
}

public double getPrice() {
return this.price;
}

public void setPrice(double price) {
this.price = price;
}
}
Moliay 2018-11-19
  • 打赏
  • 举报
回复
感谢各位猿兄的给力帮助,已解决
package javaPractice;

import java.text.*;
import java.util.*;

public class TestBook {
public static void main(String[] argsment) {
Book[] book = new Book[10];
int j=0,k=0;
String[] name1 = {"杨红云","吴军","屈婉玲","李开复","凡尔纳","王小波"};
String[] name2 = {"ISBN 962-215-001-2","ISBN 962-215-001-3","ISBN 962-215-001-4","ISBN 962-215-001-5","ISBN 962-215-001-6","ISBN 962-215-001-8","ISBN 962-215-002-21"};
Random random = new Random();
DecimalFormat df = new DecimalFormat( "0.00");
double[] arr = new double[10];
for(int i=0;i<book.length;i++) {
book[i] = new Book();
}
for(int i=0;i<book.length;i++) {
j = random.nextInt(name1.length);
k = random.nextInt(name2.length);
book[i].setAuthor(name1[j]);
book[i].setISBN(name2[k]);;
book[i].setPrice(random.nextDouble()*200);
arr[i] = book[i].getPrice();
}
for(int i=0;i<book.length;i++) {
System.out.print("author:"+book[i].getAuthor());
System.out.print(" "+book[i].getISBN());
System.out.println(" price:"+df.format(book[i].getPrice()));
}
Arrays.sort(arr);
for(int i=0;i<book.length;i++) {
if(book[i].getPrice() == arr[arr.length-1]) {
System.out.println("书价最高图书的书号为"+book[i].getISBN());
}
}
}
}

class Book{
private String author;
private String ISBN;//书号
private double price;

public String getAuthor() {return this.author;}
public void setAuthor(String author) {this.author = author;}

public String getISBN() {return this.ISBN;}
public void setISBN(String ISBN) {this.ISBN = ISBN;}

public double getPrice() {return this.price;}
public void setPrice(double price) {this.price = price;}
}
爱码少年 2018-11-18
  • 打赏
  • 举报
回复

public static void main(String[] argsment)
{
Book[] book = new Book[10];
for (int i = 0; i < 10; i++)
{
book[i] = new Book();
}
int j = 0, k = 0;
double[] arr = new double[10];
String[] name1 = {"Python", "C", "Linux/Unix", "how to form Linux/Unix", "Linux Shell"};
String[] name2 = {"ISBN 962-215-001-2", "ISBN 962-215-001-3", "ISBN 962-215-001-4", "ISBN 962-215-001-5", "ISBN 962-215-001-6", "ISBN 962-215-001-8", "ISBN 962-215-002-21"};
Random random = new Random();
for (int i = 0; i < book.length; i++)
{
j = random.nextInt(name1.length);
k = random.nextInt(name2.length);

book[i].setAuthor(name1[j]);
book[i].setISBN(name2[k]);
book[i].setPrice((double)(Math.random() * 200));
arr[i] = book[i].getPrice();
}
Arrays.sort(arr);
for (int i = 0; i < book.length / 2; i++)
{
System.out.print("author:" + book[i].getAuthor());
System.out.print("\nISBN:" + book[i].getISBN());
System.out.println("\nprice:" + book[i].getPrice());
}
}


Moliay 2018-11-18
  • 打赏
  • 举报
回复
Exception in thread "main" java.lang.NullPointerException
at freshman/javaPractice.TestBook.main(TestBook.java:16)
求教大佬

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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