01/10/2018, 00:37

Error nullpointerexception

public class Bai1 {

private int n;
private String[] arr = new String[10];
private final Scanner reader = new Scanner(System.in);

public void nhapSinhVien() {
    System.out.println("Nhap n: ");
    this.n = reader.nextInt();
    reader.nextLine();
    System.out.println("Nhap sinh vien.");
    for (int i = 0; i < this.n; i++) {
        System.out.println("Nhap sinh vien thu " + (i + 1));
        this.arr[i] = reader.nextLine();
    }
}

public void sapXep() {
    for (int i = this.n; i > 0; i--) {
        for (int j = 0; j < this.n - 1; j++) {
            if ((this.arr[i]).compareTo((this.arr[i + 1])) > 0) {
                String text = this.arr[i];
                this.arr[i] = this.arr[i + 1];
                this.arr[i + 1] = text;
            }
        }
    }
}

ở dòng if ((this.arr[i]).compareTo((this.arr[i + 1])) > 0) bị lỗi nullpointerexception .
ai biết chỉ giúp e với.

Tynk Huynk viết 02:40 ngày 01/10/2018

((this.arr[i]).compareTo((this.arr[i + 1])) > 0) thay i bang j

Trần Ngọc Khoa viết 02:52 ngày 01/10/2018
for (int i = this.n; i > 0; i--) {
        for (int j = 0; j < this.n - 1; j++) {
            if ((this.arr[i]).compareTo((this.arr[i + 1])) > 0)

chỗ này nè bạn

Vũ Văn Việt viết 02:39 ngày 01/10/2018

ừ nhỉ. lỗi sai cỏn con mà nhìn mãi ko ra.

Bài liên quan
0