exception in thread 'main' nullpointer exception error

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

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

#1 codejilly  Icon User is offline

  • New D.I.C Head

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

exception in thread 'main' nullpointer exception error

Posted 09 October 2012 - 01:37 PM

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);
            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 (int i = 0; i < tracks.length; i++){ 
                    System.out.println(tracks[i]);
                    }
            }
            
         }catch (IOException ex){
            Logger.getLogger(TrackMain.class.getName()).log(Level.SEVERE, null, ex);
       
     }
     }

 public static void selectionSort(Track1[]tracks){
         Track1 min;
         for (int pos = 0; pos < tracks.length-1; pos ++){
            min = tracks [pos];
            int minpos = pos;
            for(int i = pos + 1; i < tracks.length; i++){
                if(tracks[i].compareTo(min) < 0){
                    min = tracks[i];
                    minpos = i;
                }
            }
            swap(tracks,minpos,pos);
            
         }
    }
     
    public static void swap(Track1[] t, int i, int j){
        Track1 temp = t[i];
        t[i] = t[j];
        t[j] = temp;
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: exception in thread 'main' nullpointer exception error

#2 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: exception in thread 'main' nullpointer exception error

Posted 09 October 2012 - 01:39 PM

Where exactly is your error?
Was This Post Helpful? 0
  • +
  • -

#3 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 09 October 2012 - 01:44 PM

i think it's somewhere in the selectionsort method?!?!?!
Was This Post Helpful? 0
  • +
  • -

#4 Kinaces  Icon User is offline

  • D.I.C Head

Reputation: 78
  • View blog
  • Posts: 230
  • Joined: 04-October 12

Re: exception in thread 'main' nullpointer exception error

Posted 09 October 2012 - 01:53 PM

Can't really help you unless you copy paste the error.
Was This Post Helpful? 0
  • +
  • -

#5 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 09 October 2012 - 01:58 PM

this is the error i get when i run the code:

Exception in thread "main" java.lang.NullPointerException
	at TrackCollections.TrackMain.selectionSort(TrackMain.java:62)
	at TrackCollections.TrackMain.main(TrackMain.java:38)
Java Result: 1

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: exception in thread 'main' nullpointer exception error

Posted 09 October 2012 - 06:12 PM

When you post your code do it with the package and import statement so we can match the error stack trace line number with the actual code

at TrackCollections.TrackMain.selectionSort(TrackMain.java:62)
at TrackCollections.TrackMain.main(TrackMain.java:38)

in class TrackMain of package TrackCollections what is the metjod selectionSort() what is line 62 ?

This post has been edited by pbl: 09 October 2012 - 06:14 PM

Was This Post Helpful? 0
  • +
  • -

#7 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 - 03:26 AM

View Postcodejilly, on 09 October 2012 - 08:58 PM, said:

this is the error i get when i run the code:

Exception in thread "main" java.lang.NullPointerException
	at TrackCollections.TrackMain.selectionSort(TrackMain.java:62)
	at TrackCollections.TrackMain.main(TrackMain.java:38)
Java Result: 1


the package and imports as requested.....

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;


i should also inform you that the selection error points to this line of code in the program....

if (tracks[pos].compareTo(tracks[minPos]) < 0)

Was This Post Helpful? 0
  • +
  • -

#8 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1400
  • View blog
  • Posts: 3,108
  • Joined: 05-April 11

Re: exception in thread 'main' nullpointer exception error

Posted 10 October 2012 - 03:30 AM

View Postcodejilly, on 10 October 2012 - 10:26 AM, said:

View Postcodejilly, on 09 October 2012 - 08:58 PM, said:

this is the error i get when i run the code:

Exception in thread "main" java.lang.NullPointerException
	at TrackCollections.TrackMain.selectionSort(TrackMain.java:62)
	at TrackCollections.TrackMain.main(TrackMain.java:38)
Java Result: 1


the package and imports as requested.....

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;


i should also inform you that the selection error points to this line of code in the program....

if (tracks[pos].compareTo(tracks[minPos]) < 0)


That line is not to find in the code you have shown us haha
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: exception in thread 'main' nullpointer exception error

Posted 10 October 2012 - 03:35 AM

Just the import are useless if they are not followed by the code :)
We need both to identify the line... anyhow as you have identified the line being:

if (tracks[pos].compareTo(tracks[minPos]) < 0)

preceed it by:

System.out.println(pos + ") " + tracks[pos]);
System.out.println(minPos + ") " + tracks[minPos]);

you'll identify which one is null and then based on that you'll be able to continue your investigation
Was This Post Helpful? 0
  • +
  • -

#10 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 - 03:42 AM

View Postpbl, on 10 October 2012 - 10:35 AM, said:

Just the import are useless if they are not followed by the code :)
We need both to identify the line... anyhow as you have identified the line being:

if (tracks[pos].compareTo(tracks[minPos]) < 0)

preceed it by:

System.out.println(pos + ") " + tracks[pos]);
System.out.println(minPos + ") " + tracks[minPos]);

you'll identify which one is null and then based on that you'll be able to continue your investigation

this is the entire program.....
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);
            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 (int i = 0; i < tracks.length; i++){ 
                    System.out.println(tracks.toString());
                    }
            }
            
         }catch (IOException ex){
            Logger.getLogger(TrackMain.class.getName()).log(Level.SEVERE, null, ex);
       
     }
     }

 public static void selectionSort(Comparable[] tracks){
         int minPos; Comparable temp;
         for (int index = 0; index < tracks.length-1; index++){
             minPos = index;
             for (int pos = index+1; pos < tracks.length; pos++)
                 
                 if (tracks[pos].compareTo(tracks[minPos]) < 0)
                     minPos = pos;
                     temp = tracks[minPos];
                     tracks[minPos] = tracks[index];
                     tracks[index] = temp;
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#11 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1400
  • View blog
  • Posts: 3,108
  • Joined: 05-April 11

Re: exception in thread 'main' nullpointer exception error

Posted 10 October 2012 - 03:59 AM

Indent your code properly
I bet you have error in the following code cause of missing indention so you don't notice your errors
 public static void selectionSort(Comparable[] tracks){
         int minPos; Comparable temp;
         for (int index = 0; index < tracks.length-1; index++){
             minPos = index;
             for (int pos = index+1; pos < tracks.length; pos++)
                 
                 if (tracks[pos].compareTo(tracks[minPos]) < 0)
                     minPos = pos;
                     temp = tracks[minPos];
                     tracks[minPos] = tracks[index];
                     tracks[index] = temp;
        }
    }
}




Was this what you meant to do?
 public static void selectionSort(Comparable[] tracks){
         int minPos; Comparable temp;
         for (int index = 0; index < tracks.length-1; index++) {
             minPos = index;
             for (int pos = index+1; pos < tracks.length; pos++)                
                 if (tracks[pos].compareTo(tracks[minPos]) < 0) {
                     minPos = pos;
                     temp = tracks[minPos];
                     tracks[minPos] = tracks[index];
                     tracks[index] = temp;
				}
        }
    }
}



If that was not your intention, then just indent your code so it doesn't confuse the reader

This post has been edited by CasiOo: 10 October 2012 - 04:01 AM

Was This Post Helpful? 0
  • +
  • -

#12 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 - 04:04 AM

View PostCasiOo, on 10 October 2012 - 10:59 AM, said:

Indent your code properly
I bet you have error in the following code cause of missing indention so you don't notice your errors
 public static void selectionSort(Comparable[] tracks){
         int minPos; Comparable temp;
         for (int index = 0; index < tracks.length-1; index++){
             minPos = index;
             for (int pos = index+1; pos < tracks.length; pos++)
                 
                 if (tracks[pos].compareTo(tracks[minPos]) < 0)
                     minPos = pos;
                     temp = tracks[minPos];
                     tracks[minPos] = tracks[index];
                     tracks[index] = temp;
        }
    }
}




Was this what you meant to do?
 public static void selectionSort(Comparable[] tracks){
         int minPos; Comparable temp;
         for (int index = 0; index < tracks.length-1; index++) {
             minPos = index;
             for (int pos = index+1; pos < tracks.length; pos++)                
                 if (tracks[pos].compareTo(tracks[minPos]) < 0) {
                     minPos = pos;
                     temp = tracks[minPos];
                     tracks[minPos] = tracks[index];
                     tracks[index] = temp;
				}
        }
    }
}



If that was not your intention, then just indent your code so it doesn't confuse the reader

that's not the problem because sorry i sent the wrong code snippet but in my other code everything is indented as it should be.....so that's not it....thanks anyway for the suggestion
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: exception in thread 'main' nullpointer exception error

Posted 10 October 2012 - 11:23 AM

Don't split your :code:

public class TrackMain {  


is surely not at line 1 it has all the import before. By doing this we cannot referenece the line number displayed in the stack trace

Did you add the println() I told you to add ?
Was This Post Helpful? 0
  • +
  • -

#14 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:29 AM

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

Don't split your :code:

public class TrackMain {  


is surely not at line 1 it has all the import before. By doing this we cannot referenece the line number displayed in the stack trace

Did you add the println() I told you to add ?

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

here is the whole code:

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 = null; 
        for (int i = 0; i < tracks.length - 1; ++i){  
            mpos = i; 
            for (int j = i + 1; j < tracks.length; ++j){  
                if(tracks[j].compareTo(mpos) > 0)  
                    mpos = j;  
            }
            
            tmp = tracks[i]; 
            tracks[i] = tracks[mpos];  
            tracks[mpos] = tmp;  
        }
        
        return tracks;
    }
     
}


Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: exception in thread 'main' nullpointer exception error

Posted 10 October 2012 - 11:42 AM

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 :)
Was This Post Helpful? 0
  • +
  • -

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