exception in thread 'main' nullpointer exception error

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

34 Replies - 662 Views - Last Post: 10 October 2012 - 02:30 PM Rate Topic: -----

#16 codejilly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 09-October 12

Re: exception in thread 'main' nullpointer exception error

Posted 10 October 2012 - 11:48 AM

View Postpbl, on 10 October 2012 - 06:42 PM, said:

View Postcodejilly, on 10 October 2012 - 02:29 PM, said:

yes i did but i got errors so i took it out.....

It is these errors that we wanted to see :)

i slapped in the print line again because as you see i have again modified my selectionsort method.....
so here is the code with the print line in:::
public class TrackMain {
     static Track1[] tracks = new Track1[12];
    
     public static void main(String[] args){
        FileInputStream fs = null;
        try {
            try {
                  FileInputStream fis = new FileInputStream("G:\\OOP II\\ASSIGNMENT 1\\tracks.txt");
                  DataInputStream dis = new DataInputStream(fis);
                  BufferedReader br = new BufferedReader(new InputStreamReader(dis));
                  System.out.println(tracks.length);
                  String strLine;
                   
                  while((strLine = br.readLine()) != null){
                      System.out.println (strLine);
                  }
                      System.out.println("----------------------------------------------------------------");
                      dis.close();
           }catch (Exception e) { 
               System.err.println("Error: " + e.getMessage());
           }    
            
            selectionSort(tracks, 12);
            fs = new FileInputStream("G:\\OOP II\\ASSIGNMENT 1\\tracks.txt");
            DataInputStream ds = new DataInputStream(fs);
            BufferedReader b = new BufferedReader(new InputStreamReader(ds));
            
            String strln;
            
            while((strln = b.readLine()) != null){
                for(Track1 t : tracks){
                    System.out.println(t);
                    
                }
                               
                
            }
            
         }catch (IOException ex){
            Logger.getLogger(TrackMain.class.getName()).log(Level.SEVERE, null, ex);
       
         }
     }
     
     public static Track1[] selectionSort(Track1[] tracks, int mpos){
         Track1 tmp = null; 
        for (int i = 0; i < tracks.length - 1; ++i){  
            mpos = i; 
            for (int j = i + 1; j < tracks.length; ++j){ 
                System.out.println(mpos + ") " + tracks[mpos]);

                if(tracks[j].compareTo(mpos) > 0)  
                    mpos = j;  
            }
            
            tmp = tracks[i]; 
            tracks[i] = tracks[mpos];  
            tracks[mpos] = tmp;  
        }
        
        return tracks;
    }
     
}


and these are the errors i got....
0) null
Exception in thread "main" java.lang.NullPointerException
	at TrackCollections.TrackMain.selectionSort(TrackMain.java:67)
	at TrackCollections.TrackMain.main(TrackMain.java:38)
Java Result: 1

Was This Post Helpful? 0
  • +
  • -

#17 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2793
  • View blog
  • Posts: 11,827
  • Joined: 20-September 08

Re: exception in thread 'main' nullpointer exception error

Posted 10 October 2012 - 12:01 PM

You still haven't posted the code as is. There isn't even a line 67 in that code
Was This Post Helpful? 0
  • +
  • -

#18 codejilly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 09-October 12

Re: exception in thread 'main' nullpointer exception error

Posted 10 October 2012 - 12:09 PM

View Postg00se, on 10 October 2012 - 07:01 PM, said:

You still haven't posted the code as is. There isn't even a line 67 in that code

i don't know what program you're using but that's where my error points to.....
that's what i'm seeing....

here is the code in its entirety once more with the errors....

package TrackCollections;





import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;


public class TrackMain {
     static Track1[] tracks = new Track1[12];
    
     public static void main(String[] args){
        FileInputStream fs = null;
        try {
            try {
                  FileInputStream fis = new FileInputStream("G:\\OOP II\\ASSIGNMENT 1\\tracks.txt");
                  DataInputStream dis = new DataInputStream(fis);
                  BufferedReader br = new BufferedReader(new InputStreamReader(dis));
                  System.out.println(tracks.length);
                  String strLine;
                   
                  while((strLine = br.readLine()) != null){
                      System.out.println (strLine);
                  }
                      System.out.println("----------------------------------------------------------------");
                      dis.close();
           }catch (Exception e) { 
               System.err.println("Error: " + e.getMessage());
           }    
            
            selectionSort(tracks, 12);
            fs = new FileInputStream("G:\\OOP II\\ASSIGNMENT 1\\tracks.txt");
            DataInputStream ds = new DataInputStream(fs);
            BufferedReader b = new BufferedReader(new InputStreamReader(ds));
            
            String strln;
            
            while((strln = b.readLine()) != null){
                for(Track1 t : tracks){
                    System.out.println(t);
                    
                }
                               
                
            }
            
         }catch (IOException ex){
            Logger.getLogger(TrackMain.class.getName()).log(Level.SEVERE, null, ex);
       
         }
     }
     
     public static Track1[] selectionSort(Track1[] tracks, int mpos){
         Track1 tmp;
        for (int i = 0; i < tracks.length - 1; ++i){  
            mpos = i; 
            for (int j = i + 1; j < tracks.length; ++j){ 
                System.out.println(mpos + ") " + tracks[mpos]);

                if(tracks[j].compareTo(mpos) < 0)  
                    mpos = j;  
            }
            
            tmp = tracks[i]; 
            tracks[i] = tracks[mpos];  
            tracks[mpos] = tmp;  
        }
        
        return tracks;
    }
     
}



errors....

0) null
2	Exception in thread "main" java.lang.NullPointerException
3	    at TrackCollections.TrackMain.selectionSort(TrackMain.java:67)
4	    at TrackCollections.TrackMain.main(TrackMain.java:38)
5	Java Result: 1

Was This Post Helpful? 0
  • +
  • -

#19 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2793
  • View blog
  • Posts: 11,827
  • Joined: 20-September 08

Re: exception in thread 'main' nullpointer exception error

Posted 10 October 2012 - 12:23 PM

Quote

here is the code in its entirety once more with the errors....

Good. This time there IS a line 67

Quote

static Track1[] tracks = new Track1[12];

That array NEVER gets filled with instances of Track1, so all elements are null
Was This Post Helpful? 0
  • +
  • -

#20 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,885
  • Joined: 06-March 08

Re: exception in thread 'main' nullpointer exception error

Posted 10 October 2012 - 12:24 PM

0) null

so even the firsdt element of yiour array is null
so your array was never filled
Was This Post Helpful? 0
  • +
  • -

#21 codejilly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 09-October 12

Re: exception in thread 'main' nullpointer exception error

Posted 10 October 2012 - 12:28 PM

View Postpbl, on 10 October 2012 - 07:24 PM, said:

0) null

so even the firsdt element of yiour array is null
so your array was never filled

any suggestions as to how i fix that????
what sort of code do i have to write to solve that problem????
will appreciate the help.....

View Postg00se, on 10 October 2012 - 07:23 PM, said:

Quote

here is the code in its entirety once more with the errors....

Good. This time there IS a line 67

Quote

static Track1[] tracks = new Track1[12];

That array NEVER gets filled with instances of Track1, so all elements are null

any suggestions as to how i go about fixing that problem????
Was This Post Helpful? 0
  • +
  • -

#22 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2793
  • View blog
  • Posts: 11,827
  • Joined: 20-September 08

Re: exception in thread 'main' nullpointer exception error

Posted 10 October 2012 - 12:41 PM

Quote

any suggestions as to how i go about fixing that problem????

Fill the array with instances of Track1?

This post has been edited by g00se: 10 October 2012 - 12:42 PM

Was This Post Helpful? 0
  • +
  • -

#23 codejilly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 09-October 12

Re: exception in thread 'main' nullpointer exception error

Posted 10 October 2012 - 12:46 PM

View Postg00se, on 10 October 2012 - 07:41 PM, said:

Quote

any suggestions as to how i go about fixing that problem????

Fill the array with instances of Track1?


yes i figured as much but any idea what this code would look like???
any pointers????
Was This Post Helpful? 0
  • +
  • -

#24 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2793
  • View blog
  • Posts: 11,827
  • Joined: 20-September 08

Re: exception in thread 'main' nullpointer exception error

Posted 10 October 2012 - 12:49 PM

Well i don't know exactly as i don't know how your app is meant to work. I assume your file has something to do with Track1 but you haven't told us anything about it
Was This Post Helpful? 0
  • +
  • -

#25 codejilly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 09-October 12

Re: exception in thread 'main' nullpointer exception error

Posted 10 October 2012 - 12:53 PM

View Postg00se, on 10 October 2012 - 07:49 PM, said:

Well i don't know exactly as i don't know how your app is meant to work. I assume your file has something to do with Track1 but you haven't told us anything about it


my app is based on reading the contents from a text file filled with music tracks....
it's arranged in the following order in the file:
title
artist
album
length

the selectionSort method is then suppose to sort the tracks by its comparable nature on its title
and then output the results.....
Was This Post Helpful? 0
  • +
  • -

#26 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2793
  • View blog
  • Posts: 11,827
  • Joined: 20-September 08

Re: exception in thread 'main' nullpointer exception error

Posted 10 October 2012 - 12:59 PM

In that case, you must create instances of Track1, initialize their fields with data from the file then fill the array with the instances
Was This Post Helpful? 0
  • +
  • -

#27 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,885
  • Joined: 06-March 08

Re: exception in thread 'main' nullpointer exception error

Posted 10 October 2012 - 01:05 PM

and where do you create the Track objects from the lines read from the file ?
Was This Post Helpful? 0
  • +
  • -

#28 codejilly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 09-October 12

Re: exception in thread 'main' nullpointer exception error

Posted 10 October 2012 - 01:05 PM

View Postg00se, on 10 October 2012 - 07:59 PM, said:

In that case, you must create instances of Track1, initialize their fields with data from the file then fill the array with the instances


and the code for doing that would look like????
perhaps you can give me a few lines as to what it would look like and i'll proceed from there onwards
Was This Post Helpful? 0
  • +
  • -

#29 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,885
  • Joined: 06-March 08

Re: exception in thread 'main' nullpointer exception error

Posted 10 October 2012 - 01:11 PM

Did you ever posted your Track1 class ?
Was This Post Helpful? 0
  • +
  • -

#30 codejilly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 09-October 12

Re: exception in thread 'main' nullpointer exception error

Posted 10 October 2012 - 01:12 PM

View Postpbl, on 10 October 2012 - 08:11 PM, said:

Did you ever posted your Track1 class ?

no i didn't but here it is:

package TrackCollections;

public class Track1 implements Comparable{
    String title, artist, album;
    int length;
      
    public Track1(String t, String art, String alb, int len){
        this.title = t;
        this.artist = art;
        this.album = alb;
        this.length = len;
    }
    
   public String getTitle(){return title;}
   public void setTitle(String title){this.title = title;}
   
   public String getArtist(){return artist;}
   public void setArtist(String artist){this.artist = artist;}
   
   public String getAlbum(){return album;}
   public void setAlbum(String album){this.album = album;}
   
   public int getLength(){return length;}
   public void setLength(int length){this.length = length;}
   
   @Override
    public int compareTo(Object o)throws NullPointerException {
       Track1 t = (Track1)o;
       return title.compareTo(t.getTitle());
    }
        
    @Override
    public String toString(){
        String str = title + "\n"+ artist + "\n"+ album + "\n"+ length;
                return str;
    }

    
        
}

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3