Results 1 to 7 of 7
  1. #1
    Fabken is offline Member
    Join Date
    Nov 2010
    Posts
    18
    Rep Power
    0

    Question Data structures(sort) nullpointerexception

    Hello everyone,

    I'm learning java from the book head first java 2nd edition.
    And I'm almost at the last chapter, but now I'm in chapter 16 data structures.
    And the book explains how to sort a file with strings in it alphabetically.
    And I exactly wrote the same as the book did and I get this error.
    Java Code:
    [null, null, null, null, null, null, null, null, null, null]
    Exception in thread "main" java.lang.NullPointerException
    	at JukeBox$ArtistCompare.compare(JukeBox.java:14)
    	at JukeBox$ArtistCompare.compare(JukeBox.java:1)
    	at java.util.Arrays.mergeSort(Unknown Source)
    	at java.util.Arrays.mergeSort(Unknown Source)
    	at java.util.Arrays.sort(Unknown Source)
    	at java.util.Collections.sort(Unknown Source)
    	at JukeBox.go(JukeBox.java:22)
    	at JukeBox.main(JukeBox.java:9)
    I have two classes JukeBox and Song.
    Here is the song class:
    Java Code:
    class Song implements Comparable<Song>{
    String title;
    String artist;
    String rating;
    String bpm;
    
    public int compareTo(Song s) {
    	return title.compareTo(s.getTitle());
    }
    
    Song(String t, String a, String r, String b) {
    	t = title;
    	a = artist;
    	r = rating;
    	b = bpm;
    }
    
    
    public String getTitle() {
    	return title;
    }
    
    public String getArtist() {
    	return artist;
    }
    public String getRating() {
    	return rating;
    }
    public String getBPM() {
    	return bpm;
    }
    public String toString() {
    	return title;
    }
    
    }
    And here is the JukeBox class:
    Java Code:
    import java.util.*;
    import java.io.*;
    
    
    public class JukeBox {
    
    	ArrayList<Song> songList = new ArrayList<Song>();
    	public static void main(String[] args) {
    		new JukeBox().go();
    		
    	}
    	
    	class ArtistCompare implements Comparator<Song> {
    		public int compare(Song one, Song two) {
    			return one.getArtist().compareTo(two.getArtist());
    		}
    	}
    	
    	
    	public void go() {
    		getSongs();
    		System.out.println(songList);
    		Collections.sort(songList);
    		System.out.println(songList);
    		
    		ArtistCompare artistCompare = new ArtistCompare();
    		System.out.println(songList);
    		
    	}
    	void getSongs() {
    		try {
    			File file = new File("MySongList.txt");
    			BufferedReader reader = new BufferedReader(new FileReader(file));
    			String line = null;
    			while ((line = reader.readLine()) != null){
    				addSong(line);
    			}
    		} catch (Exception ex) {ex.printStackTrace();}
    	}
    	
    	void addSong(String lineToParse) {
    		String[] tokens = lineToParse.split("/");
    		Song nextSong = new Song(tokens[0], tokens[1], tokens[2], tokens[3]);
    		songList.add(nextSong);
    	}
    }
    and this is the textfile MySongList:
    Java Code:
    Pink Moon/Nick Drake/5/80
    Somersault/Zero 7/4/84
    Shiva Moon/Prem Joshua/6/120
    Circles/BT/5/110
    Deep Channel/Afro Celts/4/120
    Passenger/Headmix/4/100
    Can anyone say me why I get a nullpointerexception?

    Thankyou very much,
    Best regards,
    Fabken
    Last edited by Fabken; 07-28-2011 at 05:38 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default

    JukeBox$ArtistCompare?
    Where's ArtistCompare?

  3. #3
    Fabken is offline Member
    Join Date
    Nov 2010
    Posts
    18
    Rep Power
    0

    Default

    It was the first version without it, but here I editted with the ArtistCompare.
    But the error stays the same.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default

    When posting code and exceptions please ensure they match.
    It's a pretty simple thing.

    A stack trace from code that is not the code you are asking your question of is not only useless it cause confusion.

    Print out all the values from your string split. Either your title is null or an entire Song is null. The former is more likely.

  5. #5
    Fabken is offline Member
    Join Date
    Nov 2010
    Posts
    18
    Rep Power
    0

    Default

    And how do I solve that?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default

    I said..."Print out all the values from your string split".
    Or (in other words) you'll have to debug the code by sticking in lots of println() calls to see what's going on.

  7. #7
    Fabken is offline Member
    Join Date
    Nov 2010
    Posts
    18
    Rep Power
    0

    Default

    Thankyou I found my fault actually it was a quite stupid fault.
    In my Song class constructor I put t = title in place of title = t and because title had no value and the default value for a string is null I got a NullPointerException.
    But, I learn from my faults.
    Thankyou Troll.

Similar Threads

  1. Help about data structures and algorithms
    By herolua in forum New To Java
    Replies: 2
    Last Post: 05-21-2011, 05:57 AM
  2. Please help in data structures
    By pheonix in forum New To Java
    Replies: 0
    Last Post: 03-27-2009, 10:41 AM
  3. C like data structures in java ?
    By ankitmcgill in forum New To Java
    Replies: 10
    Last Post: 11-09-2008, 01:53 PM
  4. Sux data structures 1.0.1
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 04-15-2008, 06:54 PM
  5. Data Structures Help...
    By jac0117 in forum New To Java
    Replies: 1
    Last Post: 01-12-2008, 07:31 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •