java - NullPointerException in an Array Stack -
i'm trying implement stack using array program reverses sound clips, taking pieces of .dat file stack , popping them off 1 one. i'm getting nullpointerexception in pop() method whenever run client program, , can't figure out why.
here stack implementation:
import java.util.emptystackexception; public class arraystack implements dstack { private int size = 0; private static int default_capacity = 10; private double arrayst[]; public arraystack(){ arrayst = new double[default_capacity]; } public boolean isempty(){ boolean empty = true; if(arrayst.length != 0) { empty = false; } return empty; } public void push(double d){ if(size == arrayst.length){ increasecapacity(); } arrayst[size++] = d; } public double pop(){ boolean empty = isempty(); if(empty == true){ throw new emptystackexception(); } double d = arrayst[--size]; arrayst[size] = null; return d; } public double peek(){ boolean empty = isempty(); if(empty == true){ throw new emptystackexception(); } double stacktop = pop(); return stacktop; } private double[] increasecapacity(){ int arraylength = arrayst.length; int newcapacity = arrayst.length*2; double[] increasearray = new double[newcapacity]; for(int = 0; < arraylength; i++){ arrayst[i] = increasearray[i]; } return increasearray; } }
here client program:
import java.io.bufferedreader; import java.io.bufferedwriter; import java.io.filereader; import java.io.filewriter; import java.io.ioexception; import java.io.printwriter; import java.util.stringtokenizer; /** * read .dat file , reverse it. * * @version cse373, 14sp */ public class reverseredux { @suppresswarnings("unused") public static void main(string[] args) { if (args.length != 3) { system.err.println(" incorrect number of arguments"); system.err.println(" usage: "); system.err .println("\tjava reverse <stack type> <input file> <output file>"); system.exit(1); } boolean uselist = true; if (args[0].compareto("list") == 0) uselist = true; else if (args[0].compareto("array") == 0) uselist = false; else { system.err.println("\tsaw " + args[0] + " instead of list or array first argument"); system.exit(1); } try { // // set input file read, , output file write // bufferedreader filein = new bufferedreader(new filereader(args[1])); printwriter fileout = new printwriter(new bufferedwriter( new filewriter(args[2]))); // // read first line of .dat file sample rate. // want store sample rate value in variable, // can ignore "; sample rate" part of line. // step through first line 1 token (word) @ time // using stringtokenizer. fourth token 1 // want (the sample rate). // stringtokenizer str; string oneline; int samplerate; string strjunk; oneline = filein.readline(); str = new stringtokenizer(oneline); strjunk = str.nexttoken(); // read in semicolon strjunk = str.nexttoken(); // read in "sample" strjunk = str.nexttoken(); // read in "rate" // read in sample rate samplerate = integer.parseint(str.nexttoken()); // // read in remainder of file on line @ time. // values in first column thrown away. // place values second column on stack. // stop reading if reach end of file. // dstack s; if (uselist) s = new liststack(); else s = new arraystack(); string timestep; double data; int count = 0; while ((oneline = filein.readline()) != null) { if (oneline.charat(0) == ';') { continue; } str = new stringtokenizer(oneline); // read in time step value first column timestep = str.nexttoken(); // read in data value second column data = double.parsedouble(str.nexttoken()); s.push(data); count++; } system.out.println(count + " samples in file"); // // print data values output .dat file. // first, output header line: // "; sample rate <sample rate>" // fileout.println("; sample rate " + samplerate); // since first column consists of numbers start // @ 0 , increase 1/samplerate every time slice, we'll // use numsteps recalculate these numbers. int numsteps = 0; // finally, print values in reverse order (by popping // them off stack). first column consists of numbers // start @ 0 , increase 1/samplerate per row, // we'll use numsteps/samplerate recalculate appropriate // values. print tab uniform spacing. while (!s.isempty()) { fileout.println((double) numsteps / samplerate + "\t" + s.pop()); numsteps++; } // // close files // filein.close(); fileout.close(); } catch (ioexception ioe) { system.err .println("error opening/reading/writing input or output file."); system.exit(1); } catch (numberformatexception nfe) { system.err.println(nfe.tostring()); system.err.println("error in file format"); system.exit(1); } }
}
thanks help!
i guess line reason:
double d = arrayst[--size];
you have null in array position , converting primitive causes npe
Comments
Post a Comment