2016-09-12 31 views
0

Trying to sort an array of objects by overallVisits from greatest to smallest but get an error. The goal is to pass in website objects to the array then have them sorted out according to greatest overall visit to least visit. I get the following error: Exception in thread "main" java.lang.NullPointerException at java.util.ComparableTimSort.binarySort(ComparableTimSort.java:258) at java.util.ComparableTimSort.sort(ComparableTimSort.java:185) at java.util.Arrays.sort(Arrays.java:1246) at Project1.main(Project1.java:32)排序對象數組?用戶輸入和比較

import java.util.Scanner; 
import java.util.Arrays; 

public class Project1 
{ 
public static void main (String args[]) 
{ 
Scanner input = new Scanner(System.in); 

Website [] websites = new Website[20]; 

String temp; 
String temp2; 
int days = 4;  

for(int i=0; i < 10; i++) 
{ 
     System.out.println("Enter the URL of the Website:"); 
     temp = input.nextLine(); 
     websites[i] = new Website(); 
     websites[i].setUrl(temp); 

     System.out.println("Enter the name of the Website:"); 
     temp2 = input.nextLine(); 
     websites[i].setName(temp2); 

    } 

Arrays.sort(websites); 

for(int i = 0; i<10; i++) 
{ 
     System.out.println("URL:" + websites[i].getUrl()+ " Current Visits:" +  websites[i].getCurvisit() + " Overall Visits:" + websites[i].getOvrvisit()); 

     System.out.println("Name of Website:" + websites[i].getName()); 

} 

} 
} 
//Class 2: 
import java.util.Scanner; 
import java.util.Random; 

public class Website implements Comparable <Website> 
{ 
private String url; 
private String name; 
private int currentVisit; 
private int overallVisit; 

public Website() 
{ 
    this.url = url; 
    this.name = name; 
    this.currentVisit = getRandom1(); 
    this.overallVisit = getRandom2(); 

} 

public String getUrl() 
{ 
    return url; 
} 

public void setUrl(String s) 
{ 
    url = s; 
} 

public String getName() 
{ 
    return name; 
} 

public void setName(String n) 
{ 
    name = n; 
} 

public int getRandom1() 
{ 
Random rand = new Random(); 

int num = rand.nextInt(4); 
return num; 
} 

public int getRandom2() 
{ 
Random rand = new Random(); 

int num2 = 10 + rand.nextInt(11); 
return num2; 
} 

public int getCurvisit() 
{ 
return currentVisit; 
} 

public int getOvrvisit() 
{ 
    return overallVisit; 
} 

public int compareTo(Website compareVisit) 
{ 
    int compareAll = ((Website) compareVisit).getOvrvisit(); 
    return compareAll - this.overallVisit; 
} 
} 
+0

如果您需要其他地方的「網站」對象,則可以只需使用TreeMap併爲訪問定義比較器,並讓DataSet爲您處理工作。它甚至可以動態工作。否則,讓'Website'實現'Comparable',並且定義一個合適的'compareTo'方法,並且執行你正在做的事情。讓我知道你是否想要一個簡單的例子。 – MeetTitan

+0

我編輯了這個問題,它錯過了我的其他課程。看來,我的方法比較不起作用。如果你能以某種方式展示並示範或指引我朝着正確的方向發展,那麼我們將非常感激。 – Tools

+0

代碼中的兩個錯誤 第1步:在構造函數中更改20到10或將循環更改爲20. NPE的原因是您在數組中存儲空值。 ComparableTimSort的實現不檢查null。 refernce:http://stackoverflow.com/questions/23796951/why-do-i-have-nullpointerexception-in-my-sort-implementation Website [] websites = new Website [20]; 更改爲 網站[]網站=新網站[10]; 第2步: 儘管您已經提到過overallVisits,但您尚未在循環中設置其值。 – Sanka

回答

0

你得到一個java.lang.NullPointerException因爲你分配你的數組中的20個元素,但只用10

新陣列

Website [] websites = new Website[20]; 

填充它填補陣列

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

我必須從20個不同的網站的列表文本文件中挑選10個,並隨機給他們訪問。初始訪問的值範圍是當天的0-3和總體的10-20。節目開始時,根據受歡迎程度顯示所有相關信息的網站(訪問量最大的網站最受歡迎)。由於訪問次數是隨機的,因此對於程序的每次運行,輸出應該不同。 – Tools