exception in thread 'main' nullpointer exception error

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

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

#31 pbl  Icon User is offline

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

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

Re: exception in thread 'main' nullpointer exception error

Posted 10 October 2012 - 01:28 PM

so you have to call

public Track1(String t, String art, String alb, int len){

with the data read from the file and store the Track1 object created into your array
Was This Post Helpful? 0
  • +
  • -

#32 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:35 PM

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

so you have to call

public Track1(String t, String art, String alb, int len){

with the data read from the file and store the Track1 object created into your array

i think i did that because i redid my main again and added this piece of code
when i called the selectionSort method....

here it is:

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 (int i = 0; i < tracks.length; i++ ){
                    String t = b.readLine();
                    String art = b.readLine();
                    String alb = b.readLine();
                    int len = b.read();
                    Track1 t1 = new Track1 (t,art,alb,len);
                    tracks [i] = t1;
                
                    System.out.println(t1.toString());
            
                }
                
            }
            
         }catch (IOException ex){
            Logger.getLogger(TrackMain.class.getName()).log(Level.SEVERE, null, ex);
       
         }
     }

Was This Post Helpful? 0
  • +
  • -

#33 pbl  Icon User is offline

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

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

Re: exception in thread 'main' nullpointer exception error

Posted 10 October 2012 - 01:43 PM

Not really

int len = b.read();

this read as a int a single character from the file, does not read the ASCII representation of an integer

You wioll have to readLine() in a String and then use Integer.parseInt() to store in a int the String representation of the number in the file
Was This Post Helpful? 0
  • +
  • -

#34 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:59 PM

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

Not really

int len = b.read();

this read as a int a single character from the file, does not read the ASCII representation of an integer

You wioll have to readLine() in a String and then use Integer.parseInt() to store in a int the String representation of the number in the file


i tried that by changing the code to this in calling the selectionSort method:
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((b.readLine()) != null){
                for (int i = 0; i < tracks.length; i++ ){
                    String t = b.readLine();
                    String art = b.readLine();
                    String alb = b.readLine();
                    String len = b.readLine();
                    int n = Integer.parseInt(len);
                    
                    Track1 t1 = new Track1 (t,art,alb,len);
                    tracks [i] = t1;
                
                    System.out.println(t1.toString());
            
                }
                
            }
            
         }catch (IOException ex){
            Logger.getLogger(TrackMain.class.getName()).log(Level.SEVERE, null, ex);
       
         }
     }

and am still getting the same error....
this is bumming me out
Was This Post Helpful? 0
  • +
  • -

#35 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2656
  • View blog
  • Posts: 11,197
  • Joined: 20-September 08

Re: exception in thread 'main' nullpointer exception error

Posted 10 October 2012 - 02:30 PM

Use a Scanner. 's'

while(s.hasNext()) {
  tracks[index++] = new Track1(s.next(), s.next(), s.next(), s.nextInt());
}


That's the last code i'm posting on this
Was This Post Helpful? -1
  • +
  • -

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